Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multiple Inheritance in C++

Similar presentations


Presentation on theme: "Multiple Inheritance in C++"— Presentation transcript:

1 Multiple Inheritance in C++
How to do it Why to do it This could still be slicked up some Copyright © Curt Hill

2 Multiple Inheritance Recall the grad class
It inherited behavior from both student and person This is not multiple inheritance Multiple inheritance is the inheriting of behavior from two different classes These classes are not usually in the same inheritance tree Copyright © Curt Hill

3 Syntax Just about what you might expect class C: public A, public B {
The C class inherits all the properties and methods of both A and B Class C is interchangeable with both A and B classes Copyright © Curt Hill

4 Characteristics of Multiple Inheritance
When it is good it is wonderful A good example is iostreams Inherit from a string formatting object (stream) and a file I/O object (io) Thus have characteristics of both When it is bad it is horrendous C++ is the main language to retain multiple inheritance Smalltalk had it then removed it Java never had – uses Interface instead Eiffel seems to have done the most with it Copyright © Curt Hill

5 Problems with Multiple Inheritance
Main problem is name clashes and what to do with them Suppose two classes have a method X A new class is a derivation of both How do we get one X over the other When both ancestors are from same tree problems compound Copyright © Curt Hill

6 Bad Example Consider: class grademployee: public grad, public employ{…
This looks reasonable, but now there are two names, two birth dates, two genders How do we distinguish between the access to the grad set_name or the employ set_name? We may use the scope resolution operator but it still gets ugly Copyright © Curt Hill

7 Accessing Names In the previous example we might want to override set_name A normally stupid thing to do Thus we get: void grademployee::set_name (char * n){ strcpy(grad::name, n); strcpy(employ::name, n); What is the point of two names? Copyright © Curt Hill

8 Why use? We often have a class that may serve in several roles
These roles are implemented by classes and we have functions that use these classes Another indicator is to use multiple inheritance when it will reduce the number of ifs and switches in the code It is often preferable to use abstract base classes instead Not always possible Copyright © Curt Hill

9 Example Every person functions in many roles Two of these include
As a member of a family As an employee The person then has as its properties: name etc. It then does multiple inheritance on the Family and Employee class It is now with an isa relationship to both of those Example project Copyright © Curt Hill

10 Demo Commentary A Dev multiple inheritance demo exists
Dev does have some problems with large numbers of files Readding the file may be needed The Person class is multiply derived from Family and Employment Thus it may be treated as any one of these We see this in the call to SummarizeMS Copyright © Curt Hill

11 Commentary In the SummarizeMS call a person object is passed
What it actually expects is a Family object This could also be done with composition Then we would have to develop one or more methods to direct calls to the property Copyright © Curt Hill

12 Conclusion Multiple inheritance gives us some more power
It is much less common to use multiple inheritance than single inheritance Yet IOStreams show a good reason to use We should now look at a demo with family and employee Copyright © Curt Hill


Download ppt "Multiple Inheritance in C++"

Similar presentations


Ads by Google