Could someone possibly help me with this c++ program? For this program, you will
ID: 3832846 • Letter: C
Question
Could someone possibly help me with this c++ program?
For this program, you will implement the ShapeBuilder class. 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}
Explanation / Answer
#include <iostream>
#include <string>
class Diameter
{
public:
int size;
};
class Edge
{
public:
int size;
};
class Body
{
public:
std::string shape;
};
class Circle
{
public:
Diameter* diameter[4];
Edge* edge;
Body* body;
void specifications()
{
std::cout << "body:" << body->shape << std::endl;
std::cout << "edge:" << size->length<< std::endl;
std::cout << "circle :" <<diameter[0]->size << "'" << std::endl;
}
};
class Builder
{
public:
virtual Diameter* getDiameter() = 0;
virtual Edge* getEdge() = 0;
virtual Body* getBody() = 0;
};
class Director
{
Builder* builder;
public:
void setBuilder(Builder* newBuilder)
{
builder = newBuilder;
}
Circle* getCircle()
{
Circle* circle= new Circle();
circle->body = builder->getBody();
circle->engine = builder->getEdge();
circle->diameter[0] = builder->getDiameter();
circle->diameter[1] = builder->getDiameter();
circle->diameter[2] = builder->getDiameter();
circle->diameter[3] = builder->getDiameter();
return circle;
}
};
class TriangleBuilder : public Builder
{
public:
Diameter* getDiameter()
{
Diameter* diameter= new Diameter();
diameter->size = 22;
return diameter;
}
Edge* getEdge()
{
Edge* edge= new Edge();
edge->size = 60;
return edge;
}
Body* getBody()
{
Body* body = new Body();
body->shape = "SUV";
}
};
class SquareBuilder : public Builder
{
public:
Diameter* getDiameter()
{
Diameter* diameter = new Diameter();
diameter->size = 16;
return diameter;
}
Edge* getEdge()
{
Edge* edge= new Edge();
edge->size = 75;
return edge;
}
Body* getBody()
{
Body* body = new Body();
body->shape = "hatchback";
}
};
int main()
{
Circle* circle;
Director director;
TriangleBuilder triangleBuilder;
SquareBuilder squareBuilder;
std::cout << "Triangle"<< std::endl;
director.setBuilder(&triangleBuilder);
circle= director.getCircle();
circle>specifications();
std::cout << std::endl;
std::cout << "Square<< std::endl;
director.setBuilder(&squareBuilder);
circle= director.getCircle();
circle>specifications();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.