-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo36.cpp
132 lines (118 loc) · 2.28 KB
/
demo36.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
using namespace std;
class Shape
{
public:
Shape(){}
virtual ~Shape(){}
virtual double GetArea() = 0; // 纯虚函数
virtual double GetPerim() = 0;
virtual void Draw() = 0;
};
/*
纯虚函数可以写,也可以不写
void Shape::Draw()
{
cout << "Abstract drawing 方法!\n";
}
*/
class Circle: public Shape
{
public:
Circle(int radius): itsRadius(radius) {}
virtual ~Circle() {}
double GetArea() { return 3.14 * itsRadius * itsRadius; }
double GetPerim() { return 2 * 3.14 * itsRadius; }
void Draw();
private:
int itsRadius;
};
void Circle::Draw()
{
cout << "Circle drawing rounting here!\n";
// Shape::Draw();
}
class Rectangle: public Shape
{
public:
Rectangle(int len, int width): itsWidth(width), itsLength(len){}
virtual ~Rectangle() {}
double GetArea() { return itsLength * itsWidth; }
double GetPerim() { return 2 * (itsWidth + itsLength); }
virtual int GetLength() { return itsLength; }
virtual int GetWidth() { return itsWidth; }
void Draw();
private:
int itsWidth;
protected:
int itsLength;
};
void Rectangle::Draw()
{
for(int i=0; i<itsWidth; ++i)
{
for(int j=0; j<itsLength; ++j)
cout << "x ";
cout << "\n";
}
// Shape::Draw();
}
class Square: public Rectangle
{
public:
Square(int len);
Square(int len, int width);
virtual ~Square() {}
// double GetPerim() { return 4 * GetLength(); }
double GetPerim() { return 4 * itsLength; }
double GetArea() { return GetLength() * GetLength(); }
};
Square::Square(int len): Rectangle(len, len) {}
Square::Square(int len, int width): Rectangle(len, width)
{
if(GetLength() != GetWidth())
cout << "Error, not a square... a Rectangle??\n";
}
int main()
{
// Circle a(8);
// a.Draw();
// Rectangle b(10, 5);
// b.Draw();
// cout << endl;
// Square c(8);
// c.Draw();
int choice;
bool fQuit = false;
Shape *sp;
while(fQuit == false)
{
cout << "(1) Circle (2) Rectangle (3) Square (0) Quit: ";
cin >> choice;
switch(choice)
{
case 1:
sp = new Circle(5);
break;
case 2:
sp = new Rectangle(10, 5);
break;
case 3:
sp = new Square(5);
break;
case 0:
fQuit = true;
break;
default:
cout << "unvalid number, try again!"<< endl;
break;
}
if(fQuit == false)
{
sp->Draw();
delete sp;
cout << endl;
}
}
return 0;
}