Site icon T4Tutorials.com

Constructor destructor Pyramid pattern of numbers program in C++ 

Pyramid pattern of numbers in C++ 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++

#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++

#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();
}

 

Exit mobile version