Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Using C++ Implement a hierarchy of Shape s to demonstrate the OOP concep

ID: 3767124 • Letter: P

Question

Problem Using C++

Implement a hierarchy of Shapes to demonstrate the OOP concepts of inheritance and polymorphism.

Specification

Create class Shape that will serve as the base class for several types of Shapes. Declare data members in the Shapeclass for a description ("circle", "triangle", etc.) and an area, and include appropriate accessor (get) and mutator (set) functions. Also declare a virtual calcArea() function.

Derive classes Rectangle, Circle, and Triangle from class Shape. For each of these classes, include appropriate data members and implement the calcArea() function.

Create an array of at least 5 Shape pointers and fill the array with the addresses of several different kinds of shapes.

Use a for loop to calculate and print the description and area of each Shape in the array.

Include a multi-line comment in your program that describes the difference between static (early) binding and dynamic (late) binding and how dynamic binding facilitates polymorphic precessing.

Explanation / Answer

Here is the code as per your requirement. If you need any further clarifications, just get back to me.

#include <iostream>
#include <math.h>
#include <string>
using namespace std;
//inheritence example
class Shape
{//parent class
public://data member with public acces specifier
string color;
public:
Shape(const string& c)//constructor
{
color = c;
}
virtual ~Shape()   //Destructor.
{
}
virtual void print() //Base class print method.
{
cout<<color;
}
virtual double calcArea() = 0;    //Pure virtual function calcArea().
};
class Circle : public Shape   //Circle class inherits from Shape.
{
int radius;
float salary;
public:
Circle(string c, int r) : Shape(c), radius(r)   //Constructor.
{//default constructor
//color = c;
//r = radius;
}
void print()   //Overridden function print().
{
Shape::print();
cout<<" circle, radius "<<radius<<", area "<<calcArea()<<endl;
}
double calcArea()   //calcArea() definition.
{
return M_PI * radius * radius;
}
};

class Rectangle : public Shape   //Rectangle class inherits from Shape.
{
int height;
int width;
public:
Rectangle(string c, int h, int w) : Shape(c), height(h), width(w)
{
}
void print()
{
Shape::print();
cout<<" rectangle, height "<<height<<", width "<<width<<", area "<<calcArea()<<endl;
}
double calcArea()
{
return height * width;
}
};

class Triangle : public Shape   //Triangle class inherits from Shape.
{
int height;
int width;
public:
Triangle(string c, int h, int w) : Shape(c), height(h), width(w)
{
}
void print()
{
Shape::print();
cout<<" triangle, height "<<height<<", width "<<width<<", area "<<calcArea()<<endl;
}
double calcArea()
{
return 0.5 * height * width;
}
};

int main()
{
/*Circle c1 ("green", 10);
c1.print();
Rectangle r1 ("red", 8, 6);
r1.print();
Triangle t1 ("yellow", 8, 4);
t1.print();
Triangle t2 ("black", 4, 10);
t2.print();
Circle c2 ("orange", 5);
c2.print();
Rectangle r2 ("blue", 3, 7);
r2.print();
cout<<endl<<"Print only triangles..."<<endl<<endl;
t1.print();
t2.print();
return 0;*/
Shape *shapeArray[6];   //Array of Shape pointers.
Circle c1 ("green", 10);   //Creating the Circle object.
shapeArray[0] = &c1;       //Assigning it to the Shape pointer.
Rectangle r1 ("red", 8, 6);
shapeArray[1] = &r1;
Triangle t1 ("yellow", 8, 4);
shapeArray[2] = &t1;
Triangle t2 ("black", 4, 10);
shapeArray[3] = &t2;
Circle c2 ("orange", 5);
shapeArray[4] = &c2;
Rectangle r2 ("blue", 3, 7);
shapeArray[5] = &r2;
cout<<endl;
for(int i = 0; i < 6; i++)   //Looping for every pointer.
shapeArray[i]->print();   //Printing the value.
cout<<endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote