Write a new version of the area calculation program from Assessment 3 that makes
ID: 3891258 • Letter: W
Question
Write a new version of the area calculation program from Assessment 3 that makes use of inheritance in C++.
Add a new Shape base class to the area calculation program 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 should also include a virtual getArea() member function.
Revise the C++ code for Circle and Square classes so that they inherit from the Shape base class.
Add a third shape to the program that also inherits from the Shape class. The finished program should ask the user to enter the relevant information for each shape and then print the area and other information for each shape.
Organize the code correctly into header (.h) and implementation (.cpp) files. Your code should include meaningful comments and be correctly formatted.
Here's the code:
main.cpp
#include <iostream>//Need this header file to support the C++ I/O system
#include "circle.hpp"// including the header and implementation files
#include "square.hpp"
#include "circle.cpp"
#include "square.cpp"
using namespace std;//Telling the compiler to use name space where C++ library is declared
int main()
{
double radius,length;//declare variables for circumference of the circle and length of square
Circle circle;//default values used to display radius value
circle.Display();//radius value for the circle
Circle circ;
cout << "Enter the radius value :" << endl;//user to enter value
cin >> radius;
circ.setRadius(radius);
circ.Display();
Square square;//display the square
square.Display();
Square squ;//get value for length
cout << "Enter the length value :" << endl;//user to enter value
cin >> length;
squ.setLength(length);//base class
squ.Display();
return 0;
}
circle.hpp
#ifndef CIRCLE_H
#define CIRCLE_H
// circle structure class
struct Circle
{
//declare circle access specifier
public: Circle();
Circle(double rad);//parameterized constructor
//set radius value
void setRadius(double r);
//retrieve radius value
double getRadius();
//calculate area
double Area();
//display data
void Display();
//declare value for the radius access specifiers
private: double Radius;
//access specifier
};
#endif
circle.cpp
#include <iostream>//need this header file to support the C++ I/O system
#include "circle.hpp"// include the header file
using namespace std;//telling the compiler to use names pace where C++ library is declared
const double PI = 3.14159;//declare, define and initialize global values
Circle::Circle()// declare circle method
{
Radius = 0;//value of the radius initialized
}
Circle::Circle(double rad)
{//Parameterized constructor
Radius=rad;
}
void Circle::setRadius(double r)
{
Radius = r <= 0 ? 1 : r;//verify the input value
}
double Circle::getRadius()
{
return Radius;//retrieve and store value
}
double Circle::Area()
{
return Radius * Radius * PI;//return value of the area
}
void Circle::Display()
{
cout << " Circle Properties";// display the data
cout << " Radius = " << getRadius();
cout << " Area = " << Area() << " ";
}
square.hpp
#ifndef SQUARE_H
#define SQUARE_H
// square structure class
struct Square
{ //declare square access specifier
public : Square();
Square(double len);//parameterized constructor
//set length
void setLength(double l);
//retrieve length
double getLength();
//retrieve area
double Area();
//display data
void Display();
// length value declaration access specifier
//access specifiers
private: double Length;
};
#endif
square.cpp
#include <iostream>//need this header file to support the C++ I/O system
#include "square.hpp"// including the header file
using namespace std;//telling the compiler to use name space where C++ library is declared
Square::Square()//declare method used for square shape
{
Length = 0;//initialize length value
}
Square::Square(double len)
{//Parameterized constructor
Length=len;
}
void Square::setLength(double l)
{
Length = l <= 0 ? 1 : l;//verify data
}
double Square::getLength()
{
return Length;//returning length
}
double Square::Area()
{
return Length*Length;// returning the area
}
void Square::Display(){
cout << " Square Properties";// display output
cout << " Length = " << getLength();
cout << " Area = " << Area() << " ";
}
Explanation / Answer
class shape
{
protected:
double base;
double height;
int side,radius;
public:
shape(double b1,double h1 , int s1, int r1)
{
base=b1;
height=h1;
side=s1;
radius=r1;
}
virtual void disp_area()=0;
};
class circle :public shape
{
public:
circle(int r1, double h1):shape(0,h1,0,h1)
{
}
void disp_area();
};
class triangle :public shape
{
public:
triangle(double b1, double h1):shape(b1,h1,0,0)
{
}
void disp_area();
};
class square :public shape
{
public:
square(int s1):shape(0,0,s1,0)
{
}
void disp_area();
};
class rectangle:public shape
{
public:
rectangle(double b1,double h1):shape(b1,h1,0,0)
{
}
void disp_area();
};
void triangle::disp_area()
{
double a1;
a1=(base*height)*1/2;
cout <<" THE AREA OF TRIANGLE IS :"<<a1;
getch();
}
void rectangle::disp_area()
{
double a1;
a1=base*height;
cout<<" THE AREA OF RECTANGLE IS :"<<a1;
getch();
}
void circle::disp_area()
{
double a1;
a1=3.14*r*r*h;
cout<<" THE AREA OF circle IS :"<<a1;
getch();
}
void square::disp_area()
{
double a1;
a1=s1*s1;
cout<<" THE AREA OF square IS :"<<a1;
getch();
}
main()
{
double b1,h1;
int r1,s1;
clrscr();
cout<<" PLEASE ENTER THE circle DETAILS ";
cout<<" radius = ";
cin>>r1;
cout<<" height = ";
cin>>h1;
circle c1(b1,h1);
cout<<" PLEASE ENTER THE square DETAILS ";
cout<<" side = ";
cin>>s1;
square sq(s1);
cout<<" PLEASE ENTER THE TRIANGLE DETAILS ";
cout<<"BASE = ";
cin>>b1;
cout<<" HYPOTENUSE = ";
cin>>h1;
triangle t1(b1,h1);
cout<<" PLEASE ENTER THE RECTANGLE DETAILS ";
cout<<" LENGTH = ";
cin>>b1;
cout<<" BREADTH = ";
cin>>h1;
rectangle r2(b1,h1);
shape *list[4];
list[0]=&c1;
list[1]=&sq1;
list[2]=&t1;
list[3]=&r1;
while(1)
{
clrscr();
cout<<" ------------MENU------------";
cout<<" . circle area";
cout<<" , square area";
cout<<" 2. triangle AREA";
cout<<" 3. rectangle area";
cout<<" 4. exit";
cout<<" ENTER YOUR CHOICE :";
int ans;
cin>>ans;
if(ans ==1)
{
list[0]->disp_area();
}
elseif(ans==2)
{
list[1]->disp_area();
}
elseif(ans==3)
{
list[2]->disp_area();
}
elseif(ans==4)
{
list[3]->disp_area();
}
elseif(ans==5)
exit(1);
else
{
cout<<" INVALID CHOICE";
getch();
continue;
}
}
}
class shape
{
protected:
double base;
double height;
int side,radius;
public:
shape(double b1,double h1 , int s1, int r1)
{
base=b1;
height=h1;
side=s1;
radius=r1;
}
virtual void disp_area()=0;
};
class circle :public shape
{
public:
circle(int r1, double h1):shape(0,h1,0,h1)
{
}
void disp_area();
};
class triangle :public shape
{
public:
triangle(double b1, double h1):shape(b1,h1,0,0)
{
}
void disp_area();
};
class square :public shape
{
public:
square(int s1):shape(0,0,s1,0)
{
}
void disp_area();
};
class rectangle:public shape
{
public:
rectangle(double b1,double h1):shape(b1,h1,0,0)
{
}
void disp_area();
};
void triangle::disp_area()
{
double a1;
a1=(base*height)*1/2;
cout <<" THE AREA OF TRIANGLE IS :"<<a1;
getch();
}
void rectangle::disp_area()
{
double a1;
a1=base*height;
cout<<" THE AREA OF RECTANGLE IS :"<<a1;
getch();
}
void circle::disp_area()
{
double a1;
a1=3.14*r*r*h;
cout<<" THE AREA OF circle IS :"<<a1;
getch();
}
void square::disp_area()
{
double a1;
a1=s1*s1;
cout<<" THE AREA OF square IS :"<<a1;
getch();
}
main()
{
double b1,h1;
int r1,s1;
clrscr();
cout<<" PLEASE ENTER THE circle DETAILS ";
cout<<" radius = ";
cin>>r1;
cout<<" height = ";
cin>>h1;
circle c1(b1,h1);
cout<<" PLEASE ENTER THE square DETAILS ";
cout<<" side = ";
cin>>s1;
square sq(s1);
cout<<" PLEASE ENTER THE TRIANGLE DETAILS ";
cout<<"BASE = ";
cin>>b1;
cout<<" HYPOTENUSE = ";
cin>>h1;
triangle t1(b1,h1);
cout<<" PLEASE ENTER THE RECTANGLE DETAILS ";
cout<<" LENGTH = ";
cin>>b1;
cout<<" BREADTH = ";
cin>>h1;
rectangle r2(b1,h1);
shape *list[4];
list[0]=&c1;
list[1]=&sq1;
list[2]=&t1;
list[3]=&r1;
while(1)
{
clrscr();
cout<<" ------------MENU------------";
cout<<" . circle area";
cout<<" , square area";
cout<<" 2. triangle AREA";
cout<<" 3. rectangle area";
cout<<" 4. exit";
cout<<" ENTER YOUR CHOICE :";
int ans;
cin>>ans;
if(ans ==1)
{
list[0]->disp_area();
}
elseif(ans==2)
{
list[1]->disp_area();
}
elseif(ans==3)
{
list[2]->disp_area();
}
elseif(ans==4)
{
list[3]->disp_area();
}
elseif(ans==5)
exit(1);
else
{
cout<<" INVALID CHOICE";
getch();
continue;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.