@rangerx
1941 / 1550 / 141
Регистрация: 31.05.2009
Сообщений: 2,913
|
28.02.2011, 20:33
|
|
C++ | 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
88
89
90
| #include <iostream>
class Shape
{
public:
virtual double square() const = 0;
virtual ~Shape() { }
};
class Trapeze : public Shape
{
private:
double a_;
double b_;
double h_;
public:
Trapeze(double a, double b, double h)
: a_(a), b_(b), h_(h)
{
}
virtual double square() const
{
return (a_ + b_) * h_ / 2;
}
};
class Rectangle : public Shape
{
private:
double a_;
double b_;
public:
Rectangle(double a, double b)
: a_(a), b_(b)
{
}
virtual double square() const
{
return a_ * b_;
}
};
int main()
{
std::cout << "Input shape name('trapeze' or 'rectangle'): ";
std::string shapeName;
std::getline(std::cin, shapeName);
Shape* shape = 0;
if(shapeName == "trapeze")
{
std::cout << "Input a: ";
double a;
std::cin >> a;
std::cout << "Input b: ";
double b;
std::cin >> b;
std::cout << "Input h: ";
double h;
std::cin >> h;
shape = new Trapeze(a, b, h);
}
else if(shapeName == "rectangle")
{
std::cout << "Input a: ";
double a;
std::cin >> a;
std::cout << "Input b: ";
double b;
std::cin >> b;
shape = new Rectangle(a, b);
}
if(shape)
std::cout << "Square: " << shape->square() << '\n';
else
std::cout << "Invalid input...\n";
delete shape;
return 0;
} |
|
1
|