Does C++ support Multiple inheritance
Question: Does C++ support Multiple inheritance?
Answer: Yes, C++ supports the concept of Multiple inheritance between the classes.
In the figure, the class A and class B are the parents of class C.
These concepts can be represented with a simple C++ program. So, let’s begin with the program.
Example of C++ supporting the Multiple inheritance
C++ program to print a hollow square or rectangle star pattern with the support of Multiple inheritance.
In this example, one is the parent of the class of child class. You can run the program as proof that C++ supports the Multiple inheritance.
In the following program Class one and two are the parent of class child.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include<iostream> using namespace std; class one { protected: int i,j; }; class two { protected: int n; }; class child:public one,public two { public: int in() { cout<<"enter number "; cin>>n; for(i=1;i<=n;i++) {
for(j=1;j<=n;j++) { if(i==1||i==n||j==1||j==n) { cout<<"*"; } else { cout<<" "; } } cout<<" \n"; } } }; int main() { child c; c.in(); } |
Output