Re: organising classes c++

From: Josh Sebastian (curien_at_cox.net)
Date: 11/19/03


Date: Wed, 19 Nov 2003 17:50:40 -0500

On Wed, 19 Nov 2003 22:44:54 +0000, Jason wrote:

> hello,
>
> How do I organise my classes in c++? At the moment I have something like
> this in a .cpp file
>
> class classname { //class definition
> private:
> //...
> public:
> //...stuff
> char getchar();
> }
>
> char classname::classname getchar() {
> return anyhing;
> }
>
> and then in a separate header file I placed a copy of the class definition -
> obviously not the function implementations.
>
> Is that how classes make their interfaces known to other classes so a class
> in another file just includes <"classname.h">. Must I have a separate
> header file to the main .cpp file in order to create associations? Does the
> main .cpp file have to include the class definition here and in the header
> file? When you have lots of classes how do most c++ programmers organise
> them, just as described or different?
>
> I would be grateful for any comments as I just want to be clear on these
> points.

Put the class definition in a header file (say, class.h) and put the
function definitions in a source file (say, class.cpp). Then include the
header in any source file that needs to use the class. Also, just include
class.h at the beginning of class.cpp (no need to duplicate it).

If you have several classes that depend on one another, you can put them
in the same header if you want. Basically, do whatever makes life easier
for people using your classes.

Josh



Relevant Pages

  • Is a struct preserved when used into a cpp file?
    ... I have a .h that declares a struct: ... I have used the header file above into a .cpp (C++ source file). ... .cpp file uses the .lib functions, ...
    (comp.lang.cpp)
  • Re: Not using .cpp files when programming...
    ... >> right in the .H header file with the class definition. ... > Your compiles will be slower. ... > functions static or inline in the class to avoid the name space problems, ...
    (comp.programming)
  • Re: Javas performance far better that optimized C++
    ... > If you want you can place the class definition in the header file, ... > collector destroys the object (for managed objects). ... It is not the same as destructor, it is used to call destructors ...
    (comp.lang.cpp)
  • Re: organising classes c++
    ... jsut one other point. ... > header file to the main .cpp file in order to create associations? ... > main .cpp file have to include the class definition here and in the header ...
    (alt.comp.lang.learn.c-cpp)
  • parser stack overflow, program too complex eroor C1026
    ... Apparently there is a limit to how complex a single header file with a huge ... class definition in it can be. ... This diagnostic occurred in the compiler generated function 'void ...
    (microsoft.public.dotnet.languages.vc)