Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Does one need a header file?

Reply
Thread Tools

Does one need a header file?

 
 
The Cool Giraffe
Guest
Posts: n/a
 
      02-24-2007
Please note that i do intend to use a header file. However,
i'm not sure if it's really needed or just a convention.

Suppose we have the following two files.

// Something.h
class Something {
private: int number;
public:
Something ();
~Something ();
void doSome ();
};

// Something.cpp
#include "Something.h"
int number;
Something::Something () {this.number = 10;}
Something::~Something () {}
void Something::doSome () {this.number = 5;}

Can one turn that into one, single CPP-file? How?

--
Vänligen Kerstin Viltersten
(The Cool Giraffe)


 
Reply With Quote
 
 
 
 
Scott McPhillips [MVP]
Guest
Posts: n/a
 
      02-24-2007
The Cool Giraffe wrote:
> Please note that i do intend to use a header file. However,
> i'm not sure if it's really needed or just a convention.
>
> Suppose we have the following two files.
>
> // Something.h
> class Something {
> private: int number;
> public:
> Something ();
> ~Something ();
> void doSome ();
> };
>
> // Something.cpp
> #include "Something.h"
> int number;
> Something::Something () {this.number = 10;}
> Something::~Something () {}
> void Something::doSome () {this.number = 5;}
>
> Can one turn that into one, single CPP-file? How?


Well sure. Just copy the class declaration into the cpp file. That's
really all that #include does anyway.

On the other hand, you will need the header file if there are other cpp
files in your project and they need to refer to class Something. Those
other cpp files need an #include so they will know what they are dealing
with.

--
Scott McPhillips [VC++ MVP]

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      02-24-2007
The Cool Giraffe wrote:
> Please note that i do intend to use a header file. However,
> i'm not sure if it's really needed or just a convention.
>
> Suppose we have the following two files.
>
> // Something.h
> class Something {
> private: int number;
> public:
> Something ();
> ~Something ();
> void doSome ();
> };
>
> // Something.cpp
> #include "Something.h"
> int number;
> Something::Something () {this.number = 10;}
> Something::~Something () {}
> void Something::doSome () {this.number = 5;}
>
> Can one turn that into one, single CPP-file? How?
>


First point

'int number;' in Something.cpp is unnecessary. It's a completely
different thing from the number in the Something class. Get rid of int
number form Something.cpp.

Main point

There are two kinds of 'thing' in C++, those that can be compiled only
once and those that can be compiled multiple times in one program. When
you put (say) a class definiton in a header and then include that header
file in two cpp files you are compiling that class definition twice
within the same program. This is OK because class definitions are one of
the things that you can compile several times in one program.

An example of something that you cannot compile multiple times in one
program is a non-inline function definition. If you tried to put a
non-inline function definition in a header file and included that header
file in two seperate cpp file you would get some sort of multiple
definition error.

Inline functions are different, they can be compiled multiple times and
that is why it is OK to put them in a header file. In fact no matter
what any might tell you different, that is the meaning of 'inline' in
C++. It allows you to compile a function multiple times.

Now the C++ standard only talks about what can be compiled once and what
can be compiled multiple times. It's only a convention that we group all
the thing that can be compiled multiple times together and put them into
something we call a header file, and that we put all the things that can
only be compiled once only into something we call a cpp file. And then
to make sure that the stuff in a cpp file doesn't get compiled multiple
times we have a couple more conventions, 'compile all the cpp files
individually ', and 'don't include one cpp file in another'.

Now to answer your question, if you where to put all that code into
Something.cpp, the only way any other code could access the class
definition would be to include Something.cpp. Because you code includes
things which can be compiled only once (the non-inline constructor,
destructor and doSome methods) that would only work provided it was done
only once (and provided you didn't compile Something.cpp idependently).

If you really want only one file, then the correct way would be to make
the whole thing a header file, my making all the constructors etc. inline.

Hope this answers your question.

john
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      02-24-2007
Scott McPhillips [MVP] wrote:
> The Cool Giraffe wrote:
>
>> Please note that i do intend to use a header file. However,
>> i'm not sure if it's really needed or just a convention.
>>
>> Suppose we have the following two files.
>>
>> // Something.h
>> class Something {
>> private: int number;
>> public:
>> Something ();
>> ~Something ();
>> void doSome ();
>> };
>>
>> // Something.cpp
>> #include "Something.h"
>> int number;
>> Something::Something () {this.number = 10;}
>> Something::~Something () {}
>> void Something::doSome () {this.number = 5;}
>>
>> Can one turn that into one, single CPP-file? How?

>
>
> Well sure. Just copy the class declaration into the cpp file. That's
> really all that #include does anyway.
>
> On the other hand, you will need the header file if there are other cpp
> files in your project and they need to refer to class Something. Those
> other cpp files need an #include so they will know what they are dealing
> with.
>


Well you could just copy and paste the class definition into multiple
cpp files, but i'm sure you weren't meaning to suggest that when you
said 'that's really all #include does'.

john
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      02-24-2007
"The Cool Giraffe" <> wrote in message
news:...
> Please note that i do intend to use a header file. However,
> i'm not sure if it's really needed or just a convention.
>
> Suppose we have the following two files.
>
> // Something.h
> class Something {
> private: int number;
> public:
> Something ();
> ~Something ();
> void doSome ();
> };
>
> // Something.cpp
> #include "Something.h"
> int number;
> Something::Something () {this.number = 10;}
> Something::~Something () {}
> void Something::doSome () {this.number = 5;}
>
> Can one turn that into one, single CPP-file? How?


All the #include statment does is take the file that is included and put it
into the current file.

That is, when you say
#include "Something.h"
then everything that is in Something.h is put into the current file during
compile time. So, all you would have to do it not use the include file is
do this manually, go into the include file, copy everything, paste it into
the .cpp file.

The main reason that include files are used is to use the same definitions
in two different compilation units (.cpp files).


 
Reply With Quote
 
The Cool Giraffe
Guest
Posts: n/a
 
      02-25-2007
Scott McPhillips [MVP] wrote/skrev/kaita/popisal/schreibt :
> The Cool Giraffe wrote:


>> Suppose we have the following two files.
>> // Something.h
>> // Something.cpp
>> <source code snipped>
>> Can one turn that into one, single CPP-file? How?

>
> Well sure. Just copy the class declaration into the cpp file. That's
> really all that #include does anyway.
>
> On the other hand, you will need the header file if there are other
> cpp files in your project and they need to refer to class Something. Those
> other cpp files need an #include so they will know what they
> are dealing with.


What if i make a creation as below? What will we see when
main is being executed?

// header file (something.h)
class Something
{
public:
int number;
Something ();
void doSome ();
};

// cpp file (implem1.cpp)
#include "something.h"
Something::Something () {}
void Something::doSome () {cout << 1;}

// cpp file (implem2.cpp)
#include "something.h"
Something::Something () {}
void Something::doSome () {cout << 2;}

// cpp file (testing.cpp)
#include "something.h"
void main ()
{
Something some ();
some.doSome ();
}


--
Vänligen Kerstin Viltersten
(The Cool Giraffe)


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      02-25-2007
The Cool Giraffe wrote:
> Scott McPhillips [MVP] wrote/skrev/kaita/popisal/schreibt :
>
>>The Cool Giraffe wrote:

>
>
>>>Suppose we have the following two files.
>>>// Something.h
>>>// Something.cpp
>>><source code snipped>
>>>Can one turn that into one, single CPP-file? How?

>>
>>Well sure. Just copy the class declaration into the cpp file. That's
>>really all that #include does anyway.
>>
>>On the other hand, you will need the header file if there are other
>>cpp files in your project and they need to refer to class Something. Those
>>other cpp files need an #include so they will know what they
>>are dealing with.

>
>
> What if i make a creation as below? What will we see when
> main is being executed?
>
> // header file (something.h)
> class Something
> {
> public:
> int number;
> Something ();
> void doSome ();
> };
>
> // cpp file (implem1.cpp)
> #include "something.h"
> Something::Something () {}
> void Something::doSome () {cout << 1;}
>
> // cpp file (implem2.cpp)
> #include "something.h"
> Something::Something () {}
> void Something::doSome () {cout << 2;}
>
> // cpp file (testing.cpp)
> #include "something.h"
> void main ()
> {
> Something some ();
> some.doSome ();
> }
>
>


That code is an error because you are compiling Something::Something and
Something::doSome twice (non-inline constructors and methods are one of
the things you are not allowed to compile twice). Most likely that code
will not compile, and you will get some sort of multiple definition error.

john
 
Reply With Quote
 
The Cool Giraffe
Guest
Posts: n/a
 
      02-25-2007
John Harrison wrote/skrev/kaita/popisal/schreibt :
> The Cool Giraffe wrote:
>> Scott McPhillips [MVP] wrote/skrev/kaita/popisal/schreibt :
>>
>>> The Cool Giraffe wrote:

>>
>>>> Suppose we have the following two files.
>>>> // Something.h
>>>> // Something.cpp
>>>> <source code snipped>
>>>> Can one turn that into one, single CPP-file? How?
>>>
>>> Well sure. Just copy the class declaration into the cpp file. That's
>>> really all that #include does anyway.
>>>
>>> On the other hand, you will need the header file if there are other
>>> cpp files in your project and they need to refer to class
>>> Something. Those other cpp files need an #include so they will know
>>> what they are dealing with.

>>
>>
>> What if i make a creation as below? What will we see when
>> main is being executed?
>>
>> // header file (something.h)
>> class Something
>> {
>> public:
>> int number;
>> Something ();
>> void doSome ();
>> };
>>
>> // cpp file (implem1.cpp)
>> #include "something.h"
>> Something::Something () {}
>> void Something::doSome () {cout << 1;}
>>
>> // cpp file (implem2.cpp)
>> #include "something.h"
>> Something::Something () {}
>> void Something::doSome () {cout << 2;}
>>
>> // cpp file (testing.cpp)
>> #include "something.h"
>> void main ()
>> {
>> Something some ();
>> some.doSome ();
>> }

>
> That code is an error because you are compiling Something::Something
> and Something::doSome twice (non-inline constructors and methods are
> one of the things you are not allowed to compile twice). Most likely
> that code will not compile, and you will get some sort of multiple
> definition error.



Aha, that's what i was suspecting. Still,
thanks for the clarification.

--
Vänligen Kerstin Viltersten
(The Cool Giraffe)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
Header files included in header files John Smith C Programming 18 07-24-2004 04:55 AM
What is better /standard for creating files. a cpp file with header or cpp and seperate file for header DrUg13 C++ 1 02-10-2004 09:20 AM
how to avoid using another header file inside a header file? Newsgroup - Ann C++ 4 11-02-2003 01:20 PM
distutils: How "uninclude" one Python file and/or one .h (C header) file??? Christian Seberino Python 1 10-22-2003 10:52 PM



Advertisments