main()에서 Rectangle r;이라는 코드가 실행할 수 있도록 Rectangle 클래스에 코드를 추가하는 것인데 무엇을 하는게 좋을까요 ?
조회수 892회
class Point{
public:
int x, y;
Point(){}
Point(int xx, int yy) { x = xx; y = yy; }
};
class Rectangle{
Point p;
int width, height;
public:
Rectangle(int x, int y, int w, int h) {
p.x = x; p.y = y; width = w; height = h;
}
};
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
1 답변
-
class Point { public: int x, y; Point() {} Point(int x, int y) { this->x = x; this->y =y; } }; class Rectangle: private Point { Point p; int width, height; public: Rectangle(); Rectangle(int x, int y, int w, int h) { p.x = x; p.y = y; width = w; height = h; } }; Rectangle::Rectangle() { x = 1, y = 2, width = 3, height = 4; cout << x << " " << y << " " << width << " " << height << endl; }
여기서 봤을 때 뚜루뚜뚜님께서는 Point라는 클래스를 상속 받아서 Rectangle에서 그 값을 입력받고 싶으신 것 같습니다. 그런 다음에 Rectangle에 추가적으로 포인트의 값과 길이와 높이를 받고 싶은 것 같습니다. main() 함수에서 수행하고 싶으시다면 생성자인 Rectangle();을 생성한 후에 Rectangle::Rectangle()로 되어 있는 구문을 생성해서 만들면 수행이 될 겁니다. 참고로 Point 문을 Point(int x, int y) { this->x = x; this->y =y; }로 바꾸면 자기 자신이 호출되어서 편하게 사용할 수 있습니다. 이걸 this 포인터라고 합니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력