1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <iostream>
#include <vector>
class Shape
{
int x, y;
public:
Shape(int x, int y) : x{x}, y{y} {}
virtual void draw() const = 0;
void print_position() const
{
std::cout << "Position: " << x << ", " << y << '\n';
}
};
class Rect : public Shape
{
int dx, dy;
public:
Rect(int x, int y, int dx, int dy) : Shape{x, y}, dx{dx}, dy{dy} {}
void draw() const override
{
std::cout << "Rectangle:\n ";
print_position();
std::cout << " Size: " << dx << ", " << dy << '\n';
}
};
class Circle : public Shape
{
int radius;
public:
Circle(int x, int y, int r) : Shape{x, y}, radius{r} {}
void draw() const override
{
std::cout << "Circle:\n ";
print_position();
std::cout << " Radius: " << radius << '\n';
}
};
int main()
{
std::vector<Shape*> shapes;
shapes.push_back(new Rect(10, 20, 30, 40));
shapes.push_back(new Circle(50, 60, 70));
for (const auto& sh: shapes)
sh->draw();
}
|