.cpp file without .h file

C

Carmen Sei

Is that .h file is only needed when I need other Classes or .cpp file
to use the function?

If that function is only used in one .cpp file, then I don't need to
write a .h file?
 
I

Ian Collins

Carmen said:
Is that .h file is only needed when I need other Classes or .cpp file
to use the function?
Or you want to split the declarations form the definitions.
If that function is only used in one .cpp file, then I don't need to
write a .h file?

No.
 
M

Michael.Boehnisch

Is that .h file is only needed when I need other Classes or .cpp file
to use the function?

If that function is only used in one .cpp file, then I don't need to
write a .h file?

For a valid C++ program you formally do not need any .h files, not
even the ones provided by your compiler environment. You are free to
put every bit of your source code into a single, big .cpp file and it
will work. For some programming languages (e.g. Pascal without
proprietary extensions, ancient Cobol versions, old BASIC dialects)
this is the only way to organize source code.

However, separating implementation and declaration of functionality is
considered a smart thing - it allows easy reuse of code in other
modules of the same program or even share to other programs. You
should welcome the feature and make it a habit to:

1. Put only one class into any source module. Programming languages
like Java and C# strongly encourage or even enforce this for good
reason.
2. Always create a .h and a .cpp file for each class. You never know
when you'll need the same feature again and this will make it easy to
incorporate the class into another program.
3. Speaking of #2, when you design a class, try to make it potentially
useful for other programs - keep the interface lean and avoid
aggregation of too many functionalities. Use inheritance to specialize
for your current problem. If you incorporate too many other classes,
you will have to move all of them to the next project in order to use
only one function.

I'll try to elaborate on this in an example. Say, you want to write an
application about car insurances. A weak approach would be to put
everything into one class: properties of the car mixed with properties
of the insurance:

// CarInsurance.h:
class CarInsurance {
public:
std::string brand; // maker of the car
std::string model; // model of the car
int maxspeed; // maximum speed in mph
std::string company; // name of insurance company
int limit; // limit of insurance coverage per incident
/* ... */
};

This restricts the reusability of your class a lot. I consider it
better to make three classes, each with own .h and .cpp file. You will
be able to cover other applications that deal with cars *or*
insurances without the burden of the unneeded context:

// Car.h:
class Car {
public:
std::string brand; // maker of the car
std::string model; // model of the car
int maxspeed; // maximum speed in mph
/* ... */
};

// Insurance.h
class Insurance {
public:
std::string company; // name of insurance company
int limit; // limit of insurance coverage per incident
/* ... */
};

// CarInsurance.h
#include "Car.h"
#include "Insurance.h"

class CarInsurance : public Car, public Insurance {
/* ... */
};

Methods and .cpp files left out as an exercise to the reader :) -
Note how the .h files make it easy to construct the derived class.

It may sound dogmatic to follow these principles even when you do not
feel you need this level of architectural cleanliness in the problem
at hand. After some time it will become the natural thing to do,
though, and you will appreciate how much easier it is to keep track of
your own programs.

best,

Michael.
 
P

Paul Brettschneider

For a valid C++ program you formally do not need any .h files, not
even the ones provided by your compiler environment. You are free to
put every bit of your source code into a single, big .cpp file and it
will work. For some programming languages (e.g. Pascal without
proprietary extensions, ancient Cobol versions, old BASIC dialects)
this is the only way to organize source code.

However, separating implementation and declaration of functionality is
considered a smart thing - it allows easy reuse of code in other
modules of the same program or even share to other programs. You
should welcome the feature and make it a habit to:

1. Put only one class into any source module. Programming languages
like Java and C# strongly encourage or even enforce this for good
reason.

Incidentally, the one file per class rule has always been one of the
Java "features" I detested most. From time to time I happen to have a class
which is unwieldily large and is a good candidate for splitting into
multiple .cpp files. Or I have a collection of very simple but closely
related classes which I put into one file. Another example is a class for
which one method is autogenerated. Then it makes sense to put this one
method into a separate file. And as long as this is documented in the
header files, why not?
2. Always create a .h and a .cpp file for each class. You never know
when you'll need the same feature again and this will make it easy to
incorporate the class into another program.
3. Speaking of #2, when you design a class, try to make it potentially
useful for other programs - keep the interface lean and avoid
aggregation of too many functionalities. Use inheritance to specialize
for your current problem. If you incorporate too many other classes,
you will have to move all of them to the next project in order to use
only one function.
[...example snipped...]
 
I

Ian Collins

Paul said:
Incidentally, the one file per class rule has always been one of the
Java "features" I detested most. From time to time I happen to have a class
which is unwieldily large and is a good candidate for splitting into
multiple .cpp files.

A class which is too large shouldn't have its definition split over
multiple source files, it should be refactored into smaller classes with
specific responsibilities.
 
P

Paul Brettschneider

Ian said:
A class which is too large shouldn't have its definition split over
multiple source files, it should be refactored into smaller classes with
specific responsibilities.

Sure, sometimes. But definitely not always. There's no point in unnaturally
splitting a class. You gain nothing and yet have the burden of writing and
maintaining pointless interfaces. I'm so glad that I don't have to put up
with Java's enforcement of some arbitrary (and IMHO often
counter-productive) coding style. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top