By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Does one need a header file?

P: n/a
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)
Feb 24 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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]

Feb 24 '07 #2

P: n/a
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
Feb 24 '07 #3

P: n/a
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
Feb 24 '07 #4

P: n/a
"The Cool Giraffe" <gi******@viltersten.comwrote in message
news:54*************@mid.individual.net...
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).
Feb 24 '07 #5

P: n/a
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)
Feb 25 '07 #6

P: n/a
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
Feb 25 '07 #7

P: n/a
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)
Feb 25 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.