views:

1795

answers:

8

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?

+1  A: 

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.

Joe
+6  A: 

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.
Brian R. Bondy
Thanks. Implementation are bit longer for concrete classes. So I am worried about the performance difference if I put it in header file.
Appu
+1  A: 

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.

Mark Ransom
Thanks. Will it make any performance issues if I put the concrete class in header file without creating CPP file for each? I am going to include all concrete classes in only one place.
Appu
If you want to put the full implementation of each class into a .h file, you just need to declare each function as inline. This may result in a little code bloat, as you'll get at least one copy in each file you call it from - but usually not too much to worry about. Might even be faster.
Mark Ransom
+1  A: 

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.

TokenMacGuy
+1  A: 

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.

mbyrne215
Yeah. It uses a factory to create appropriate child classes. Unfortunatly, I can't have it in a single CPP file as the implementation will be longer for each concrete class. Do you recommend keeping it in header files and not using CPP files? I am worried about the performance issues it may bring.
Appu
No, you should definitely have the implementations in cpp files. You could easily have a single header file with all the definitions, and each implementation in a separate cpp file, though. What performance issues exactly are you worried about?
mbyrne215
That's great help. Thanks
Appu
+1  A: 

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.

JohnMcG
+1  A: 

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 :-)

Nick
A: 

i need conc.h header file