-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path30.cpp
107 lines (81 loc) · 2.2 KB
/
30.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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
class Heart {
private:
float _purseRate; // 박동수
public:
Heart(float purseRate)
: _purseRate(purseRate)
{
cout << "Heart의 생성자" << endl;
}
~Heart() {
cout << "Heart의 소멸자" << endl;
}
void Show() {
cout << "심장이 " << _purseRate << "로 뛰고 있습니다." << endl;
}
};
class Watch {
private:
string _color;
public:
Watch(string color)
: _color(color) {
cout << "Watch의 생성자" << endl;
}
~Watch() {
cout << "Watch의 소멸자" << endl;
}
string GetColor() {
return _color;
}
void DisplayCurrentTime() {
time_t timer;
struct tm* t;
timer = time(NULL);
t = localtime(&timer);
cout << _color << "Watch의 현재시각은 : " << t->tm_year + 1900 << "년 " << t->tm_mon << "월 "
<< t->tm_mday << "일 " << t->tm_hour << "시 " << t->tm_min << "분 입니다." << endl;
}
};
class Human {
private:
Heart _heart; // 포함관계 Composition (강한결합) // 포함한객체와 생명주기를 같이함.
Watch* _pWatch; // 참조 관계 agreggation(약한결합) // 포함한 객체와 생명주기를 같이 하지 않음.
public:
Human(float purseRate, Watch* pWatch)
:_heart(purseRate), _pWatch(pWatch)
{
cout << "Human 생성자" << endl;
}
~Human() {
cout << "Human 소멸자" << endl;
}
void ShowHeartRate() {
_heart.Show();
}
void ChangeWatch(Watch* pWatch) {
cout << _pWatch->GetColor() << "를 " << pWatch->GetColor() << "로 바꿔참." << endl;
_pWatch = pWatch;
}
void DisplayWatch() {
_pWatch->DisplayCurrentTime();
}
};
int main() {
Watch redWatch("빨간");
Watch blackWatch("검정");
cout << "-------------------------- 프로그램 시작 -------------------------------\n";
{
Human human(70, &redWatch);
human.ShowHeartRate();
human.DisplayWatch();
human.ChangeWatch(&blackWatch);
human.DisplayWatch();
}
cout << "-------------------------- 프로그래밍 종료 -----------------------------\n";
}