Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Referring to struct Square that you wrote for Shapes v . 2 in the last module, rewrite the struct definition and supporting function prototype so

Referring to struct Square that you wrote for Shapes v.2 in the last module, rewrite the struct definition and supporting function prototype so that the supporting function becomes a member function instead.
Just write the struct definition...
#include
#include
#include
#include
using namespace std;
// Structs for shapes
struct Square {
double side;
};
struct Rectangle {
double length;
double width;
};
struct Circle {
double radius;
};
struct Triangle {
double base;
double height;
};
struct Cube {
double side;
};
struct Box {
double length;
double width;
double height;
};
struct Cylinder {
double radius;
double height;
};
struct Prism {
double baseSide;
double height;
};
// Function to output each shape to console
void outputSquare(ostream &out, const Square &square){
out << "Square: Side ="<< square.side << endl;
}
void outputRectangle(ostream &out, const Rectangle &rectangle){
out << "Rectangle: Length ="<< rectangle.length
<<", Width ="<< rectangle.width << endl;
}
void outputCircle(ostream &out, const Circle &circle){
out << "Circle: Radius ="<< circle.radius << endl;
}
void outputTriangle(ostream &out, const Triangle &triangle){
out << "Triangle: Base ="<< triangle.base
<<", Height ="<< triangle.height << endl;
}
void outputCube(ostream &out, const Cube &cube){
out << "Cube: Side ="<< cube.side << endl;
}
void outputBox(ostream &out, const Box &box){
out << "Box: Length ="<< box.length <<", Width ="<< box.width
<<", Height ="<< box.height << endl;
}
void outputCylinder(ostream &out, const Cylinder &cylinder){
out << "Cylinder: Radius ="<< cylinder.radius
<<", Height ="<< cylinder.height << endl;
}
void outputPrism(ostream &out, const Prism &prism){
out << "Prism: Base Side ="<< prism.baseSide
<<", Height ="<< prism.height << endl;
}
int main(){
vector shapes; // Using vector to store shapes
ifstream fin("shapes.txt"); // Input file
ofstream fout("output.txt"); // Output file
char shapeType;
while (fin >> shapeType){
void *shapePtr = nullptr;
switch (shapeType){
case 'S': {
Square *square = new Square;
fin >> square->side;
shapePtr = square;
break;
}
case 'R': {
Rectangle *rectangle = new Rectangle;
fin >> rectangle->length >> rectangle->width;
shapePtr = rectangle;
break;
}
case 'C': {
Circle *circle = new Circle;
fin >> circle->radius;
shapePtr = circle;
break;
}
case 'T': {
Triangle *triangle = new Triangle;
fin >> triangle->base >> triangle->height;
shapePtr = triangle;
break;
}
case 'B': {
Box *box = new Box;
fin >> box->length >> box->width >> box->height;
shapePtr = box;
break;
}
case 'Y': {
Cylinder *cylinder = new Cylinder;
fin >> cylinder->radius >> cylinder->height;
shapePtr = cylinder;
break;
}
case 'P': {
Prism *prism = new Prism;
fin >> prism->baseSide >> prism->height;
shapePtr = prism;
break;
}
default:
break;
}
shapes.push_back(shapePtr);
}
// Output to console
for (const auto &shape : shapes){
switch (shapeType){
case 'S':
outputSquare(cout,*static_cast(shape));
break;
case 'R':
outputRectangle(cout,*static_cast(shape));
break;
case 'C':
outputCircle(cout,*static_cast(shape));
break;
case 'T':
outputTriangle(cout,*static_cast(shape));
break;
case 'B':
outputBox(cout,*static_cast(shape));
break;
case 'Y':
outputCylinder(cout,*static_cast(shape));
break;
case 'P':
outputPrism(cout,*static_cast(shape));
break;
default:
break;
}
}
// Output to file
for (const auto &shape : shapes){
switch (shapeType){
case 'S':
outputSquare(fout,*static_cast(shape));
break;
case 'R':
outputRectangle(fout,*static_cast(shape));
break;
case 'C':
outputCircle(fout,*static_cast(shape));
break;
case 'T':
outputTriangle(fout,*static_cast(shape));
break;
case 'B':
outputBox(fout,*static_cast(shape));
break;
case 'Y':
outputCylinder(fout,*static_cast(shape));
break;
case 'P':
outputPrism(fout,*static_cast(shape));
break;
default:
break;
}
}
// Deallocate memory
for (auto &shape : shapes){
delete shape;
}
fout.close(); // Close output file
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions