This area calculation program includes data members common to all shapes, such a
ID: 3892702 • Letter: T
Question
This area calculation program includes data members common to all shapes, such as a shape ID, a shape type, but does not include a unit of measure.
Unit of measure should be included.
//main.cpp
#include <iostream>//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" <<endl;
cout << " 0: Exit" << endl;
cout << " Enter Your Choice: ";
cin >> menuOption;
switch(menuOption)
{
case 1:
{
cout<< "Enter the radius : " ;
cin>> radius;
circle.setRadius(radius);
cout<< "Area of the circle is "<<circle.getArea();
break;
}
case 2:
{
cout<< "Enter length of one side of the square : "<<endl;
cin >> length;
square.setLength(length);
cout<<"Area of the square is "<<square.getArea();
break;
}
case 3:
{
cout<< "Enter length of one side of the rectangle : "<<endl;
cin >> length;
rectangle.setLength(length);
cout<< "Enter width of one side of the rectangle : "<<endl;
cin>> width;
rectangle.setWidth(width);
cout<<"Area of the rectangle is "<<rectangle.getArea();
break;
}
}
}
while(menuOption!=0);
cout<<" ________________ END PROGRAM ___________________" <<endl;
}
//circle.cpp
#include "Circle.h"
#include <iostream>
using namespace std;
Circle::Circle (double r)
{
radius = r;
}
double Circle::getRadius() {
return radius;
}
void Circle::setRadius (double r){
radius = r;
}
double Circle::getArea(){
return radius * radius * 3.14159;
}
//circle.h
#include "Circle.h"
#include <iostream>
using namespace std;
Circle::Circle (double r)
{
radius = r;
}
double Circle::getRadius() {
return radius;
}
void Circle::setRadius (double r){
radius = r;
}
double Circle::getArea(){
return radius * radius * 3.14159;
}
//square.cpp
#include "Square.h"
#include <iostream>
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
//rectangle.cpp
#include "Rectangle.h"
#include <iostream>
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 <iostream>
#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 <iostream>
Shape::Shape(){
area = 0;
}
double Shape::getArea(){
return area;
}
//shape.h
#ifndef SHAPE_H
#define SHAPE_H
#include <string>
class Shape{ // Base Class
public:
Shape();
double getArea();
private:
double area;
};
#endif // SHAPE_H_INCLUDED
Explanation / Answer
circle.cpp
#include "circle.h"
#include <iostream>
//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
main.cpp
#include <iostream>//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" <<endl;
cout << " 0: Exit" << endl;
cout << " Enter Your Choice: ";
cin >> 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 "<<circle.getArea() << " " << circle.getUnit() << endl;
break;
}
case 2:
{
square.setShapeID(1);
square.setShape_type("square");
square.setUnit("mm^2");
cout<< "Enter length of one side of the square : "<<endl;
cin >> length;
square.setLength(length);
cout << "Shape ID - " << square.getShapeID() << endl;
cout << "Shape type - " << square.getShape_type() << endl;
cout<< "Area of the circle is "<<square.getArea() << " " <<square.getUnit() << endl;
break;
}
case 3:
{
rectangle.setShapeID(1);
rectangle.setShape_type("rectangle");
rectangle.setUnit("mm^2");
cout<< "Enter length of one side of the rectangle : "<<endl;
cin >> length;
rectangle.setLength(length);
cout<< "Enter width of one side of the rectangle : "<<endl;
cin>> width;
rectangle.setWidth(width);
cout << "Shape ID - " << rectangle.getShapeID() << endl;
cout << "Shape type - " << rectangle.getShape_type() << endl;
cout<< "Area of the circle is "<<rectangle.getArea() << " " << rectangle.getUnit() << endl;
break;
}
}
}
while(menuOption!=0);
cout<<" ________________ END PROGRAM ___________________" <<endl;
}
rectangle.cpp
#include "rectangle.h"
#include <iostream>
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 <iostream>
#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 <iostream>
#include <string>
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 <string>
#include <iostream>
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 <iostream>
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