473,387 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Does one need a header file?

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
7 2225
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lawrence | last post by:
I'm trying to read up on the rfc's that govern form inputs. Much of what I'm reading is stuff I didn't know before and some of it is alarming. This one left with me questions: ...
7
by: deko | last post by:
I need to allow users to download files from a directory that is not publicly accessible. To do this, I use a download script: header("Content-Type: application/octet-stream");...
12
by: Fred Pacquier | last post by:
First off, sorry for this message-in-a-bottle-like post... I haven't been able to phrase my questions well enough to get a meaningful answer from Google in my research. OTOH, it is standard...
74
by: Suyog_Linux | last post by:
I wish to know how the free()function knows how much memory to be freed as we only give pointer to allocated memory as an argument to free(). Does system use an internal variable to store allocated...
5
by: wheel | last post by:
I've never tried this but, what if Access (2003) is not installed on the server, in the classic front end back end setup? I know sometimes Jet can process requests on the server, returning less...
22
by: macAWM | last post by:
Hi list, First let me explain that my background is in Java and I am quite spoiled to its niceties (read "less ambiguous nature"). Anyway to my problems. 1. I want to write my own library for...
15
by: amit.man | last post by:
Hi, i have newbie qestion, when i write #include <somthing.h> the precompiler subtitue that line with the lines from "somthing.h" header file. when, where and how the compiler insert the...
14
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the...
2
by: Lambda | last post by:
The code is simple: // Token.h #ifndef TOKEN_H #define TOKEN_H #include <vector> #include <string> class Token
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.