Create a new class called Diamond that will print diamond shapes on the screen b
ID: 3531201 • Letter: C
Question
Create a new class called Diamond that will print diamond shapes on the screen based on its "size", which is the number of "stars" along a side. i. e. If the diamond size is 2. it should appear like: Your new Diamond class should have a private member variable that stores the integer size of the diamond. Create two constructors in the Diamond class: One constructor should have no parameters, but should be able to be used to create a diamond object of size 1. Another constructor should have a single integer parameter which should be used to set the appropriate member variable. NOTE: Neither constructor should use cin or cout because constructors should be used to simply create a new Diamond object, not to print it or do I/O. Prompt the user to input a positive integer that refers to the size of a diamond your program will draw on the screen. If this number is not positive or zero, thank the user and end the program. Otherwise, your program should declare a variable of type Diamond thereby calling a constructor to create this object of this size. Use your public member function to print the hollow diamond of that size.Explanation / Answer
#include <iostream>
using namespace std;
class Diamond
{
public:
Diamond()
{
size=1;
}
Diamond(int f):size(f)
{
}
~Diamond()
{
}
void display()
{
int k=2*size-1;
int b=0;
for (int i = 0; i < k; i++)
{
if(i==size){b=b-2;}
for (int j = 0; j < k; j++)
{
if((j==(size+b-1))||(j==(size-b-1)))
cout<<"*";
else
{
cout<<" ";
}
}
cout<<endl;
if(i<size)b++;
else b--;
}
}
private:
int size;
};
int main(){
int n;
cin>>n;
if((n>0))
cout<<"thanks"<<endl;
Diamond k(n);
k.display();
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.