For this assignment, you will write a new version of the area calculation progra
ID: 3724142 • Letter: F
Question
For this assignment, you will write a new version of the area calculation program from Unit 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). You will use the Code::Blocks software to write this application.
Assignment Instructions
Part 1: Write the Application
Write a new version of the area calculation program from Unit 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 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.
My code from the previous assignment is below:
CODE :
circle.h :
#ifndef CIRCLE_H
#define CIRCLE_H
struct Circle { // circle structure class
public: Circle(); // declaration
void setRadius(double r); // setting the radius
double getRadius(); // getting the radius
double Area(); // get the area calculated
void Display(); // display the datda
protected: double Radius; // radius value declared
private:
};
#endif
square.h :
#ifndef SQUARE_H
#define SQUARE_H
struct Square { // square structure class
public : Square(); // declaration
void setLength(double l); // setting the length
double getLength(); // getting the length
double Area(); // get the area calculated
void Display(); // display the data
protected : double Length; // length value declaration
private:
};
#endif
circle.cpp :
#include <iostream>
#include "circle.h"
using namespace std;
const double PI = 3.14159; // initialisng the global values
Circle::Circle() { // method declaration
Radius = 0; // initialisng the radius value
}
void Circle::setRadius(double r) {
Radius = r <= 0 ? 1 : r; // checking the input value
}
double Circle::getRadius() {
return Radius; //getting and setting the value
}
double Circle::Area(){
return Radius * Radius * PI; // returning the area
}
void Circle::Display(){ // display the data
cout << " Circle Properties";
cout << " Radius = " << getRadius();
cout << " Area = " << Area() << " ";
}
square.cpp :
#include <iostream>
#include "square.h"
using namespace std;
Square::Square(){ // method declaration
Length = 0; // length initialisng
}
void Square::setLength(double l) {
Length = l <= 0 ? 1 : l; // checking the data
}
double Square::getLength() {
return Length; // returning the length
}
double Square::Area() {
return Length*4; // returning the area
}
void Square::Display(){ // display the data
cout << " Square Properties";
cout << " Length = " << getLength();
cout << " Area = " << Area() << " ";
}
main.cpp :
#include "circle.h"
#include <iostream>
#include "square.h" // including the header files
using namespace std;
int main(){
int radius,length;
Circle circle; // Displaying a circle using the default value of the radius
circle.Display();
Circle circ; // circle with a value supplied for the radius
cout << "Enter the radius value :" << endl; // taking the value from the user
cin >> radius;
circ.setRadius(radius);
circ.Display();
Square square; // Displaying a square
square.Display();
Square squ; // square with a value supplied for the length
cout << "Enter the length value :" << endl;
cin >> length;
squ.setLength(length); // of the base class
squ.Display();
return 0;
}
Explanation / Answer
#include<iostream.h>
#include<conio.h>
const double PI=3.14159;
class shape
{
public:
int shapeID, type;
float measure;
virtual void getArea()
{
cout<<"Enter shape id";
cin>>shapeID;
if(shapeID==0)
}
protected:
double Radius; // radius value declared
double base; // radius value declared
double height;
double Length;
};
class Circle : public shape
{ // circle class
public:
Circle(); // declaration
void setRadius(double r); // setting the radius
double getRadius(); // getting the radius
double Area(); // get the area calculated
void Display(); // display the datda
};
class Square : public shape
{ // square structure class
public :
Square(); // declaration
void setLength(double l); // setting the length
double getLength(); // getting the length
double Area(); // get the area calculated
void Display(); // display the data
};
class tri : public shape
{ // triangle class
public:
tri();
void getValues(double x, double y);
double Area();
void Display();
};
tri::tri() // declaration
{base=0;
height=0;
}
void tri::getValues(double x, double y)
{
base=x;
height=y;
} // setting values
double tri::Area() // get the area calculated
{
double a=(base*height)/2;
return a;
}
void tri::Display() // display the datda
{
cout<<"Area of trianlge is"<<Area();
}
Circle::Circle()
{ // method declaration
Radius = 0; // initialisng the radius value
}
void Circle::setRadius(double r) {
Radius = r <= 0 ? 1 : r; // checking the input value
}
double Circle::getRadius() {
return Radius; //getting and setting the value
}
double Circle::Area(){
return Radius * Radius * PI; // returning the area
}
void Circle::Display(){ // display the data
cout << " Circle Properties";
cout << " Radius = " << getRadius();
cout << " Area = " << Area() << " ";
}
Square::Square(){ // method declaration
Length = 0; // length initialisng
}
void Square::setLength(double l) {
Length = l <= 0 ? 1 : l; // checking the data
}
double Square::getLength() {
return Length; // returning the length
}
double Square::Area() {
return Length*4; // returning the area
}
void Square::Display(){ // display the data
cout << " Square Properties";
cout << " Length = " << getLength();
cout << " Area = " << Area() << " ";
}
int main()
{
int radius,length;
//Circle circle; // Displaying a circle using the default value of the radius
//circle.Display();
Circle circ; // circle with a value supplied for the radius
cout << "Enter the radius value :" << endl; // taking the value from the user
cin >> radius;
circ.setRadius(radius);
circ.Display();
Square square; // Displaying a square
square.Display();
Square squ; // square with a value supplied for the length
cout << "Enter the length value :" << endl;
cin >> length;
squ.setLength(length); // of the base class
squ.Display();
tri T;
double b,h;
cout<<" Enter Triangle data";
cout<<" Enter base";
cin>>b;
cout<<"ENTER height";
cin>>h;
T.getValues(b,h);
T.Display();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.