Multiple Inheritance Program in C++
Posted by Superadmin on August 08 2022 09:06:01

Multiple Inheritance Program in C++

 

 

This C++ program illustrates multiple inheritance. The concept of multiple inheritance is used when derivation is to be done from two or more classes. The features available in the classes from which derived class is to be constructed are available in the derived class.

 

Here is the source code of the C++ program which illustrates multiple inheritance. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to illustrate multiple inheritance
  3.  */
  4. #include <iostream>        
  5. using namespace std;
  6.  
  7. class A {
  8.     int i;
  9.     public:
  10.         A() : i(1) {
  11.             cout << "A's constructor\n";
  12.         }
  13.         void printI(){
  14.     	    cout << "i = " << i << "\n";
  15.         }
  16. };
  17.  
  18. class B {
  19.     char c;
  20.     public:
  21.         B() : c('a') {
  22.             cout << "B's constructor\n";
  23. 	}
  24.  
  25. 	void printC(){
  26.     	    cout << "c = " << c << "\n";
  27.         }
  28. };
  29.  
  30. class C : public A, public B {
  31.     public:
  32.         C() {
  33.             cout << "C's constructor\n";
  34. 	}
  35. };
  36.  
  37. int main () {
  38.     C c;
  39.  
  40.     c.printI();
  41.     c.printC();
  42. }

 

$ gcc test.cpp
$ a.out
A's constructor
B's constructor
C's constructor
i = 1
c = a