Site icon T4Tutorials.com

Multilevel inheritance C++ program pattern like a pyramid repeat

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 Multilevel inheritance in OOP.

Print pyramid pattern using do while loop c++

#include<iostream>
using namespace std;
class A
{
	protected:
		int no;
		public:
			void get(int);
};
void A::get(int)
{
	cout<<"enter a number of rows=";
	cin>>no;
}
class B:public A
{
	protected:
		int n;
		public:
			void show(int);
};
void B::show(int)
{
	n=no;
}
class C:public B
{
	protected:
		int r,sp,disp;
		public:
			void display(int);
};
void C::display(int)
{
	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()
{
	C myobj;
	myobj.get(3);
	myobj.show(3);
	myobj.display(6);
}

 

Exit mobile version