107 lines
2.8 KiB
C++
107 lines
2.8 KiB
C++
#include <iostream>
|
|
|
|
class Shape {
|
|
public:
|
|
Shape(){};
|
|
~Shape(){};
|
|
virtual float getArea() = 0;
|
|
virtual float getPerim() = 0;
|
|
virtual std::istream &operator>>(std::istream &input) = 0;
|
|
};
|
|
|
|
class Rectangle : public Shape {
|
|
private:
|
|
float length, height;
|
|
|
|
public:
|
|
Rectangle(float _length = 0, float _height = 0)
|
|
: length(_length), height(_height){};
|
|
~Rectangle(){};
|
|
virtual float getArea() {
|
|
return this->length * this->height;
|
|
}
|
|
|
|
virtual float getPerim() {
|
|
return (this->length + this->height) * 2;
|
|
}
|
|
virtual std::istream &operator>>(std::istream &input) {
|
|
std::cout << "Length: ";
|
|
std::cin >> this->length;
|
|
std::cout << "Height: ";
|
|
std::cin >> this->height;
|
|
return input;
|
|
}
|
|
};
|
|
|
|
class Circle : public Shape {
|
|
private:
|
|
float radius;
|
|
|
|
public:
|
|
Circle(float _radius = 0) : radius(_radius){};
|
|
~Circle(){};
|
|
virtual float getArea() {
|
|
return 3.14 * this->radius * this->radius;
|
|
}
|
|
|
|
virtual float getPerim() {
|
|
return 2 * 3.14 * this->radius;
|
|
}
|
|
|
|
virtual std::istream &operator>>(std::istream &input) {
|
|
std::cout << "Radius: ";
|
|
std::cin >> this->radius;
|
|
return input;
|
|
}
|
|
};
|
|
|
|
inline std::istream &operator>>(std::istream &input, Shape &thisShape) {
|
|
return thisShape >> input;
|
|
}
|
|
|
|
int main() {
|
|
Shape *shapes[2];
|
|
{
|
|
std::string shapeType;
|
|
do {
|
|
std::cout << "The type of shape[0] ((r)ectangle, (c)ircle): ";
|
|
std::cin >> shapeType;
|
|
if (shapeType == "r") {
|
|
shapes[0] = new Rectangle();
|
|
break;
|
|
}
|
|
else if (shapeType == "c") {
|
|
shapes[0] = new Circle();
|
|
break;
|
|
}
|
|
else {
|
|
std::cout << "Unknown name. Try again." << std::endl;
|
|
}
|
|
} while (true);
|
|
std::cin >> *shapes[0];
|
|
|
|
do {
|
|
std::cout << "The type of shape[1] ((r)ectangle, (c)ircle): ";
|
|
std::cin >> shapeType;
|
|
if (shapeType == "r") {
|
|
shapes[1] = new Rectangle();
|
|
break;
|
|
}
|
|
else if (shapeType == "c") {
|
|
shapes[1] = new Circle();
|
|
break;
|
|
}
|
|
else {
|
|
std::cout << "Unknown name. Try again." << std::endl;
|
|
}
|
|
} while (true);
|
|
std::cin >> *shapes[1];
|
|
}
|
|
std::cout << "##### Shapes[0] #####" << std::endl;
|
|
std::cout << "Area = " << shapes[0]->getArea()
|
|
<< ", Perim = " << shapes[0]->getPerim() << std::endl;
|
|
std::cout << "##### Shapes[1] #####" << std::endl;
|
|
std::cout << "Area = " << shapes[1]->getArea()
|
|
<< ", Perim = " << shapes[1]->getPerim() << std::endl;
|
|
return 0;
|
|
} |