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

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.