Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Multiple Inheritance in C++ - a complete example

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 {
    HireFilm(int boxo) : Film(boxo),unit(6.7) {};
    void setcost(float);
    float getcost(int);
    HireFilm * longer(HireFilm *);
    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)

Back to
Dear Planners, please provide viable alternatives
Previous and next
Horse's mouth home
Forward to
Make and makefiles - a commented example to help you learn
Some other Articles
Blowing out the winter cobwebs
Changing Times
Redirecting to your main domain for correct security keys
Multiple Inheritance in C++ - a complete example
Dear Planners, please provide viable alternatives
Melksham - Carnival, Party in the Park, and Spot the Oddity
Pointers to Pointers to Pointers - what is the point?
Efficient use of dynamic memory - C and realloc
Is it worth it?
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2673_Mul ... ample.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb