Re: organising classes c++

From: David White (no_at_email.provided)
Date: 11/20/03


Date: Thu, 20 Nov 2003 10:06:38 +1100


"Jason" <jason.carney1@btinternet.com> wrote in message
news:bpgrp5$970$1@sparta.btinternet.com...
> 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();
> }

Place this in your header file only, and #include the header in this source
file. Unless the class is used in only one source file, put it in a header
file.

> char classname::classname getchar() {
> return anyhing;
> }

Placing this in a source file (.cpp in your case) is correct. However, any
member functions that you want inline need to be in the header file - either
within the class definition itself or with the 'inline' keyword outside the
class.

> and then in a separate header file I placed a copy of the class
definition -
> obviously not the function implementations.

Don't make copies of class definitions. Any changes that you want to make
will have to made in two places. Just include the header that contains the
single definition, as explained above.

> Is that how classes make their interfaces known to other classes so a
class
> in another file just includes <"classname.h">.

Use ONE of <> and "", not both. Use <> for system headers and "" is for your
own, e.g.,

#include <string>
#include "MyClass.h"

> Must I have a separate
> header file to the main .cpp file in order to create associations?

What do you mesan by associations?

> Does the
> main .cpp file have to include the class definition here and in the header
> file?

A program doesn't have to have a main.cpp. It just has to have a 'main'
function. You can put this function anywhere and there are no special rules
regarding header files for it. You #include header files in any source file
when the source file needs them. That's all.

> When you have lots of classes how do most c++ programmers organise
> them, just as described or different?

Some people use the convention of one class per source file, but I think
that's too rigid. I'll often put closely related classes in the same file.
There are no rules for this. Just use common sense.

DW



Relevant Pages

  • 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)
  • Re: organising classes c++
    ... > and then in a separate header file I placed a copy of the class definition - ... > header file to the main .cpp file in order to create associations? ... function definitions in a source file ...
    (alt.comp.lang.learn.c-cpp)
  • 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)
  • Common Environment Object Model: getting the path to the cpp file a certain class is implemented in
    ... Say we have a CodeClass object and call AddFunction() on it. ... the call will end up with the function declaration ... in the header file for that class and the implementation in the cpp file. ...
    (microsoft.public.vsnet.general)
  • Re: Novice - Advice required
    ... > a seperate .cpp file (or header file?) and then whenever I want to ... int XYX::doTask ...
    (microsoft.public.vc.language)