명품C++프로그래밍
명품 C++ Programming 실습문제 8장 4번
anycoding
2021. 5. 21. 22:20
반응형
문제 3, 4번에 적용되는 2차원 상의 한 점을 표현하는 Point 클래스가 있다.
class Point {
int x, y;
public:
Point(int x, int y) { this->x = x; this->y = y; }
int getX() { return x; }
int getY() { return y; }
protected:
void move(int x, int y) {
this->x = x; this->y = y;
}
};
문제
다음 main() 함수가 실행되도록 Point 클래스를 상속받는 ColorPoint 클래스를 작성하고, 전체 프로그램을 완성하라.
int main() {
ColorPoint zeroPoint;
zeroPoint.show();
ColorPoint cp(5, 5);
cp.setPoint(10, 20);
cp.setColor("BLUE");
cp.show();
}
결과
BLACK색으로 ( 0, 0 )에 위치한 점입니다.
BLUE색으로 ( 10, 20 )에 위치한 점입니다.
소스코드
#include<iostream>
#include<string>
using namespace std;
class Point {
int x, y;
public:
Point(int x, int y) { this->x = x; this->y = y; }
int getX() { return x; }
int getY() { return y; }
protected:
void move(int x, int y) { this->x = x; this->y = y; }
};
class ColorPoint : public Point {
string color;
public:
ColorPoint(int x = 0, int y = 0, string color = "BLACK") : Point(x, y) {
this->color = color;
}
void show();
void setPoint(int x, int y);
void setColor(string color);
};
void ColorPoint::show()
{
cout << color << "색으로 ( " << getX() << ", " << getY() << " )에 위치한 점입니다." << endl;
}
void ColorPoint::setPoint(int x, int y)
{
move(x, y);
}
void ColorPoint::setColor(string color)
{
this->color = color;
}
int main() {
ColorPoint zeroPoint;
zeroPoint.show();
ColorPoint cp(5, 5);
cp.setPoint(10, 20);
cp.setColor("BLUE");
cp.show();
}
반응형