include iostream include sstream using namespace std class Figure publ

 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <sstream>
using namespace std;
class Figure
{
public:
virtual string info() = 0;
virtual double area() = 0;
string fullInfo() {
return mName + " ( " + info() + " )";
}
string name() {
return mName;
}
protected:
string mName;
};
class Rectangle : public Figure
{
public:
Rectangle( double w, double h ) {
width = w;
height = h;
mName = "Rectangle";
}
string info() {
stringstream ss;
ss << "width: " << width << ", height: " << height;
return ss.str();
}
double area() {
return width * height;
}
protected:
double width, height;
};
class Circle : public Figure
{
public:
Circle( double r ) {
radius = r;
mName = "Circle";
}
string info() {
stringstream ss;
ss << "radius: " << radius;
return ss.str();
}
double area() {
return 3.14 * radius * radius;
}
protected:
double radius;
};
void printFigure( Figure *figure )
{
cout << "For figure " << figure->fullInfo()
<< " area is " << figure->area() << endl;
}
int main()
{
Rectangle rect( 20.0, 5.0 );
Circle circle( 6 );
printFigure( &rect );
printFigure( &circle );
}
---- В результате в консоле будет ----
For figure Rectangle ( width: 20, height: 5 ) area is 100
For figure Circle ( radius: 6 ) area is 113.04