Question

In: Computer Science

Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived class of Rectangle. There can...

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.

Solutions

Expert Solution

#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


Related Solutions

Refactor the following classes so they are both derived from a base class called Person. Write...
Refactor the following classes so they are both derived from a base class called Person. Write the code for the new base class as well. Try to avoid repeating code as much as possible. Write the classes so that any common methods can be invoked through a pointer or reference to the base class. #include <string> #include <cmath> using namespace std; class Student { private: string name;    int age;    int studyYear; public:    Student(string,int,int); void study();    void...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The program to present the user with a menu where one of the shapes can be selected. Based on the selection made, the user enters the proper input, the program validates the input (i.e all entries must be greater than zero). Once the input is entered and validated, the intended area is calculated and the entered information along with the area are displayed. Area of...
Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length...
Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length and width, a Circle constructed by a radius and a Square constructed by a side length. Both classes should have the methods that compute: - The area - The diagonal - The perimeter Use as much abstraction as you can. At the end of the file, use those classes to calculate the perimeter of a circle with radius the half of the diagonal of...
Using Java Write only “stubs” for methods in the Figure, Rectangle, and Triangle classes: Consider a...
Using Java Write only “stubs” for methods in the Figure, Rectangle, and Triangle classes: Consider a graphics system that has classes for various figures — say, rectangles, boxes, triangles, circles, and so on. For example, a rectangle might have data members’ height, width, and either a center point or upper-left corner point, while a box and circle might have only a center point (or upper-right corner point) and an edge length or radius, respectively. In a well-designed system, these would...
JAVA Design a class named Person and its two derived classes named Student and Employee. Make...
JAVA Design a class named Person and its two derived classes named Student and Employee. Make Faculty and Staff derived classes of Employee. A person has a name, address, phone number, and e-mail address. A student has a class status (freshman, sophomore, junior, or senior). An employee has an office, salary, and datehired. Define a class named MyDate that contains the fields year, month, and day. A faculty member has office hours and a rank. A staff member has a...
Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the...
Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep ( i.e., as many levels ) as possible. Specify the instance variables and methods for each class. The private instances variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. Write a program that instantiates objects...
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4)...
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4) Circle 5) TestAllShapes (create 1 object of each type and print the Area for each of them.)
Class object in C++ programming language description about lesson base class and derived class example.
Class object in C++ programming language description about lesson base class and derived class example.
7.24 LAB: Triangle area comparison (classes) Language: C++ Given class Triangle (in files Triangle.h and Triangle.cpp),...
7.24 LAB: Triangle area comparison (classes) Language: C++ Given class Triangle (in files Triangle.h and Triangle.cpp), complete main() to read and set the base and height of triangle1 and of triangle2, determine which triangle's area is larger, and output that triangle's info, making use of Triangle's relevant member functions. Ex: If the input is: 3.0 4.0 4.0 5.0 where 3.0 is triangle1's base, 4.0 is triangle1's height, 4.0 is triangle2's base, and 5.0 is triangle2's height, the output is: Triangle...
In this assignment, students should create five Java classes called Point, Circle, Square, Rectangle, and TestAll,...
In this assignment, students should create five Java classes called Point, Circle, Square, Rectangle, and TestAll, as well as a Java interface called FigureGeometry. The TestAll class should implement the main method which tests all of the other files created in the assignment. After the assignment has been completed, all six files should be submitted for grading into the Dropbox for Assignment 3, which can be found in the Dropbox menu on the course website. Students should note that only...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT