Question: This C++ area calculation program includes data members common to all shapes, such as a shape ID, a shape type, and unit of measure. Correction

This C++ area calculation program includes data members common to all shapes, such as a shape ID, a shape type, and unit of measure. Correction needed: It should be updated to allow user to enter unit of measure rather than being hard coded.

Here's the C++ code.

//main.cpp #include //Need this header file to support the C++ I/O system #include "circle.cpp"// including the header and implementation files #include "square.cpp" #include "rectangle.cpp" #include "shape.cpp"

using namespace std;//Telling the compiler to use name space where C++ library is declared

int main( ) { double radius = 0; double length = 0; double width = 0; Square square; Circle circle; Rectangle rectangle;

int menuOption;//declare menu selection variable

do { // Initialize while loop cout << endl << "_______________Calculate Area______________" << endl; cout << "Select an Object" << endl; cout << " 1: Circle" << endl; cout << " 2: Square" << endl; cout << " 3: Rectangle" <> menuOption;

switch(menuOption) {

case 1: {

cout<< "Enter the radius : " ; cin>> radius; circle.setRadius(radius); circle.setShapeID(1); circle.setShape_type("circle"); circle.setUnit("mm^2"); cout << "Shape ID - " << circle.getShapeID() << endl; cout << "Shape type - " << circle.getShape_type() << endl; cout<< "Area of the circle is "<> length; square.setLength(length); cout << "Shape ID - " << square.getShapeID() << endl; cout << "Shape type - " << square.getShape_type() << endl; cout<< "Area of the circle is "<> length; rectangle.setLength(length); cout<< "Enter width of one side of the rectangle : "<> width; rectangle.setWidth(width); cout << "Shape ID - " << rectangle.getShapeID() << endl; cout << "Shape type - " << rectangle.getShape_type() << endl; cout<< "Area of the circle is "<

} while(menuOption!=0);

cout<<" ________________ END PROGRAM ___________________" <

//circle.cpp

#include "circle.h"

#include

using namespace std;

Circle::Circle (double r=0)

{

radius = r;

}

double Circle::getRadius() {

return radius;

}

void Circle::setRadius (double r){

radius = r;

}

double Circle::getArea(){

return radius * radius * 3.14159;

}

//circle.h #ifndef CIRCLE_H #define CIRCLE_H #include "shape.h" using namespace std;

class Circle : public Shape {

public: Circle (double r); double getRadius(); void setRadius (double r); double getArea(); private:

double radius;

};

#endif // SQUARE_H

//rectangle.cpp #include "rectangle.h" #include

using namespace std;

Rectangle::Rectangle(double len, double wid)

{

length = len;

width = wid;

}

double Rectangle::getLength() {

return length;

}

void Rectangle::setLength(double len){

length = len;

}

double Rectangle::getWidth() {

return width;

}

void Rectangle::setWidth(double wid){

width= wid;

}

double Rectangle::getArea(){

return length * width;

}

//rectangle.h #ifndef RECTANGLE_H_INCLUDED #define RECTANGLE_H_INCLUDED #include #include "shape.h"

class Rectangle: public Shape

{

public:

Rectangle (double length =0, double width =0);

double getLength();

double getWidth();

void setLength(double length);

void setWidth(double width);

virtual double getArea ();

private:

double length;

double width;

};

#endif // RECTANGLE_H_INCLUDED

//shape.cpp

#include "shape.h"

#include

#include

using namespace std;

Shape::Shape(){

area = 0;

}

double Shape::getArea(){

return area;

}

void Shape::setShapeID(int Shape_ID)

{

ShapeID = Shape_ID;

}

int Shape::getShapeID(void)

{

return ShapeID;

}

void Shape::setShape_type(string Shape_type)

{

ShapeType = Shape_type;

}

string Shape::getShape_type(void)

{

return ShapeType;

}

void Shape::setUnit(string unit)

{

unit = unitM;

}

string Shape::getUnit(void)

{

return unitM;

}

//shape.h #ifndef SHAPE_H #define SHAPE_H #include #include using namespace std; class Shape{ // Base Class

public:

Shape();

double getArea(); void setShapeID(int Shape_ID); void setShape_type(string Shape_type); void setUnit(string unit); int getShapeID(void); string getShape_type(void); string getUnit(void); int ShapeID; string ShapeType; string unitM;

private:

double area;

};

#endif // SHAPE_H_INCLUDED

//square.cpp #include "square.h" #include using namespace std;

Square::Square(double len) { length = len; }

double Square::getLength() { return length; }

void Square::setLength(double len){ length = len; }

double Square::getArea(){ return length * length; }

square.h #ifndef SQUARE_H #define SQUARE_H #include "shape.h" using namespace std; class Square : public Shape { public: Square (double length =0); double getLength(); void setLength(double length); virtual double getArea (); private: double length; }; #endif // SQUARE_H

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!