Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This program was created to calculate the area that includes data members common to all shapes (such as a shape ID, a shape type, and

This program was created to calculate the area that includes data members common to all shapes (such as a shape ID, a shape type, and a unit of measure). The Shape base class has a virtual getArea() member function. I am having a problem with the header and implementation files due to a problem defining the Circle, Triangle, and Rectangle classes so that they inherit from the Shape base class. The finished program does not ask the user to enter the relevant information for each shape due to "Undefined reference errors" so it cannot print the area and other information for each shape.

Here is the code:

//main.cpp #include "Circle.h" #include "Shape.h" #include "Rectangle.h" #include "Triangle.h" #include "main.h"

using namespace std; int main(int argc, char**argv) { // Prints welcome message...

double length = 0.0; double height = 0.0; double base = 0.0; double width = 0.0; double radius = 0.0; string unitOfmeasure = ""; cout << "Welcome ..." << endl; for (int i = 0; i<3; i++) { try {

unitOfmeasure = ""; if(i == 0) { cout << "Please enter the unit of measurement for rectangle: "<< endl; cin >> unitOfmeasure; cout << "Please enter the width of the rectangle: "<< endl; cin >> width; cout << "Please enter the length of the rectangle"<< endl; cin >> length; if((width|| length) == 0) { throw("This integer field can't be zero");

} else { Shape * rect = new Rectangle(width,length, unitOfmeasure); std::cout <<"ID = " << std::fixed << rect->getId() <<", Type = " << std::fixed << rect->getShapeType() <<", Area = " << std::fixed << rect->getArea() << std::fixed << rect->getunitOfmeasure() <> unitOfmeasure;

cout << "Please enter the base of the Triangle: "<< endl; cin >> base; cout << "Please enter the height of the Triangle"<< endl; cin >> height; if(height == 0) { throw("This integer field can't be zero"); } else {

Shape * tri = new Triangle(base, height, unitOfmeasure); std::cout <<"ID = " << std::fixed << tri->getId() <<", Type = " << std::fixed << tri->getShapeType() <<", Area = " << std::fixed << tri->getArea() << std::fixed << tri->getunitOfmeasure() <> unitOfmeasure; cout << "Please enter the radius of the circle: "<< endl; cin >> radius; if( radius> 0) { Shape * cir=new Circle(radius, unitOfmeasure); std::cout <<"ID = " << std::fixed << cir->getId() <<", Type = " << std::fixed << cir->getShapeType() <<", Area = " << std::fixed << cir->getArea() << std::fixed << cir->getunitOfmeasure() <

} catch (string e) { Shape::setError(e);

} } return 0; }

//main.h #include #include #include "Rectangle.h" #include "Triangle.h" #include "Circle.h"

#ifndef MAIN_H #define MAIN_H

#ifdef __cplusplus extern "C" { #endif

#ifdef __cplusplus } #endif

#endif /* MAIN_H */

//circle.cpp #include //toget M_PI #include "Circle.h" #include

using namespace std; Circle::Circle(float diameter, string unitOfmeasure) :Shape(CIRCLE, unitOfmeasure){ this->diameter=diameter; } float Circle::getArea(){ float radius=diameter/2; return M_PI *(radius*radius); } Circle::~Circle() { }

//circle.h #ifndef CIRCLE_H #define CIRCLE_H #include "Shape.h" #include

using namespace std;

class Circle :public Shape{ public:; Circle(float diameter, string unitOfmeasure); float getArea(); virtual ~Circle(); private: float diameter; };

#endif /* CIRCLE_H */

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

using namespace std;

Rectangle::Rectangle(float w, float h, string unitOfmeasure):Shape(RECTANGLE, unitOfmeasure) { width=w; height=h; } float Rectangle::getArea(){ return height*width; } Rectangle::~Rectangle() { }

//rectangle.h #ifndef RECTANGLE_H #define RECTANGLE_H #include "Shape.h"

using namespace std;

class Rectangle : public Shape { public: Rectangle(float width, float height, string unitOfmeasure); float getArea(); virtual ~Rectangle(); private: float height; float width; string unitOfmeasure; };

#endif /* RECTANGLE_H */

//shape.cpp #include "Shape.h" const string Shape::RECTANGLE = "RECTANGLE"; const string Shape::TRIANGLE = "TRIANGLE"; const string Shape::CIRCLE ="CIRCLE";

static int sNextId = 1234; Shape::Shape(string shapeType, string unitOfMeasure) { this->shapeType=shapeType; this->unitOfMeasure = unitOfMeasure; id=++sNextId; } int Shape::getId(){ return id; } string Shape::getShapeType(){ return shapeType; } string Shape::getunitOfmeasure(){ return unitOfMeasure; } void Shape::setError(string errorString){ cout << errorString << "Error:" << endl; }

Shape::~Shape() { }

//shape.h #include #ifndef SHAPE_H #define SHAPE_H #include

using namespace std; class Shape {

public: static const string RECTANGLE; static const string TRIANGLE; static const string CIRCLE;

Shape(string shapeType, string unitOfMeasure); int getId(); string getShapeType(); string getunitOfmeasure(); static void setError(string errorString);

virtual float getArea()=0; virtual ~Shape(); private: int id; string shapeType; string unitOfMeasure; };

#endif /* SHAPE_H */

//triangle.cpp #include "Triangle.h" #include

using namespace std; Triangle::Triangle(float base,float height, string unitOfmeasure) :Shape(TRIANGLE, unitOfmeasure) { this->base=base; this->height=height; } float Triangle::getArea(){ return height*base/2; } Triangle::~Triangle() {

//triangle.h #ifndef TRIANGLE_H #define TRIANGLE_H #include "Shape.h"

class Triangle :public Shape{ public: Triangle(float base,float height, string unitOfmeasure); float getArea(); virtual ~Triangle(); private: float base; float height; string unitOfmeasure; };

#endif /* TRIANGLE_H */

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

Database Design Application And Administration

Authors: Michael Mannino, Michael V. Mannino

2nd Edition

0072880678, 9780072880670

More Books

Students also viewed these Databases questions

Question

14-18 Compare the two major types of planning and control tools.

Answered: 1 week ago