Polygon Perimeter Calculation via Inheritance
Givan the base class framework for polygons:
class polygon
{
protected:
int number; // Number of sides, at most 100
private:
int side_length[100]; // Array of side lengths
public:
polygon(); // Constructor can be overloaded as needed
int perimeter(); // Calculate polygon perimeter
void display(); // Output side count and perimeter
}
Create a derived class retcangle, adding the following data members:
int height;
int width;
Add the following member functions:
rectangle's default and parameterized constructors
int perimeter(); // Calculate rectangle perimeter
void display(); // Output side count and perimeter
Create another derived class equal_polygon (equilateral polygon), adding the following data members:
int side_len;
Add the following member functions:
equal_polygon's default and parameterized constructors
int perimeter(); // Calculate equilateral polygon perimeter
void display(); // Output side count and perimeter
Implement the above classes and write a main function. Based on input polygon information, create an object of either the polygon class, rectangle class, or equal_polygon class. Calculate each polygon's perimeter and output its side count and perimeter.
Input format: The test input consists of a single test case. The first line contains an integer n, the number of polygnos. Each of the next n lines provides basic information for one polygon. The first number on each line indicates the polygon type: 0 for a general polygon, followed by m side lengths, with -1 marking the end of side lengths; 1 for a rectangle, followed by two numbers representing height and width; 2 for an equilateral polygon, followed by two numbers representing the number of sides and the side length.
Sample Input:
3
0 32 54 76 88 24 -1
1 32 54
2 3 32
Sample Output:
5 274
4 172
3 96
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class polygon {
protected:
int number;
private:
int side_length[100];
public:
int total = 0;
polygon() {}
polygon(int num, int lengths[]) {
number = num;
for (int i = 0; i < num; ++i) {
side_length[i] = lengths[i];
}
}
int perimeter() {
total = 0;
for (int i = 0; i < number; ++i) {
total += side_length[i];
}
return total;
}
void display() {
cout << number << " " << total << endl;
}
};
class rectangle : public polygon {
protected:
int height;
int width;
public:
rectangle(int h, int w) {
number = 4;
height = h;
width = w;
}
int perimeter() {
total = (height + width) * 2;
return total;
}
};
class equal_polygon : public polygon {
protected:
int side_len;
public:
equal_polygon(int num, int len) {
number = num;
side_len = len;
}
int perimeter() {
total = number * side_len;
return total;
}
};
int main() {
int polygonCount;
cin >> polygonCount;
for (int i = 0; i < polygonCount; ++i) {
int type;
cin >> type;
if (type == 0) {
int lengths[100];
int count = 0;
int val;
while (true) {
cin >> val;
if (val == -1) break;
lengths[count++] = val;
}
polygon p(count, lengths);
p.perimeter();
p.display();
}
if (type == 1) {
int h, w;
cin >> h >> w;
rectangle r(h, w);
r.perimeter();
r.display();
}
if (type == 2) {
int num, len;
cin >> num >> len;
equal_polygon e(num, len);
e.perimeter();
e.display();
}
}
return 0;
}