C++ classes - declaration/definition

Programming, for all ages and all languages.
Post Reply
Rui
Posts: 11
Joined: Sun Jan 25, 2009 6:53 am
Location: Portugal

C++ classes - declaration/definition

Post by Rui »

Hello there, I was thinking, what's the advantages of splitting classes in two files like in C++, beside the fact the code gets a cleaner etc. Am I a bad person if I just use only one file like in Java? :D
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: C++ classes - declaration/definition

Post by pcmattman »

Probably one of the biggest advantages is that if you modify the class implementation, you don't have to recompile every file that includes it.

For instance, on my project we have some minor class methods implemented in header files which are included by 20-30 different files, and when you modify that implementation you have a fairly large recompile ahead of you :)

Splitting the class implementation from the prototype can also make it a bit easier to remember the class interface when you need it - rather than trawl through a header littered with implementation you have a nice clean view of the class.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: C++ classes - declaration/definition

Post by JamesM »

Hi,
Rui wrote:Hello there, I was thinking, what's the advantages of splitting classes in two files like in C++, beside the fact the code gets a cleaner etc. Am I a bad person if I just use only one file like in Java? :D
To the second question, the answer is 100% yes ;)

There is a large reason why. In C/C++ as opposed to Java, there is an explicit preprocessor. What does this mean/do? Well, your "#include" statement, and anything with a "#" before it incidentally such as #ifdef etc are never seen by the compiler itself. (Please pedants for the sake of argument ignore that gcc has a builtin preprocessor as the same principle applies).

So an #include in C or C++ is largely just a text-replace - replacing the #include line with the content of the file you're including. This simplicity is, incidentally, why you need to add include guards in C/C++ (#ifndef MYHEADER_H #define MYHEADER_H etc). When the resultant file gets to the compilation (parse) stage, it can be rather large as it is just one big file containing everything needed.

So, if you put all your class code in the header file and start #including them, the resultant intermediate file passed to the compiler is going to be huge and is going to result in slower builds.

So that's the first reason. The second reason is that header files provide a nice way of having interfaces; What I mean by that is that someone should be able to look at the header file for a class and instantly see what functions it exports and what they do (by comments). The actual implementation of the functions is in the .cc file, and this seperation of interface and implementation is a Good Thing. I'll leave other more zealot C++ programmers to tell you why ;)

So to summarise, yes it's a bad thing, put your code in .cc files.
Rui
Posts: 11
Joined: Sun Jan 25, 2009 6:53 am
Location: Portugal

Re: C++ classes - declaration/definition

Post by Rui »

In fact, it makes all sense. Thanks a lot for the fantastic and quik reply :)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: C++ classes - declaration/definition

Post by Solar »

pcmattman wrote:Probably one of the biggest advantages is that if you modify the class implementation, you don't have to recompile every file that includes it.
Worse. You don't have multiple translation units. Whenever you change a single line of code anywhere, you have to recompile everything.

Because either you #include everything together into one big translation unit, or your linker will give you hell about "multiple definitions", as every .cpp file including e.g. your string.cpp will contain the definitions of its functions - which the linker won't accept.
Every good solution is obvious once you've found it.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: C++ classes - declaration/definition

Post by gravaera »

I personally, in my own projects do my headers like this:

Code: Select all

#ifndef __CLASS_NAME_H
#define __CLASS_NAME_H

//
//Declarations:
//
class foo
{
   foo(void);
   ~foo(void);
   int bar(char *);
};


//
//Definitions:
//
foo::foo(void)
{
   //Statements
};

foo::~foo(void)
{
   //Statements
};

int foo::bar(char *ch)
{
   while(ch)
   {
      //Statements
   }
   return(1);
};

#endif
I then have only one file for each header, with an orderly organization of declarations at the top, for easy reading, just like you peoples' *.h files, and then my definitions below, like your separate *.cpp files.

That way I don't have to keep two whole files open, and move between windows/tabs to look around. It's kind of irritating, I believe.
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: C++ classes - declaration/definition

Post by Combuster »

And then you decide to use OGRE, which makes compilation cost an additional 20 secs if you include it somewhere.
Now, you have 10 classes, and you change one line, the time spent compiling the whole lot will take you three and a half minutes.

Enough time to put your office chair in the corridor and play out DND acts. :mrgreen:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: C++ classes - declaration/definition

Post by NickJohnson »

Combuster wrote:Enough time to put your office chair in the corridor and play out DND acts. :mrgreen:
Like this: http://www.xkcd.org/303/?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: C++ classes - declaration/definition

Post by JamesM »

holypanl wrote:I personally, in my own projects do my headers like this:

Code: Select all

#ifndef __CLASS_NAME_H
#define __CLASS_NAME_H

//
//Declarations:
//
class foo
{
   foo(void);
   ~foo(void);
   int bar(char *);
};


//
//Definitions:
//
foo::foo(void)
{
   //Statements
};

foo::~foo(void)
{
   //Statements
};

int foo::bar(char *ch)
{
   while(ch)
   {
      //Statements
   }
   return(1);
};

#endif
I then have only one file for each header, with an orderly organization of declarations at the top, for easy reading, just like you peoples' *.h files, and then my definitions below, like your separate *.cpp files.

That way I don't have to keep two whole files open, and move between windows/tabs to look around. It's kind of irritating, I believe.

I'd take a look at the arguments presented above by myself, pcmattman and Solar and see if that alters your opinion any.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: C++ classes - declaration/definition

Post by Owen »

Combuster wrote:And then you decide to use OGRE, which makes compilation cost an additional 20 secs if you include it somewhere.
Now, you have 10 classes, and you change one line, the time spent compiling the whole lot will take you three and a half minutes.

Enough time to put your office chair in the corridor and play out DND acts. :mrgreen:
Precompiled Headers to the rescue!

I chuck OGRE, Qt and all the other monumentously huge libraries into one header, spend half an hour building that into a 300MB blob, then GCC sails through really quickly :)
tarrox
Posts: 19
Joined: Wed Dec 31, 2008 8:40 am

Re: C++ classes - declaration/definition

Post by tarrox »

holypanl wrote: I then have only one file for each header, with an orderly organization of declarations at the top, for easy reading, just like you peoples' *.h files, and then my definitions below, like your separate *.cpp files.

That way I don't have to keep two whole files open, and move between windows/tabs to look around. It's kind of irritating, I believe.
It is less irritating then to have to scroll trough the whole file everytime, you want to look something up or to change.

In addition i have to agree with Solar. "Multiple definitions" is a hell that comes with you if you dont splitt up.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: C++ classes - declaration/definition

Post by Solar »

General rule of thumb: When you're new in town, do as the natives do.
Every good solution is obvious once you've found it.
Post Reply