#pragma once // 04.h
#include
using namespace std;
class Point { // 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 { // Point를 상속받은 ColorPoint
string name;
public:
ColorPoint(int x=0, int y=0, string name="BLACK"); // 디폴트 매개 변수로 선언한 생성자
void setPoint(int x, int y);
void setColor(string name);
void show(); // 출력
};
#include // 04.cpp
#include
#include"04.h"
using namespace std;
ColorPoint::ColorPoint(int x, int y, string name) :Point(x, y) { // Point를 상속받은 ColorPoint 생성자
this->name = name;
}
void ColorPoint::setPoint(int x, int y) {
Point::move(x, y);
}
void ColorPoint::setColor(string name) {
this->name = name;
}
void ColorPoint::show() { // 출력
cout << this->name << "색으로 " << "(" << Point::getX() << ", " << Point::getY() << ")에 위치한 점입니다." << endl;
}
#include // 04_main.cpp
#include
#include"04.h"
using namespace std;
int main() {
ColorPoint zeroPoint;
zeroPoint.show();
ColorPoint cp(5, 5, "RED"); // ColorPoint 타입의 cp선언
cp.setPoint(10, 20);
cp.setColor("BLUE");
cp.show();
}