Write a program in C++ to make such a pattern like a pyramid with a number which will repeat the number in the same row using constructor overloading and destructor.
Constructor destructor Pyramid pattern of numbers program in C++
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 pyramid { private: int r,T4Tutorials,SHOW,n,no; public: pyramid() //constructor { no=5; n=no; } void sol() { for(r=1;r<=no;r++) { for(T4Tutorials=1;T4Tutorials<=n;T4Tutorials++) { cout<<" "; } n--; for(SHOW=1;SHOW<=r;SHOW++) { cout<<" "<<r; } cout<<endl; } } ~pyramid() //destructor { cout<<endl<<"object destroyed"; } }; int main() { pyramid ob; ob.sol(); } |
Constructor overloading Pyramid pattern of numbers program in C++
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 |
#include<iostream> using namespace std; class pyramid { int r,T4Tutorials,show,n; public: pyramid() { n=0; } pyramid(int s) { n=s; } void sol() { for(r=1;r<=n;r++) { for(T4Tutorials=1;T4Tutorials<=n;T4Tutorials++) { cout<<" "; } n--; for(show=1;show<=r;show++) { cout<<" "<<r; } cout<<endl; } } }; int main() { pyramid obj1(8); pyramid obj2(7); cout<<"first pyramid"<<endl; obj1.sol(); cout<<"second pyramid"<<endl; obj2.sol(); } |