C++ For this program, you will implement the ShapeBuilder class in C++. The Shap
ID: 3832611 • Letter: C
Question
C++
For this program, you will implement the ShapeBuilder class in C++.
The ShapeBuilder will ask the user what kind of shape he/she would like to build, and then ask for the appropriate dimensions of the shape.
For example, a circle will only need a diameter and a rectangle will need a height and width.
Upon definition, the ShapeBuilder will display the shape to the screen. If the user likes what he/she sees, then they have the option to save the shape to a file (“shapes.txt”). The saving operation will be performed by the ShapeSaver class, which has one saveShape function, which accepts only a Shape* object into the function.
The ShapeBuilder then asks the user if they would like to build/save another shape or to exit the program.
Implementation Details
You must implement a ShapeBuilder class, a ShapeSaver class, a Shape base class, and the following classes derived from Shape: Circle, EquilateralTriangle, and Rectangle.
A circle is defined by its diameter. Valid sizes are 1 to 80.
An equilateral triangle is defined by its edge size. Valid sizes are 1 to 80.
A rectangle is defined by its height and width. Valid sizes are 1 to 80 each.
Each class must have a separate .H and .cpp file.
The Shape class must contain:
A dynamically allocated 2-D character array
The pure virtual function “build()”, which is implemented by the derived classes
The build function will ask the user for shape dimensions, allocate the appropriate memory, and assign the appropriate values into the 2-D character array {use ‘x’ for the shape; all other characters will be spaces}
You should try to put all common information into the base Shape class. Avoid repeating member variables throughout each derived class.
All dynamically allocated memory must be freed at the appropriate moment.
Your main function will be very simple:
int main(){
ShapeBuilder sb;
sb.run();
return 0;
}
Hints
Try starting with the rectangle case. That is the simplest. Move from there to implement triangle and then circle.
Note that the indices for the data array are integers, but the triangle and circle shape representations will necessarily have cases where the perimeter of the shape lies “between” the indices. This means circles with an even numbered diameter will appear off-center. All triangles will look like a wizard’s hat at the peak. This is ok.
At the end of this document, there are sample representations for circle and triangle.
Your implementation need not follow these representations exactly. However, your shapes must be able to be interpreted “as a circle” or “as an equilateral triangle” or “as a rectangle”. That is, you should be close to those representations, if not exact.
Example Program Output
What kind of shape would you like to build?
Enter c for circle.
Enter r for rectangle.
Enter t for isosceles triangle.
Selection: c
You selected circle.
What would you like your circle's diameter to be: 21
Here is your shape:
xxxxxxxxx
xxxxxxxxxxxxx
xxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxx
xxxxxxxxxxxxx
xxxxxxxxx
Would you like to save your shape (y for yes): y
Shape saved.
Would you like to keep going (y for yes): y
What kind of shape would you like to build?
Enter c for circle.
Enter r for rectangle.
Enter t for isosceles triangle.
Selection: c
You selected circle.
What would you like your circle's diameter to be: 8
Here is your shape:
xxxxx
xxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxx
Would you like to save your shape (y for yes): y
Shape saved.
Would you like to keep going (y for yes): y
What kind of shape would you like to build?
Enter c for circle.
Enter r for rectangle.
Enter t for isosceles triangle.
Selection: r
You selected rectangle.
What would you like your rectangle width to be: 8
What would you like your rectangle height to be: 11
Here is your shape:
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
Would you like to save your shape (y for yes): y
Shape saved.
Would you like to keep going (y for yes): y
What kind of shape would you like to build?
Enter c for circle.
Enter r for rectangle.
Enter t for isosceles triangle.
Selection: t
You selected isosceles triangle.
What would you like your triangle's edge length to be: 6
Here is your shape:
x
xx
xxx
xxxx
xxxxx
xxxxxx
Would you like to save your shape (y for yes): y
Shape saved.
Would you like to keep going (y for yes): n
The contents of shapes.txt are as follows:
xxxxxxxxx
xxxxxxxxxxxxx
xxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxx
xxxxxxxxxxxxx
xxxxxxxxx
xxxxx
xxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
xxxxxxxx
x
xx
xxx
xxxx
xxxxx
xxxxxx
The first seven representations for both circle and triangle:
Circle:
x
xx
xx
xxx
xxx
xxx
xxxx
xxxx
xxxx
xxxx
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx
xxxxxx
xxxxxx
xxxxxx
xxxxxx
xxxxxx
xxxxx
xxxxxxx
xxxxxxx
xxxxxxx
xxxxxxx
xxxxxxx
xxxxx
Triangle:
x
x
xx
x
xx
xxx
x
xx
xxx
xxxx
x
xx
xxx
xxxx
xxxxx
x
xx
xxx
xxxx
xxxxx
xxxxxx
x
xx
xxx
xxxx
xxxxx
xxxxxx
xxxxxxx
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
char ch,che;
cout<<" What Kind of Shapes would you like to build:";
cout<<" Enter c for Circle";
cout<<" Enter r for Rectangle";
cout<<" Enter t for Isoceles Triangle";
cout<<" Selection";
cin>>ch;
switch(ch)
{
case 'c':
int diameter;
cout<<endl;
cout<<"You Selected Circle"<<endl;
cout<<"Enter the diameter:";
cin>>diameter;
cout<<endl;
for(int i=0;i<2*diameter;i++)
{
for(int j=0;j<2*diameter;j++)
{
if(i*i+j*j==diameter*diameter)
{
cout<<"*";
}
else
{
cout<<"*";
}
}
cout<<endl;
}
break;
case 'r':
int width,height;
cout<<endl;
cout<<"You Selected Rectangle"<<endl;
cout<<"Enter the width:";
cin>>width;
cout<<"Enter the Height:";
cin>>height;
cout<<endl;
for(int i=1; i<=height; i++)
{
for (int j=1;j<=width;j++)
{
cout <<'*';
}
cout<<endl;
}
break;
case 't':
int length;
cout<<" You Selected Triangle" <<endl;
cout<<" Enter the Length of the Triangle:";
cin>>length;
cout<<endl;
for(int i=0;i<length;i++)
{
for(int j=0;j<i;++j)
{
cout<<"*";
}
cout<<endl;
}
break;
}
cout<<" Would you like to save your shape(Y for Yes):";
cin>>che;
if(che=='Y' || che=='y')
{
cout<<" Shape Saved";
}
else
{
cout<<" Program Exits";
}
return 0;
}
OUTPUT
What Kind of Shapes would you like to build:
Enter c for Circle
Enter r for Rectangle
Enter t for Isoceles Triangle
Selection
You Selected Rectangle
Enter the width:Enter the Height:
********
********
********
********
********
********
********
********
********
********
********
Would you like to save your shape(Y for Yes):
Shape Saved
What Kind of Shapes would you like to build:
Enter c for Circle
Enter r for Rectangle
Enter t for Isoceles Triangle
Selection
You Selected Triangle
Enter the Length of the Triangle:
*
**
***
****
*****
Would you like to save your shape(Y for Yes):
Program Exits
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.