-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc04232.cpp
More file actions
60 lines (51 loc) · 1010 Bytes
/
src04232.cpp
File metadata and controls
60 lines (51 loc) · 1010 Bytes
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
#include "stdafx.h"
#include <iostream>
using namespace std;
// 초기 제작자의 코드
class CMyObject
{
public:
CMyObject() { }
virtual ~CMyObject() { }
// 모든 파생 클래스는 이 메서드를 가졌다고 가정할 수 있다.
virtual int GetDeviceID() = 0;
protected:
int m_nDeviceID;
};
// 초기 제작자가 만든 함수
void PrintID(CMyObject *pObj)
{
// 실제로 어떤 것일지는 모르지만 그래도 ID는 출력할 수 있다!
cout << "Device ID: " << pObj->GetDeviceID() << endl;
}
// 후기 제작자의 코드
class CMyTV : public CMyObject
{
public:
CMyTV(int nID) { m_nDeviceID = nID; }
virtual int GetDeviceID()
{
cout << "CMyTV::GetDeviceID()" << endl;
return m_nDeviceID;
}
};
class CMyPhone : public CMyObject
{
public:
CMyPhone(int nID) { m_nDeviceID = nID; }
virtual int GetDeviceID()
{
cout << "CMyPhone::GetDeviceID()" << endl;
return m_nDeviceID;
}
};
// 사용자 코드
int main()
{
CMyTV a(5);
CMyPhone b(10);
// 실제 객체가 무엇이든 알아서 자신의 ID를 출력한다.
::PrintID(&a);
::PrintID(&b);
return 0;
}