C++ 포함
사실 포함을 설명하는 이유는 다음에 이어갈 상속과 관련해서 볼 때 도움이 될꺼같아서 이렇게 포스팅을 합니다....
객체가 다른 객체를 멤버변수로 사용하고 있는 것을 포함 이라고 합니다
/*
Class 포함 예제
made by hahahia
*/
#include<iostream>
using namespace std;
class point{ /* x,y 좌표를 나타내는 간단한 클래스 */
private:
int x,y;
public:
point(int _x,int _y): x(_x),y(_y) {}; /* C++ Style(= {x = _x; y = _y;} */
int getX() const { return x; }
int getY() const { return y; }
};
class rectangle{ // rectangle class(사실 이 클래스도 간단하네요....ㅎㅎ)
private:
point topLeft;
point bottomRight; //point 객체를 포함
public:
rectangle(int a, int b, int c, int d) : topLeft(a,b), bottomRight(c,d){};
void print(){
cout << "Top Left : (" << topLeft.getX() << ", " << topLeft.getY() << ")\n";
cout << "Bottom Right : (" << bottomRight.getX() << ", " << bottomRight.getY() << ")\n";
}
};
int main(){
rectangle myRec(1,2,3,4);
myRec.print();
return 0;
}
실행결과