
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Multiple Inheritance in C++
Multiple inheritance occurs when a class inherits from more than one base class. So the class can inherit features from multiple base classes using multiple inheritance. This is an important feature of object oriented programming languages such as C++.
A diagram that demonstrates multiple inheritance is given below −
A program to implement multiple inheritance in C++ is given as follows −
Example
#include <iostream> using namespace std; class A { public: int a = 5; A() { cout << "Constructor for class A" << endl; } }; class B { public: int b = 10; B() { cout << "Constructor for class B" << endl; } }; class C: public A, public B { public: int c = 20; C() { cout << "Constructor for class C" << endl; cout<<"Class C inherits from class A and class B" << endl; } }; int main() { C obj; cout<<"a = "<< obj.a <<endl; cout<<"b = "<< obj.b <<endl; cout<<"c = "<< obj.c <<endl; return 0; }
Output
The output of the above program is given as follows −
Constructor for class A Constructor for class B Constructor for class C Class C inherits from class A and class B a = 5 b = 10 c = 20
In the above program, classes A and B are defined. This is given below −
class A { public: int a = 5; A() { cout << "Constructor for class A" << endl; } }; class B { public: int b = 10; B() { cout << "Constructor for class B" < endl; } };
Class C inherits from both classes A and B. It is an example of multiple inheritance. Class C definition is shown below −
class C: public A, public B { public: int c = 20; C() { cout << "Constructor for class C" << endl; cout<<"Class C inherits from class A and class B" << endl; } };
In main() function, an object obj of class C is defined. The constructors of Class A, B and C are automatically called and their contents are displayed. Then the values of a, b and c are printed. These are data members of classes A, B and C respectively. The code snippet for this is as follows −
C obj; cout<<"a = "<< obj.a <<endl; cout<<"b = "<< obj.b <<endl; cout<<"c = "<< obj.c <<endl;
- Related Articles
- Multiple inheritance in JavaScript
- Java and multiple inheritance
- C# and Multiple Inheritance
- Multiple inheritance by Interface in Java
- Does Python support multiple inheritance?
- Java Program to Implement Multiple Inheritance
- Haskell Program to Implement multiple inheritance
- Why multiple inheritance is not supported in Java
- How multiple inheritance is implemented using interfaces in Java?
- How we can extend multiple Python classes in inheritance?
- Why multiple inheritance is not supported by Java?
- What is diamond problem in case of multiple inheritance in java?
- How does Python's super() work with multiple inheritance?
- Does Java support multiple inheritance? Why? How can we resolve this?
- Inheritance in Python
