C++ 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.
(written 2010-03-12)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C239 - C and C based languages - Putting it all together [836] Build on what you already have with OO - (2006-08-17)
[925] C++ - just beyond the basics. More you can do - (2006-11-14)
[945] Code quality counts - (2006-11-26)
[1181] Good Programming practise - where to initialise variables - (2007-05-09)
[2646] Compile but do not run PHP - syntax check only - (2010-02-22)
[2674] Make and makefiles - a commented example to help you learn - (2010-03-12)
[2851] Further C++ material - view new or old - (2010-07-04)
[3067] Using C and C++ functions in the same program - how to do it - (2010-11-24)
[3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
[3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
[3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
[4326] Learning to program - comments, documentation and test code - (2014-11-22)
[4374] Test driven development, and class design, from first principles (using C++) - (2014-12-30)
[4559] When do I use the this keyword in C++? - (2015-10-29)
C234 - C and C based languages - Further C++ Object Oriented features [801] Simple polymorphism example - C++ - (2006-07-14)
[802] undefined reference to typeinfo - C++ error message - (2006-07-15)
[831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
[1159] It can take more that one plus one to get two. - (2007-04-22)
[1819] Calling base class constructors - (2008-10-03)
[2004] Variable Scope in C++ - (2009-01-22)
[2005] Variables and pointers and references - C and C++ - (2009-01-23)
[2576] What does const mean? C and C++ - (2010-01-15)
[2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
[2849] What are C++ references? Why use them? - (2010-07-02)
[3057] Lots of things to do with and within a C++ class - (2010-11-16)
[3124] C++ - putting the language elements together into a program - (2011-01-08)
[3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
[3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
[3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
[3982] Using a vector within an object - C++ - (2013-01-19)
[4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
[4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)
Some other Articles
Blowing out the winter cobwebsChanging TimesRedirecting to your main domain for correct security keysMultiple Inheritance in C++ - a complete exampleDear Planners, please provide viable alternativesMelksham - Carnival, Party in the Park, and Spot the OddityPointers to Pointers to Pointers - what is the point?Efficient use of dynamic memory - C and reallocIs it worth it?