-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc04221.cpp
More file actions
53 lines (43 loc) · 874 Bytes
/
src04221.cpp
File metadata and controls
53 lines (43 loc) · 874 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
#include "stdafx.h"
#include <iostream>
using namespace std;
// 초기 제작자
class CMyData
{
public:
int GetData() { return m_nData; }
virtual void SetData(int nParam) { m_nData = nParam; }
private:
int m_nData = 0;
};
// 후기 제작자
class CMyDataEx : public CMyData
{
public:
// 파생 클래스에서 기본 클래스의 메서드를 재정의했다.
virtual void SetData(int nParam)
{
// 입력 데이터의 값을 보정하는 새로운 기능을 추가한다.
if (nParam < 0)
CMyData::SetData(0);
if (nParam > 10)
CMyData::SetData(10);
}
};
// 사용자 코드
int main()
{
/*// 구형에서는 값을 보정하는 기능이 없다.
CMyData a;
a.SetData(-10);
cout << a.GetData() << endl;
// 새로 만든 신형에는 값을 보정하는 기능이 있다.
CMyDataEx b;
b.SetData(15);
cout << b.GetData() << endl;*/
CMyDataEx a;
CMyData& rdata = a;
rdata.SetData(15);
cout << rdata.GetData() << endl;
return 0;
}