Question: Does C++ support single inheritance?
Answer: Yes, C++ supports the concept of single inheritance between the classes.
In the figure, the sum is the parent class and child is the child class and if the child class wants to sum number 1 and number, then the child can get it from the parent class sum.
These concepts can be represented with a simple C++ program. So, let’s begin with the program.
Example of C++ supporting the single inheritance
C++ program to print a hollow square or rectangle star pattern with the support of single 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 single inheritance
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 |
#include<iostream> #include<conio.h> using namespace std; class one { protected: int i,j,n; }; class child:public one { 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 obj; obj.in(); getch(); } |
Output