Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
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 {
  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
C234 - C and C based languages - Further C++ Object Oriented features
  [4559] When do I use the this keyword in C++? - (2015-10-29)
  [4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)
  [4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
  [3982] Using a vector within an object - C++ - (2013-01-19)
  [3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
  [3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
  [3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
  [3124] C++ - putting the language elements together into a program - (2011-01-08)
  [3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
  [3057] Lots of things to do with and within a C++ class - (2010-11-16)
  [2849] What are C++ references? Why use them? - (2010-07-02)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2576] What does const mean? C and C++ - (2010-01-15)
  [2005] Variables and pointers and references - C and C++ - (2009-01-23)
  [2004] Variable Scope in C++ - (2009-01-22)
  [1819] Calling base class constructors - (2008-10-03)
  [1159] It can take more that one plus one to get two. - (2007-04-22)
  [831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
  [802] undefined reference to typeinfo - C++ error message - (2006-07-15)
  [801] Simple polymorphism example - C++ - (2006-07-14)

C239 - C and C based languages - Putting it all together
  [4374] Test driven development, and class design, from first principles (using C++) - (2014-12-30)
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
  [3067] Using C and C++ functions in the same program - how to do it - (2010-11-24)
  [2851] Further C++ material - view new or old - (2010-07-04)
  [2674] Make and makefiles - a commented example to help you learn - (2010-03-12)
  [2646] Compile but do not run PHP - syntax check only - (2010-02-22)
  [1181] Good Programming practise - where to initialise variables - (2007-05-09)
  [945] Code quality counts - (2006-11-26)
  [925] C++ - just beyond the basics. More you can do - (2006-11-14)
  [836] Build on what you already have with OO - (2006-08-17)


Back to
Dear Planners, please provide viable alternatives
Previous and next
or
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., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2673_Mul ... ample.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat