-
열혈 C++ 프로그래밍 (클래스 완성)Language/C 2023. 8. 19. 18:45
<정보은닉>
//Point.h #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 // !__POINT_H_
//Point.cpp #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; //Point::x y = ypos; //Point::y 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; }
//Rectangle.h #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 // !__Rectangle_H_
//Rectangle.cpp #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; }
//RectangleFaultFind.cpp #include <iostream> #include "Point.h" #include "Rectangle.h" using namespace std; int main(void) { Point pos1; if (!pos1.InitMembers(-2, 4)) cout << "초기화 실패" << endl; if (!pos1.InitMembers(2, 4)) cout<< "초기화 실패" << endl; Point pos2; if (!pos2.InitMembers(5, 9)) cout << "초기화 실패" << endl; Rectangle rec; if (!rec.InitMembers(pos2, pos1)) cout << "직사각형 초기화 실패" << endl; if (!rec.InitMembers(pos1, pos2)) cout << "직사각형 초기화 실패" << endl; rec.ShowRecInfo(); return 0; }
맴버변수를 private으로 선언하고, 해당 변수에 접근하는 함수를 별도로 정의해서, 안전한 형태로 맴버변수의 접근을 유도하는 것이 바로 '정보은닉' 이다.
헤더 Point.h와 Rectangle.h에 선언된 다음 함수들에는 const 선언이 추가되어있는데,
int GetX() const; int GetY() const; void ShowRecInfo() const;
이 const는 "이 함수 내에서는 맴버변수에 저장된 값을 변경하지 않겠다." 라는 뜻이다.
const 함수 내에서는 const가 아닌 함수의 호출이 제한된다.
<캡슐화>
//Encaps2.cpp #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() { CONTAC600 cap; ColdPatient sufferer; sufferer.TakeCONTAC600(cap); return 0; }
'Language > C' 카테고리의 다른 글
열혈 C++ 프로그래밍 (클래스 기본) (0) 2023.08.15 열혈 C++ 프로그래밍 (참조자 이해하기) (0) 2023.08.14 열혈 C++ 프로그래밍 (Chapter01 문제풀이) (0) 2023.08.13 C 재귀호출 (하노이탑) (0) 2023.07.30 백준 10809번 : 알파벳 찾기 (0) 2021.06.10