C++ - Classes and inheritance Please help me with my homework. Overview In this
ID: 654557 • Letter: C
Question
C++ - Classes and inheritance
Please help me with my homework.
Overview
In this project you are building a base class, with pure virtual functions. The class is called
GeometricShape. You will then create the classes Circle, Rectangle and Triangle. These will all
inherit publicly from GeometricShape will implement the virtual functions. You can inline the
member functions for trivial member functions, but you must NOT inline all of the member
functions in a class.
Building the base class GeometricShape
In the base class GeometricShape you need to have a virtual destructor and three pure virtual
functions. The following are the signatures of the virtual destructor and the three pure virtual
functions:
virtual ~GeometricShape();
virtual double getArea() const = 0;
virtual double getPerimeter() const = 0;
virtual std::string to_string() const = 0;
Outside of the GeometricShape class definition, but in the header file, you need to create the
following function:
std::ostream& operator<< (std::ostream& outputStream,
const GeometricShape& shape);
In the operator<< function you will call the GeometricShape function to_string() to actually
build the formatted output. You will be implementing this to_string member function in each of
the classes that is derived from the GeometricShape class.
You will need a GeometricShape.h file and a GeometricShape.cpp file for this class.
Building the Circle class
The Circle class will be implemented with files Circle.h and Circle.cpp. The Circle class will
inherit, publicly, from GeometricShape. You need to implement a destructor and the three
virtual functions getArea, getPerimeter and to_string. You will also need a constructor that
takes one input parameter, the radius of the circle. The radius is of type double. The getArea
function needs to calculate the area and return it back as a double. The getPerimeter function
needs to calculate the perimeter (circumference) of the circle.
For the to_string function you need the following:
std::string Circle::to_string() const
{
return "Circle radius(" + std::to_string(radius) + ")";
}
Note that the std::to_string(radius) function is part of the string class and you need to include
<string>.
Building the Rectangle class
The Rectangle class will be implemented with files Rectangle.h and Rectangle.cpp. It will have a
destructor and will implement the three virtual functions. It will also have a constructor. The
constructor will take two input parameters, both of type double. The first parameter is the
width and the second is the length. When you implement the to_string function you need to
display that it is a Rectangle and you need to display the width and the length. This is similar to
what you did for the Circle class
Explanation / Answer
#include<iostream>
#include<string>
#include<vector>
#include<math.h>
using namespace std;
class Shape
{
public:
virtual float get_perimeter() = 0;
virtual float get_area() = 0;
virtual ~Shape() {};
std::string name;
};
class Rectangle : public Shape
{
private :
float width,height;
public:
Rectangle()
{
}
Rectangle(float w,float h)
{
width = w;
height = h;
name = "Rectangle";
}
float get_perimeter()
{
float perimeter;
perimeter = 2*(width+height);
return perimeter;
}
float get_area()
{
float area;
area = width*height;
return area;
}
~Rectangle()
{
cout<<"Destroying Rectangle object ";
}
};
class Circle : public Shape
{
private :
float radius;
public:
Circle()
{
}
Circle(float r)
{
radius = r;
name = "Circle";
}
float get_perimeter()
{
float perimeter;
perimeter = 2*3.141*radius;
return perimeter;
}
float get_area()
{
float area;
area = 3.141*radius*radius;
return area;
}
~Circle()
{
cout<<"Destroying Circle object ";
}
};
class Square : public Rectangle
{
public:
Square()
{
}
Square(float side):Rectangle(side,side){
name = "Square";
}
~Square()
{
cout<<"Destroying Square object ";
}
};
class Triangle : public Shape
{
private:
float side1,side2,side3;
public:
Triangle()
{
}
Triangle(float s1,float s2,float s3)
{
side1 =s1;
side2 =s2;
side3 =s3;
name = "Triangle";
}
float get_perimeter()
{
float perimeter;
perimeter = side1+side2+side3;
return perimeter;
}
float get_area()
{
float area;
float s;
s= (side1+side2+side3)/2;
area = sqrt(s*(s-side1)*(s-side2)*(s-side3));
return area;
}
~Triangle()
{
cout<<"Destroying Triangle object ";
}
};
class IsoscelesTriangle : public Triangle
{
public:
IsoscelesTriangle()
{
}
IsoscelesTriangle(float side1,float side2):Triangle(side1,side1,side2)
{
name = "Isosceles";
}
~IsoscelesTriangle()
{
cout<<"Destroying IsoscelesTriangle object ";
}
};
class EquilateralTriangle : public Triangle
{
public:
EquilateralTriangle()
{
}
EquilateralTriangle(float side1):Triangle(side1,side1,side1)
{
name = "Isosceles";
}
~EquilateralTriangle()
{
cout<<"Destroying EquilateralTriangle object ";
}
};
int main()
{
std::vector<Shape*> shapes;
shapes.push_back(new Rectangle(3.5, 4.5));
shapes.push_back(new Circle(2));
shapes.push_back(new Triangle(4,5,6));
shapes.push_back(new Square(4));
shapes.push_back(new IsoscelesTriangle(4, 5));
shapes.push_back(new EquilateralTriangle(4));
for (int i=0;i<shapes.size();i++)
{
std::cout << "The area for this "<<shapes[i]->name<<" is " << shapes[i]->get_area() << std::endl;
std::cout << "The perimeter for this "<<shapes[i]->name<<" is " << shapes[i]->get_perimeter() << std::endl;
}
for (int i=0;i<shapes.size();i++)
{
delete shapes[i];
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.