c++ headers and classes

This is a discussion on c++ headers and classes within the C++ Programming forums, part of the General Programming Boards category; So suppose I have a big C++ program with a class called Hello. Suppose furthermore that Hello has 3 methods ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    12

    c++ headers and classes

    So suppose I have a big C++ program with a class called Hello.

    Suppose furthermore that Hello has 3 methods foo(), bar(), asdf().

    Now, my question what the usual way to store the classes is. Should Hello go in a file called Hello.hpp? Should the definitions of the methods also go in that file? Should they all be in a regular .cpp file?

    Thanks!!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Normally, the class definition would go into a header caller Hello.h (with appropriate include guards). Some people use .hpp instead of .h, so you could do that if you preferred.

    That class definition would include the declarations of the three member functions.

    The three member functions would be defined in Hello.cpp, which would #include "Hello.h".

    Things might be different if you want an inline function or if you're using templates, but that's the basic idea.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    The goal should always be to put as little in your header files as possible, only what must be shared with other source files. Keep what you can in the cpp file.
    For one you reduce recompile times when you change something.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Headers and Classes
    By MMD_Lynx in forum Game Programming
    Replies: 2
    Last Post: 12-01-2004, 09:40 AM
  2. Headers, Classes, and Functions question.
    By Zeusbwr in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2004, 03:18 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21