Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

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

Fazal Rehman Shamil Click Here to Know More
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325
Dear Professors and Resarchers!You are welome to Cite these tutorials in your research or slides etc. Please don't forget to mention the reference of website. Copy Paste of text is strcitly forbidden. Images can be reuse because images are protected with watermark.