Site icon T4Tutorials.com

C++ Program using Virtual Base Class to to display Pascals triangle

Write a C++ program to display Pascal’s triangle using Virtual base Class in Object Oriented Programming(OOP).

#include<iostream>
using namespace std;
class A
{
		public:
		int r,s,c;
};
class B:virtual public A
{
	public:
int i,j;
};
class C:virtual public A
{
	public:
		int T4Tutorials_Print()
		{
			cout<<"Enter number of rows"<<endl;
			cin>>r;
		}
};
class D:public C,public B
{
	public:
		
		int T4Tutorials_Function()
		{
			C::T4Tutorials_Print();
			cout<<"You have entered number of rows= "<<r<<endl;

			for(i=0;i<r;i++)
				{
					for(s=1;s<=r-i;s++)
					{
						cout<<" ";
					}
					for(j=0;j<=i;j++)
					{
						if(j==0||i==0)
						{
							c=1;
						}
						else
						{
							c=c*(i-j+1)/j;
						}
						cout<<c<<" ";
					}
					cout<<endl;
				}
			}

};
int main()
{
	D obj;
	obj.T4Tutorials_Function();
}

Output

Program of pascal triangle in c++ using for loop virtual base class.
C++ program to print patterns of numbers using Virtual Base Class.

Exit mobile version