Please help. I need an explanation on how inheritance works in THIS C++ CODE and
ID: 3911315 • Letter: P
Question
Please help. I need an explanation on how inheritance works in THIS C++ CODE and how the inclusion of the Shape base class affects the structure of the other classes IN THIS CODE. Not a definition of inheretiance and inclusion. Thanks
Circle.cpp
#include "circle.h"
#include <iostream>
#include <string>
using namespace std;
void Circle::input()
{
ID = 1;
type = "Circle";
cout << "Enter radius: ";
cin >> radius;
cout << "Enter Unit Of Measure: ";
cin >> unitOfMeasure;
}
void Circle::print()
{
// circumference of circle
double circumference = 2 * 3.14159 * radius;
cout << type << " with ID = " << ID
<< ", radius = " << radius << " " << unitOfMeasure
<< ", area = " << GetArea() << " " << unitOfMeasure << "^2"
<< ", circumference = " << " " << circumference << unitOfMeasure << endl;
}
double Circle::GetArea()
{
// area of circle
return 3.14159 * radius * radius;
}
Main.cpp
#include <iostream>
#include "square.h"
#include "circle.h"
#include "triangle.h"
using namespace std;
int main()
{
char type;
// Loop till user want to quit
Shape* sh = nullptr;
while (true)
{
// Prompt user for option
cout << "Enter type of figure(C - Circle, S - Square, T - Triangle or Q - quit: ";
cin >> type;
switch(type)
{
case 'C': sh = new Circle; break;
case 'S': sh = new Square; break;
case 'T': sh = new Triangle; break;
case 'Q': exit(0);
default:
cout << "Invalid ";
break;
}
if(sh)
{
sh->input();
sh->print();
delete sh;
sh = nullptr;
}
cout << endl;
}
return 0;
}
Square.cpp
// include square.h header file
#include "square.h"
// Constructor
Square::Square(double _a)
: a(_a)
{
}
// function to calculate area
double Square::GetArea()
{
return a * a;
}
// function to read sides
void Square::input()
{
ID = 2;
type = "Square";
cout << "Enter side: ";
cin >> a;
cout << "Enter Unit Of Measure: ";
cin >> unitOfMeasure;
}
// print sides and area
void Square::print()
{
cout << type << endl;
cout << "ID: " << ID << endl;
cout << "Side: " << a << " " << unitOfMeasure << endl;
cout << "Area: " << GetArea() << " " << unitOfMeasure << "^2" << endl;
}
Triangle.cpp
#include "triangle.h"
#include <cmath>
double Triangle::GetArea()
{
double s = (a + b + c)/2;
return sqrt((s*(s-a)*(s-b)*(s-c)));
}
void Triangle::input()
{
ID = 3;
type = "Triangle";
cout << "Enter side a: ";
cin >> a;
cout << "Enter side b: ";
cin >> b;
cout << "Enter side c: ";
cin >> c;
cout << "Enter Unit Of Measure: ";
cin >> unitOfMeasure;
}
void Triangle::print()
{
cout << type << endl;
cout << "ID: " << ID << endl;
cout << "Side a: " << a << " " << unitOfMeasure << ", Side b: "
<< b << " " << unitOfMeasure
<< ", Side c: " << c << " " << unitOfMeasure << endl;
cout << "Area: " << GetArea() << " " << unitOfMeasure << "^2" << endl;
}
Circle.h
#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include <iostream>
#include "shape.h"
using namespace std;
class Circle : public Shape
{
private:
double radius;
double GetArea() override;
public:
Circle() = default;
void input() override;
void print() override;
};
#endif // CIRCLE_H_INCLUDED
Shape.h
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
#include <iostream>
using namespace std;
class Shape
{
protected:
int ID;
string type;
string unitOfMeasure;
Shape() = default;
virtual double GetArea() = 0;
public:
virtual void input() = 0;
virtual void print() = 0;
virtual ~Shape(){}
};
#endif // SHAPE_H_INCLUDED
Square.h
#ifndef SQUARE_H_INCLUDED
#define SQUARE_H_INCLUDED
#include <iostream>
#include <string>
#include "shape.h"
using namespace std;
class Square : public Shape
{
private:
double a;
//declaration of functions in header file
double GetArea() override;
public:
// constructor with default arguments 0.0
Square(double _a = 0.0);
void print() override;
void input() override;
};
#endif // SQUARE_H_INCLUDED
Triangle.h
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include "shape.h"
class Triangle : public Shape
{
private:
double a, b, c;
double GetArea() override;
public:
Triangle() = default;
void input() override;
void print() override;
};
#endif // TRIANGLE_H
Explanation / Answer
In the given example shape class is the base class and remaining others are derived classes means they inherit state and behaviour of shape class.The presence of shape class largely affects the structure of other classes because all the classes inherit variables ike unit of measure from shape class without even declaring it in their respective classes.Moreover the methods are inherited as well as overriden by derived classes from base class.All the methods declared as public in base classes can be accessed by derived classes.