티스토리 뷰

C++

정보은닉과 캡슐화

취뽀가자!! 2019. 3. 3. 18:10

정보은닉과 캡슐화

정보은닉

정보은닉은 멤버변수를 private로 선언하고, 해당 변수에 접근하는 함수를 별도로 정의해서, 안전한 형태로 멤버변수의 접근을 유도하는 것이다. 간단한 예제를 봐 보자.


헤더 파일

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef __POINT_H_
#define __POINT_H_
 
class Point
{
private:
    int x; 
    int y;    
 
public:
    bool InitMembers(int xpos, int ypos);
    int GetX() const;
    int GetY() const;
    bool SetX(int xpos);
    bool SetY(int ypos);
};
 
#endif
cs


소스 파일

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
#include <iostream>
#include "Point.h"
using namespace std;
 
bool Point::InitMembers(int xpos, int ypos)
{
    if(xpos<0 || ypos<0)
    {
        cout<<"벗어난 범위의 값 전달"<<endl;
        return false;
    }
    
    x=xpos;
    y=ypos;
    return true;
}
 
int Point::GetX() const {return x;}
int Point::GetY() const {return y;}
 
bool Point::SetX(int xpos)
{
    if(0>xpos || xpos>100)
    {
        cout<<"벗어난 범위의 값 전달"<<endl;
        return false;
    }
 
    x=xpos;
    return true;
}    
bool Point::SetY(int ypos)
{
    if(0>ypos || ypos>100)
    {
        cout<<"벗어난 범위의 값 전달"<<endl;
        return false;
    }
 
    y=ypos;
    return true;
}
cs


위 코드를 보면 함수명에 접두사 set을 붙인 함수들이 멤버변수에 잘못된 값을 입력하려 하면 출력된 메시지를 통해 잘못 된 입력을 방지 할 수 있다.


캡슐화

캐슐화는 정보은닉과 유사하여 그 차이를 인식하기 어렵다. 캡슐화를 설명하기 전에 감기약에 비유해 보겠다. 예를 들어 감기가 걸려 먹어야 하는 약이 3가지가 있는데, 이 세 가지의 약 복용 순서를 지키지 않으면 부작용이 있다고 한다. 따라서 약을 먹을 때 항상 순서를 지켜야 한다. 하지만 3가지의 감기약들을 캡슐 하나에 넣어 알약 형태를 만들어 두어 복용하면 복용 순서를 지키지 않아도 되고, 부작용의 위험도 없어 좋지 않겠는가? 바로 이런 알약을 c++에서는 캡슐화 했다고 할 수 있다.

아래 예제를 보면서 이해해 보자.

캡슐화 하기 전 소스코드

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
#include <iostream>
using namespace std;
 
class SinivelCap   
{
public
    void Take() const {cout<<"콧물이 멈춘다."<<endl;}
};
 
class SneezeCap   
{
public
    void Take() const {cout<<"재채기가 멈춘다."<<endl;}
};
 
class SnuffleCap   
{
public
    void Take() const {cout<<"코가 뚫린다.."<<endl;}
};
 
class ColdPatient
{
public:
    void TakeSinivelCap(const SinivelCap &cap) const {cap.Take();}
    void TakeSneezeCap(const SneezeCap &cap) const {cap.Take();}
    void TakeSnuffleCap(const SnuffleCap &cap) const{cap.Take();}
};
 
int main(void)
{
    SinivelCap scap;
    SneezeCap zcap;
    SnuffleCap ncap;
    
    ColdPatient sufferer;
    sufferer.TakeSinivelCap(scap);
    sufferer.TakeSneezeCap(zcap);
    sufferer.TakeSnuffleCap(ncap);
    return 0;
}
cs

캡슐화 후 소스코드

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
#include <iostream>
using namespace std;
 
class SinivelCap    
{
public
    void Take() const {cout<<"콧물이 멈춘다.."<<endl;}
};
 
class SneezeCap    
{
public
    void Take() const {cout<<"재채기가 멈춘다."<<endl;}
};
 
class SnuffleCap   
{
public
    void Take() const {cout<<"코가 뚫린다."<<endl;}
};
 
class CONTAC600  //캡슐화
{
private//객체를 멤버변수로 선언
    SinivelCap sin; 
    SneezeCap sne;
    SnuffleCap snu;
 
public:
    void Take() const
    {
        sin.Take();
        sne.Take();
        snu.Take();
    }
};
 
class ColdPatient
{
public:
    void TakeCONTAC600(const CONTAC600 &cap) const { cap.Take(); }
};
 
int main(void)
{
    CONTAC600 cap;
    ColdPatient sufferer;
    sufferer.TakeCONTAC600(cap);
    return 0;
}
cs

 



----------------------------------------------

이 글은 열혈 c++을 보고 정리한 글입니다.

'C++' 카테고리의 다른 글

const 이해하기  (2) 2020.11.19
inline 함수  (0) 2019.02.24
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함