-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDD80_2.cpp
63 lines (52 loc) · 1014 Bytes
/
DD80_2.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
#include <iostream>
using namespace std;
const double PI = 3.141592653589793;
/*
추상클래스 존재의 목적 : 도형(Shape클래스)을 상속받는다면 ( 부모 클래스가 도형이라면 )
자식클래스는 반드시 GetArea, Resize를 해야한다 . ( 반드시 상속받은 추상 클래스의 순수가상함수를 구현해야한다 )
즉, 추상 클래스는 틀을 제공한다
*/
class Shape {
public:
virtual double GetArea() = 0;
virtual void Resize(double f) = 0;
};
class Circle : public Shape {
public:
Circle(double r) : r(r) {}
double GetArea() {
return PI * r * r;
}
void Resize(double f) {
r *= f;
}
private:
double r;
};
class Rectangle : public Shape {
public:
Rectangle(double a, double b) : a(a), b(b) {}
double GetArea() {
return a * b;
}
void Resize(double f) {
a *= f;
b *= f;
}
private:
double a, b;
};
int main() {
Shape *shapes[] = {
new Circle(10),
new Rectangle(20, 30)
};
for (Shape *s : shapes) {
s->Resize(2);
cout << s->GetArea() << endl;
}
// 메모리해제 반드시..
for (Shape *s : shapes) {
delete s;
}
}