Learning Objectives
At the end of this Module, you will be able to:- Understand the concept of multiple inheritance
- Identify and solve the ambiguities that arise in multiple inheritance.
- Define Virtual base class
- State the order of invocation of constructors and destructors
Introduction
Multiple Inheritance
Multiple Inheritance is the concept where a subclass inherits properties from multiple base classes. A familiar example of multiple inheritance is the child inheriting the characteristics of the parents.You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance.
In the following example, classes A, B, and C are direct base classes for the derived class X:
The following inheritance graph describes the inheritance relationships of the above example. An arrow points to the direct base class of the class at the tail of the arrow:
class A { /* ... */ }; class B { /* ... */ }; class C { /* ... */ }; class X : public A, private B, public C { /* ... */ };
Ambiguities in Multiple Inheritance
When a class inherits from multiple base classes, a whole lot of ambiguities creep in. For example , what happens when two base classes contain a function of the same name?For example given below --> Class A , Class B has Member functions disp() (to display the attributes of the class) and Class C inherits A and B.
class A { /* ... */ public: void disp(void); }; class B { /* ... */ public: void disp(void); }; class C : public A, private B { /* ... */ }; void main(){ C c; C.disp();//Ambiguous }
class C : public A, private B { /* ... */ public: void disp(void){ A.disp(); B.disp(); } }; void main(){ C c; c.disp(); }
void main(){ C c; c.A::disp(); c.B.::disp(); }
class L { /* ... */ }; // indirect base class class B2 : public L { /* ... */ }; class B3 : public L { /* ... */ }; class D : public B2, public B3 { /* ... */ }; // valid
B2::L
B3::L
Virtual Base Classes
The principle behind virtual base classes is very simple. When the same class is inherited more than once via multiple paths, multiple copies of the base class members are created in memory. By declaring the base class inheritance as virtual, only one copy of the base class is inherited. A base class inheritance can be specified as virtual using virutal qualifier.The class definitions would be modified as follows
class L { /* ... */ }; // indirect base class class B1 : virtual public L { /* ... */ }; class B2 : virtual public L { /* ... */ }; class D : public B1, public B2 { /* ... */ }; // valid
Invocation of Constructors and Destructors
- Constructors are invoked in the following order:
- Virtual base class constructors -- in the order of inheritance.
- Non-Virtual base class constructors -- in the order of inheritance.
- Member objects constructors -- in the order of declaration
- Derived class constructor.
- Destructor are invoked in the reverse order
Resources
Problem Sets
Problem Set A
1. Create a set of classes named A, B, C, D, E, F, & G with the following inheritance relationship:Give each class a constructor and destructor only that print simple trace messages to the screen('like Constructor A or Destructor A ). Use non-virtual inheritance. Test each of the classes individually by creating objects of each type in a Test Program. Study the constructor and destructor trace messages and submit the trace.
The Trace document should contain the trace output for each of the classes individually by creating objects of each type.
Name the program as: PA1_NonVirtualInheritance.cpp Name the Trace Document as: PA1_NonVirtualInheritance.doc
Problem Set B
1. Create a set of classes named A, B, C, D, E, F, & G with the following inheritance relationship:Give each class a constructor and destructor only that print simple trace messages to the screen('like Constructor A or Destructor A ). Use virtual qualifier in the inheritance wherever necessary. Test each of the classes individually by creating objects of each type in a Test Program. Study the constructor and destructor trace messages and submit the trace.
The Trace document should contain the trace output for each of the classes individually by creating objects of each type.
Name the program as: PB1_VirtualInheritance.cpp Name the Trace Document as: PB1_VirtualInheritance.doc
Problem Set C
1. Thomson publishing company markets both books and video-cassette. Create a class called Publication that stores the title(string) and price(float) of a publication. From this class derive two classes: Book which adds a page count (type int); and VideoCassette, which adds a playing time (type int) in minutes. Each of these classes should have a getdata() function to get its data from the user and a putdata()function to display its data.Add another base class Sales that holds an array of three floats so that it can record the sales of a particular publication for the last three months. Include a getdata() function to get three sales amounts from the user, and a putdata() function to display the sales figures. Alter the book andtape classes so that they are derived from both Publication and Sales. An object of class book or tape should input or output sales along with its other data. Write a main() function to create a book object and a tape object and which will update their corresponding member data along with the sales made.
Comments
Post a Comment