Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate header files for concrete classes - C++

Background

I have an abstract class, something like

class IConverter{
    public:
    virtual void DoConvertion() = 0;
};

There will be many concrete classes which just implements DoConvertion method.

class TextConverter : public IConverter{
    public:
    virtual void DoConvertion(){
         // my code goes here
     }
};

class ImageConverter : public IConverter{
    public:
    virtual void DoConvertion(){
         // my code goes here
     }
};

There will be many concrete implementation like this. I have created a header file say, CharacterConverter.h which has the abstract class IConverter.

Question

Since my concrete classes just implement the DoConvertion method, is it required to create separate header files for each concrete class? I mean is it required to create ImageConverter.h, TextConverter.h and so on for all concrete classes? All these header files is going to contain the same code like IConverter abstract class.

Any thoughts?

like image 576
Navaneeth K N Avatar asked Feb 18 '09 01:02

Navaneeth K N


People also ask

Can you have multiple header files in C?

Note: We can't include the same header file twice in any program. Create your own Header File: Instead of writing a large and complex code, we can create your own header files and include them in our program to use it whenever we want.

Do you put classes in header files?

Traditionally, the class definition is put in a header file of the same name as the class, and the member functions defined outside of the class are put in a . cpp file of the same name as the class.

How do you prevent multiple inclusions of header files?

To avoid multiple inclusions of the same header file we use the #ifndef, #define and #endif preprocessor directives. Just write the entire program in only file and include headers once only.


7 Answers

It is not required. It's basically a judgment call.

If the implementation is simple for each class you can put them all in one .h and one .cpp

If the implementations are a bit longer, then it's probably cleaner to use a separate .h and .cpp file for each.

Some advantages of using a different .h/.cpp for each class:

  • It will keep the code organized and clean
  • Reduced compiling work: A change in one of the implementations won't need to recompile all others
  • Faster compiling time: Several compilers can compile multiple files at once such as Visual Studio's /MP switch. With several files you'll have a faster compile time.
  • Other files can include only what they need instead of everything
  • Faster link time: Linking time will be reduced due to incremental linking
  • Using version control you can look back on only the changes to a particular derived class, instead of having to go through all changes made to the massive 1 .h/.cpp file to find that one change in a particular derived class.
like image 200
Brian R. Bondy Avatar answered Oct 04 '22 16:10

Brian R. Bondy


One of the main points of creating an interface class is so that clients can be depend on the abstract interface rather than the concrete implementation, and you are then free to change the implementation without impacting clients.

Putting the concrete declarations in the same header files as the interface declarations defeats this, so now if you change an implementation detail of a concrete class, your clients would need to re-compile.

like image 37
JohnMcG Avatar answered Oct 02 '22 16:10

JohnMcG


Something you might consider, depending on the rest of your design, is a factory, where your abstract class has a static method (or multiple static methods, depending on how you implement it) that constructs the appropriate subclass and returns it as an IConverter*. With this, you can expose only the abstract definition in the header file, and have all the concrete class definitions and implementations in a single .cpp file along with the super class implementation. This gets a bit unwieldy if your subclass are large, but with smaller classes it reduces the number of files you have to manage.

But, as others have pointed out, it's ultimately a judgment call. The only performance issues would be related to compiling; more cpp files might take (slightly) longer to compile and more header files might increase dependency analysis. But there's no requirement that every header file have a matching cpp and vice verse.

Based on the comments, I'd recommend a structure like this:

IConverter.h ==> definition of IConverter
Converters.h ==> definitions of all subclasses
IConverter.cpp ==> include IConverter.h and Converters.h, contain implementation of IConverter abstract functionality (static factory method and any inheritable functionality)
TextConvter.cpp, ImagerConverter.cpp, etc. ==> seperate cpp files for each subclass, each containing IConverter.h and Converters.h

This allows you to only include the IConverter.h in any clients that use the factory and generic functionality. Putting all the other definitions in a single header allows you to consolidate if they're all basically the same. Separate cpp files allow you to take advantage of the compiler benefits mentioned by Brian. You could inline the subclass definitions in header files as mentioned, but that doesn't really buy you anything. Your compiler is usually smarter than you are when it comes to optimizations like inline.

like image 33
mbyrne215 Avatar answered Oct 04 '22 16:10

mbyrne215


You'll probably get answers both ways.

I'd say, for any trivial converters, having all of them in a single .h/.cpp pair is sufficient and that it's overkill to split every one into a single pair. I think the tradeoff of maintenance of lots of files vs. maintenance of a bunch of methods within a single file is worth it in this case.

Complex conversions probably deserve their own file pairs.

like image 34
Joe Avatar answered Oct 01 '22 16:10

Joe


You will need definitions of the concrete classes to create objects, so you'll need to put those definitions into a .h file somewhere. Which file you put them in is up to you.

like image 42
Mark Ransom Avatar answered Oct 04 '22 16:10

Mark Ransom


The best answer to this is what's easier to read. One long source file is going to be difficult for you and other programmers to follow. On the other hand, many tiny (half screen-full) source files is just as bad.

like image 32
SingleNegationElimination Avatar answered Oct 02 '22 16:10

SingleNegationElimination


You'd probably be better off using factories or function pointers.

However, one particularly nasty way that springs to mind is using a macro to declare your concrete classes. For example:

At the bottom of IConverter.h include the following macro

#define DECLARE_CONVERTER_CLASS(CLASS_NAME) \
class CLASS_NAME : public IConverter\
{ \
    public: \
    CLASS_NAME() {} \
    virtual void DoConversion(); \
}; \

Then in MyConverter1.cpp

DECLARE_CONVERTER_CLASS(MyConverter1)

virtual void MyConverter1::DoConversion()
{
    ...
}

Yuck :-)

like image 31
Nick Avatar answered Oct 02 '22 16:10

Nick