Re: organising classes c++

From: Jason (jason.carney1_at_btinternet.com)
Date: 11/20/03


Date: Wed, 19 Nov 2003 23:21:16 +0000 (UTC)


"David White" <no@email.provided> wrote in message
news:LISub.1783$xm.87676@nasal.pacific.net.au...
> "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?

sorry to use jargon, by association i just meant making a class known to
another class such as by including one object as an attribute of another
object. So the question was does the class definition have to be in a
separate header file to be visible to the outside world, but that question
has already been answered by yours and others responses so thanks.

>
> > 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: 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)
  • 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)