Skip to main content

Multiple Inheritance

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.
father_mother.jpg
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 { /* ... */ };
abc_x.jpg

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
}
Here the reference to disp() is ambiguous because the compiler does not know whether disp() refers to the member in class base1 or base2. This ambiguity can be resolved using the scope resolution operator as illustrated hereunder.
class C : public A, private B {
/* ... */
public:
   void disp(void){
      A.disp();
      B.disp();
   }
};
void main(){
 C c;
 c.disp(); 
}
This ambiguity can also be resolved by overriding as illustrated hereunder.
void main(){
 C c;
 c.A::disp();
 c.B.::disp();
}
Another ambiguity that arises in multiple inheritance is the possibility of the derived class having multiple copies of the same base class. Consider the following diagram.
bl_d.jpg
class L { /* ... */ };                  // indirect base class
class B2 : public L { /* ... */ };
class B3 : public L { /* ... */ };
class D : public B2, public B3 { /* ... */ }; // valid
In the above example, class D inherits the indirect base class L once through class B2 and once through class B3. However, this may lead to ambiguities because two objects of class L exist, and both are accessible through class D. You can avoid this ambiguity by referring to class L using a qualified class name. For example:
B2::L
or
B3::L
You can also avoid this ambiguity by using the base specifier virtual to declare a base class.

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:
pa1.jpg
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:
pa1.jpg
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

Popular posts from this blog

Mini Project - Railway Reservation System

Module 1 Overview Railway Reservation System-(RRS) Project is developed to help students learn and realize capability of C programming language, and appreciate procedural approach of developing a system. RRS helps users reserve/cancel berths, search for trains, check reservation/cancellation history, etc... On the other hand it helps the railway reservation officers to prepare reservation charts, organize train schedules, etc... RRS provides two types of log-ins one is the User log-in for railway customers and the other one is Admin log-in for railway reservation officers. Project is divided into 10 modules of four hours duration each. All the modules will give exactly what to develop in the 4 hour time frame. Since most of the modules are inter-related it is important to keep track of the previous code. After submission it is allowed to make changes in the previous code to suit / integrate well to the system. Before we start the project, we need to make the followin...

Inheritance

Week 1 - M5 : Inheritance Learning Objectives At the end of this module, you will be able to Understand the implementation of inheritance in C++ Override the base class members Access overridden base class members using the scope resolution operator Understand the concept of base class initialization Introduction Inheritance The philosophy behind inheritance is to portray things as they exist in the real world. As inheritance is found in real world, it is important feature of OO programming. Inheritance has many advantages, the most important of them being the resuability of code. Once a class is defined and debugged, it can be used to create new subclasses. The reuse of existing class saves time and effort. The Class from which another class is derived is called the base class . The class which inherits the properties of the base class is called the derived class . Each instance of the derived class includes all the members of the base class. Since...