2024-10-29 22:26:00 +08:00
|
|
|
//
|
|
|
|
// Created by Marcus Vinicius de Carvalho
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
enum KindOfPolygon {POLY_PLAIN, POLY_RECT, POLY_TRIANG};
|
|
|
|
|
2024-10-30 07:36:34 +08:00
|
|
|
string StringKindofPolygon[] = {"POLY_PLAIN", "POLY_RECT", "POLY_TRIANG"};
|
2024-10-29 22:26:00 +08:00
|
|
|
|
|
|
|
class Polygon {
|
|
|
|
|
|
|
|
protected:
|
|
|
|
string name;
|
|
|
|
float width;
|
|
|
|
float height;
|
|
|
|
KindOfPolygon polytype;
|
|
|
|
|
|
|
|
public:
|
2024-10-30 07:36:34 +08:00
|
|
|
Polygon(string name, float width, float height) : name(name), width(width), height(height) {
|
2024-10-29 22:26:00 +08:00
|
|
|
polytype = POLY_PLAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
KindOfPolygon getPolytype() {
|
|
|
|
return polytype;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPolytype(KindOfPolygon value) {
|
|
|
|
polytype = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
string getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual float calArea() = 0;
|
|
|
|
|
|
|
|
void printWidthHeight() const {
|
|
|
|
cout << "Width: " << width << "; Height: " << height << endl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Rectangle : public Polygon {
|
|
|
|
public:
|
|
|
|
Rectangle(string name, float width, float height) : Polygon(std::move(name), width, height) {
|
|
|
|
polytype = POLY_RECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
float calArea() override {
|
|
|
|
return width * height;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Triangle : public Polygon {
|
|
|
|
public:
|
|
|
|
Triangle(string name, float width, float height) : Polygon(std::move(name), width, height) {
|
|
|
|
polytype = POLY_TRIANG;
|
|
|
|
}
|
|
|
|
|
|
|
|
float calArea() override {
|
|
|
|
return width * height * 0.5f;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestPolygon {
|
|
|
|
public:
|
|
|
|
static void printArea(Polygon& poly) {
|
|
|
|
float area = poly.calArea();
|
|
|
|
cout << "The area of the " << StringKindofPolygon[poly.getPolytype()] << " is " << area << endl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
Rectangle rectangle1("Rectangle1", 3.0f, 4.0f);
|
|
|
|
rectangle1.printWidthHeight();
|
|
|
|
TestPolygon::printArea(rectangle1);
|
|
|
|
|
|
|
|
Triangle triangle1("Triangle1", 3.0f, 4.0f);
|
|
|
|
triangle1.printWidthHeight();
|
|
|
|
TestPolygon::printArea(triangle1);
|
|
|
|
}
|