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

Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived c

ID: 3807126 • Letter: B

Question

Base class: Polygon

Derived classes: Rectangle, Triangle

Make Square a derived class of Rectangle.

There can be several ways to represent the shapes. For example, a shape can be represented by an array of side lengths counted in the clock-wise order. For example, {2, 4, 2, 4} for a rectangle of width 2 and height 4, and {4, 4, 4, 4} for a square. You need to design your representation.

Each polygon should have a function area() that returns its area, and perimeter() that returns its perimeter.

The area of a triangle, given three sides a, b, and c, can be calculated using Heron's formula (http://www.mathopenref.com/heronsformula.html).

In a main program, create an array of pointers to Polygon; create at least one object from each derived class, assign it to the array, and print out the area and perimeter of each array member.

Polygon * shapes[3];

shapes[0] = new Rectangle(4.0, 2.0);

shapes[1] = new Square(4.0);

shapes[3] = new Triangle(4.0, 4.0, 4.0);

for (int i=0; i<3)

cout<<shapes[i]->area()<<" ,"<<shapes[i]->perimeter()<<endl;

Design your classes properly by using dynamic binding and well-thought-out constructors.

Submit 9 C++ files in one zip file: one header file and one cpp file for each of the four classes, plus a client.cpp.

Explanation / Answer

#include <iostream>
#include<math.h>

using namespace std;

class Polygon{ // This is the base class!
  
private:
  
public:
virtual float area(){
cout << "inside shape" << endl;
}
  
virtual float perimeter(){
cout << "inside shape" << endl;
}

};
class Triangle : public Polygon{
  
private:
float Aside, bside, cside;

public:
Triangle(float asideL, float bsideL, float csideL){
Aside = asideL;
bside = bsideL;
cside = csideL;
}
  
float area(){
float p = (Aside+bside+cside)/2;
return sqrt(p*(p-Aside)*(p-bside)*(p-cside));
}
float perimeter(){
return Aside + bside + cside;
}
};

class Rectangle : public Polygon{
private:
float width, length;

public:
Rectangle(float widthL, float lengthL){
width = widthL;
length = lengthL;
}
  
float area(){
cout << " enter into area" << endl;
return width*length;
}
float perimeter(){
return (2 * (length + width));
}
};
class Square : public Polygon{
  
private:
float side;

public:
Square(float sideL){
side = sideL;
}
  
float area(){
return side *side;
}
float perimeter(){
return 4*side;
}
};


int main()
{
Polygon *shapes[3];
  
Rectangle rec(4.0, 2.0);
shapes[0] = &rec;
  
Square sqr(4.0);
shapes[1] = &sqr ;
  
Triangle tri(4.0, 4.0, 4.0);
shapes[2] = &tri ;
  
int i = 0;
cout.precision(2);
for (; i<3 ; i++){
cout<< shapes[i]->area() <<" ," <<shapes[i]->perimeter()<<endl;
}
cout << "Hello World" << endl;

return 0;
}

Output:

-----------

sh-4.2$ main                                                                                                                                                        

Rectangle :                                                                                                                                                        

         Area - 8 , Perimeter - 12                                                                                                                                  

Square :                                                                                                                                                           

         Area - 16 , Perimeter - 16                                                                                                                                 

Triangle :                                                                                                                                                         

         Area - 6.9 , Perimeter - 12

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