How to Print Diamond Shape with OOP Classes and objects

How to Print Diamond Shape with OOP Classes and objects

In this tutorial, we will code the program of printing the Diamond Shape with OOP Classes and objects.

Program to Print Diamond Shape with Classes and objects

Here, we have a code of Program to Print Diamond Shape with Classes and objects.

#include<iostream>
using namespace std;
class DiamodShape
{
	public :
		 int dimond()
		{
		int n, c, k, space=1;
    cout<<"Enter number of rows (for diamond dimension) : ";
    cin>>n;
    space=n-1;
    for (k=1; k<=n; k++)
    {
	for(c=1; c<=space; c++)
	{
		cout<<" ";
	}
	space--;
	for(c=1; c<=(2*k-1); c++)
	{
		cout<<"*";
	}
	cout<<"\n";
    }
    space=1;
    for(k=1; k<=(n-1); k++)
    {
	for(c=1; c<=space; c++)
	{
		cout<<" ";
	}
	space++;
	for(c=1 ; c<=(2*(n-k)-1); c++)
	{
		cout<<"*";
	}
	cout<<"\n";
		}
	}
};
int main()
{
	DiamodShape dimondDiamodShape;
	dimondDiamodShape.dimond();
}

Output

How to Print Diamond Shape with OOP Classes and objects C++
Figure: How to Print Diamond Shape with OOP Classes and objects C++

Program to Print Diamond Shape with Constructor

Here, we have a code of Program to Print Diamond Shape with the constructor.

#include<iostream>
using namespace std;
class shape
{
	public :
		shape()
		{
		int n, c, k, space=1;
    cout<<"Enter number of rows (for diamond dimension) : ";
    cin>>n;
    space=n-1;
    for (k=1; k<=n; k++)
    {
	for(c=1; c<=space; c++)
	{
		cout<<" ";
	}
	space--;
	for(c=1; c<=(2*k-1); c++)
	{
		cout<<"*";
	}
	cout<<"\n";
    }
    space=1;
    for(k=1; k<=(n-1); k++)
    {
	for(c=1; c<=space; c++)
	{
		cout<<" ";
	}
	space++;
	for(c=1 ; c<=(2*(n-k)-1); c++)
	{
		cout<<"*";
	}
	cout<<"\n";
		}
	}
};
int main()
{
	shape dimondshape;
	
}

Output

How to Print Diamond Shape with OOP Classes and objects C++
Figure: How to Print Diamond Shape with OOP Classes and objects C++

Program to Print Diamond Shape with Destructor

Here, we have a code of Program to Print the Diamond Shape with the destructor.

#include<iostream>
using namespace std;
class shape
{
	public :
		shape()
		{
		int n, c, k, space=1;
    cout<<"Enter number of rows (for diamond dimension) : ";
    cin>>n;
    space=n-1;
    for (k=1; k<=n; k++)
    {
	for(c=1; c<=space; c++)
	{
		cout<<" ";
	}
	space--;
	for(c=1; c<=(2*k-1); c++)
	{
		cout<<"*";
	}
	cout<<"\n";
    }
    space=1;
    for(k=1; k<=(n-1); k++)
    {
	for(c=1; c<=space; c++)
	{
		cout<<" ";
	}
	space++;
	for(c=1 ; c<=(2*(n-k)-1); c++)
	{
		cout<<"*";
	}
	cout<<"\n";
		}
	}
	~shape()
		{
		int n, c, k, space=1;
    cout<<"Enter number of rows (for diamond dimension) : ";
    cin>>n;
    space=n-1;
    for (k=1; k<=n; k++)
    {
	for(c=1; c<=space; c++)
	{
		cout<<" ";
	}
	space--;
	for(c=1; c<=(2*k-1); c++)
	{
		cout<<"*";
	}
	cout<<"\n";
    }
    space=1;
    for(k=1; k<=(n-1); k++)
    {
	for(c=1; c<=space; c++)
	{
		cout<<" ";
	}
	space++;
	for(c=1 ; c<=(2*(n-k)-1); c++)
	{
		cout<<"*";
	}
	cout<<"\n";
		}
	}
};
int main()
{
	shape dimondshape;
	
}

Output

How to Print Diamond Shape with OOP Classes and objects C++
Figure: How to Print Diamond Shape with OOP Classes and objects C++

Program to Print Diamond Shape with inheritance (Parent and Child Class)

Here, we have a code of Program to Print Diamond Shape with inheritance (Parent and Child Class).

#include<iostream>
using namespace std;
class shape
{
	protected:
			int n, c, k, space=1;
};
class dimond : public shape
{
	public :
		 int dimonds()
		{
		int n, c, k, space=1;
    cout<<"Enter number of rows (for diamond dimension) : ";
    cin>>n;
    space=n-1;
    for (k=1; k<=n; k++)
    {
	for(c=1; c<=space; c++)
	{
		cout<<" ";
	}
	space--;
	for(c=1; c<=(2*k-1); c++)
	{
		cout<<"*";
	}
	cout<<"\n";
    }
    space=1;
    for(k=1; k<=(n-1); k++)
    {
	for(c=1; c<=space; c++)
	{
		cout<<" ";
	}
	space++;
	for(c=1 ; c<=(2*(n-k)-1); c++)
	{
		cout<<"*";
	}
	cout<<"\n";
		}
	}
};
int main()
{
	dimond dimondshape;
	dimondshape.dimonds();
}

Output

How to Print Diamond Shape with OOP Classes and objects C++
Figure: How to Print Diamond Shape with OOP Classes and objects C++
Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.