C++ Chapter 04. 클래스의 완성.


04-1 정보은닉(Information Hiding)

* 정보은닉의 이해


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
#include<iostream>
using namespace std;
 
class Point
{
public:
    int x;   // x좌표의 범위는 0이상 100이하.
    int y;   // y좌표의 범위는 0이상 100이하.
};
 
class Rectangle
{
public:
    Point upLeft; // pos2(5, 9)
    Point lowRight; // pos1(-2, 4)
 
public:
    void ShowRecInfo()
    {
        cout<<"좌 상단: "<<'['<<upLeft.x<<", ";
        cout<<upLeft.y<<']'<<endl;
        cout<<"우 하단: "<<'['<<lowRight.x<<", ";
        cout<<lowRight.y<<']'<<endl<<endl;
    }
};
 
int main(void)
{
    Point pos1={-24};
    Point pos2={59};
    Rectangle rec={pos2, pos1};
    rec.ShowRecInfo();
    return 0;
}
 
cs




*Rectangle 객체의 이해


1. 제한된 방법으로의 접근만 허용을 해서 잘못된 값이 저장되지 않도록 도와야 하고,

2. 또 실수를 했을 때, 실수가 쉽게 발견되도록 해야 한다


Point.h // Point 클래스의 선언
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



Point.cpp // Point 클래스의 정의

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;} // const 함수 (엑세스 함수)
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


* Point 클래스의 정보은닉 결과

1
2
3
4
5
int GetX() const;
bool SetX(int xpos);
 
int GetY() const;
bool SetY(int ypos);
cs

// 엑세스 함수(access function) : 멤버변수를 private으로 선언하면서 클래스 외부에서의 멤버변수의 접근을 목적으로 정의되는 함수들



* Rectangle 클래스의 정보은닉 결과


Rectangle.h // Rectangle 클래스의 선언

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef __RECTANGLE_H_
#define __RECTANGLE_H_
 
#include "Point.h"
 
class Rectangle
{
private:
    Point upLeft;
    Point lowRight;
 
public:
    bool InitMembers(const Point &ul, const Point &lr);
    void ShowRecInfo() const;
};
 
#endif
cs


Rectangle.cpp // Rectangle 클래스의 정의

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include "Rectangle.h"
using namespace std;
 
bool Rectangle::InitMembers(const Point &ul, const Point &lr)
{
    if(ul.GetX()>lr.GetX() || ul.GetY()>lr.GetY())
    {
        cout<<"잘못된 위치정보 전달"<<endl;
        return false;
    }
 
    upLeft=ul;
    lowRight=lr;
    return true;
}
 
void Rectangle::ShowRecInfo() const
{
    cout<<"좌 상단: "<<'['<<upLeft.GetX()<<", ";
    cout<<upLeft.GetY()<<']'<<endl;
    cout<<"우 하단: "<<'['<<lowRight.GetX()<<", ";
    cout<<lowRight.GetY()<<']'<<endl<<endl;
}
cs


RectangleFaultFind.cpp // 실행을 위한 main 함수의 정의

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
#include<iostream>
#include "Point.h"
#include "Rectangle.h"
using namespace std;
 
int main(void)
{
    Point pos1;
    if(!pos1.InitMembers(-24))
        cout<<"초기화 실패"<<endl;
    if(!pos1.InitMembers(24))
        cout<<"초기화 실패"<<endl;
 
    Point pos2;
    if(!pos2.InitMembers(59))
        cout<<"초기화 실패"<<endl;
 
    Rectangle rec;
    if(rec.InitMembers(pos2, pos1))
        cout<<"직사각형 초기화 실패"<<endl;
 
    if(rec.InitMembers(pos1, pos2))
        cout<<"직사각형 초기화 실패"<<endl;
    
    rec.ShowRecInfo();
    return 0;
}
cs



* const 함수





Posted by 너래쟁이
: