Multiple Inheritance in C++ - a complete example
Archive - Originally posted on "The Horse's Mouth" - 2010-03-12 07:31:31 - Graham EllisC++ and some other OO languages support multiple inheritance ... Java and some others do not, and it's often argued that multiple inheritance is an unnecessary complication. That may be the case in some languages, but certainly in Perl 5 it is necessary to have it to make use of eome of the modules supplied with the distribution.
Yesterday, I gave a tailored C++ course for an application which includes some quite somplex class relationships, and as a result I have produced a new and complete working example of multiple inheritance in C++ - it's a real "tutor's example" in that it uses the complexity for the sake of it, but it does show you the mechanisms.
Here are the files to look through:
Filmtest.cpp - the main program
HireFilm.cpp - the class that the main program uses, which inherits from both Film and Expense
Film.cpp - one of the base classes
Expense.cpp - the other base class
HireFilm.h - the header file for the class that does the multiple inheritance. This is where the interesting stuff is!
Film.h - The header file for one of the base classes
Expense.h - The header file for the other base class
Makefile - the file of dependencies and instructions that let you build the whole thing.
The "interesting stuff" is in HireFilm.h, which includes:
class HireFilm : public Film, public Expense {
public:
HireFilm(int boxo) : Film(boxo),unit(6.7) {};
void setcost(float);
float getcost(int);
HireFilm * longer(HireFilm *);
private:
float unit;
};
You'll see that the class is simply defined as inheriting first from Film and second from Expense (and there's a need to add in more logic if Film and Expense include conflicing methods).
And I have chosen as part of this demo to also show you how we can control which constructor a subclass calls in its base class(es) and how any extra parameters are set:
HireFilm(int boxo) : Film(boxo),unit(6.7) {};
A new Hire Film is to call up a base film (passing in a parameter that was passed in to hire film) as opposed to the default constructore call which will be to the base class constructor without parameters. It then sets the unit variable in the object to 6.7. Finally (and implicity - it's not stated in this line of code) it will call the base constructor of the Expense class without any parameters. Calls to base class constructors are always made, whether explicity or implicitly, as the variables within the base object need memory allocating for them.
There probably are times that multiple inheritance is a good idea in C++, but there are more times that it's used but unnecessary. The example above shows you how ... in "just" 7 source files, all of which you can click on, compile up and link through using the instruction in the 8th file - the Makefile.
We offer no less that 3 C++ courses - for delegates who have never programmed before, for delegates who have programmed before (but not in C), and for delegates who are converting from C. See [here] for further details of them.