728x90
1.
#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, int y, string color);
void setPoint(int x, int y) { move(x, y); }
void setColor(string color) { this->color = color; }
void show() {
cout << color << "색으로" << " (" << getX() << "," << getY() << ")에 위치한 점입니다." << endl;
}
};
ColorPoint::ColorPoint(int x, int y, string color) :Point(x, y) { this->color = color; }
int main()
{
ColorPoint cp(5, 5, "RED");
cp.setPoint(10, 20);
cp.setColor("BLUE");
cp.show();
}
2.
#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");
void setPoint(int x, int y) { move(x, y); }
void setColor(string color) { this->color = color; }
void show() {
cout << color << "색으로" << " (" << getX() << "," << getY() << ")에 위치한 점입니다." << endl;
}
};
ColorPoint::ColorPoint(int x, int y, string color) :Point(x, y) { this->color = color; }
int main()
{
ColorPoint zeroPoint;
zeroPoint.show();
ColorPoint cp(5, 5);
cp.setPoint(10, 20);
cp.setColor("BLUE");
cp.show();
}
반응형
'전공 공부 > C++' 카테고리의 다른 글
chp2 실습문제 1번 (0) | 2021.03.14 |
---|---|
C++ 2주차 강의 (0) | 2021.03.11 |
클래스 Circle, NamedCircle (클래스 상속) (0) | 2021.01.05 |
클래스 Book (프렌드 함수) (0) | 2021.01.05 |
클래스 ArrayUtility2 (두 배열의 뺄셈) (0) | 2021.01.04 |