본문 바로가기

Technote/Data Structure C++42

[C++] 생성자와 소멸자 - 생성자의 필요성 p 라는 이름의 객체를 "생성과 동시에 초기화 하려고 한다. 초기화 하고자 하는 멤버들이 private으로 선언되어 있다. Person p 줄은 클래스의 내부가 아니라 외부에 해당되므로 접근이 허용되지 않는다. 멤버들을 public 으로 선언할수도 있으나 이는 정보 은닉에 위배 되므로 다른 경우를 택한다. #include using std::cout; using std::endl; using std::cin; const int SIZE=20; class Person { char name[SIZE]; char phone[SIZE]; int age; public: void ShowData(); }; void Person::ShowData() { cout 2009. 9. 20.
[C++] 캡슐화 2 #include using std::cout; using std::endl; using std::cin; class Point { int x; int y; public: int GetX(){return x;} int GetY(){return y;} void SetX(int _x); void SetY(int _y); void ShowData(); }; void Point::SetX(int _x) { if(_x100) { cout 2009. 9. 20.
[C++] 캡슐화의 기본 개념 캠슐화 : 관련 있는 데이터와함수를 하나의 단위로 묶는 것 관련 있는 데이터와 함수를 클래스 라는 하나의 캡슐내에 모두 정의 하는것 #include using std::cout; using std::cin; using std::endl; class Point { int x; int y; public: int GetX(){return x;} int GetY(){return y;} void SetX(int _x); void SetY(int _y); }; void Point::SetX(int _x) { if(_x100) { cout 2009. 9. 20.
[C++] 정보은닉 멤버 함수의 정의를 클래스 외부로 빼낼때, 클래스 내에 존재하는 멤버 함수의 선언에는 매개 변수의 타입과 개수에 대한 정보만 가지고 있어도 된다. void SetX(int); void SetY(int); #include using std::cout; using std::cin; using std::endl; class Point { int x; int y; public: int GetX(){return x;} int GetY(){return y;} void SetX(int _x); void SetY(int _y); }; void Point::SetX(int _x) { if(_x100) { cout 2009. 9. 20.