WAP to implement single inheritance in C++.
Example Program
WAP in C++ to display such a pattern for n number of rows using a number which will start with the number 1 and the first and the last number of each row will be 1 with the help of multiple inheritances. The pattern is as follows:
In the following program, the child is the child class of the parent class.
WAP to implement 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> using namespace std; class parent { protected: int i,j,k,l,n; }; class child:public parent { public: int show() { cout<<"num of rows:"; cin>>n; for(i=1;i<=n;i++) { for(l=1;l<=n-i;l++) { cout<<" "; } for(j=1;j<=i;j++) { cout<<j; } for(k=i-1;k>=1;k--) { cout<<k; } cout<<endl; } } }; int main() { child obj; obj.show(); } |
WAP to implement Multiple 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 39 40 41 42 |
#include<iostream> using namespace std; class A { protected: int i,j,k,l; }; class B { protected: int n; }; class C:public A,public B { public: int show() { cout<<"num of rows:"; cin>>n; for(i=1;i<=n;i++) { for(l=1;l<=n-i;l++) { cout<<" "; } for(j=1;j<=i;j++) { cout<<j; } for(k=i-1;k>=1;k--) { cout<<k; } cout<<endl; } } }; int main() { C obj; obj.show(); } |