Write a program in C++ to make such a pattern like a pyramid with a number that will repeat the number in the same row using a single inheritance in OOP.
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 pyramid { protected: int no; public: void input() { cout<<"enter the num of rows="; cin>>no; } }; class pyr:public pyramid { private: int n,r,sp,disp; public: void show() { n=no; for(r=1;r<=no;r++) { for(sp=1;sp<n;sp++) { cout<<" "; } n--; for(disp=1;disp<=r;disp++) { cout<<" "<<r; } cout<<endl; } } }; int main() { pyr myobj; myobj.input(); myobj.show(); } |