class Point {
public:
int x_cor, y_cor;
void move(int dx, int dy) {
x_cor += dx;
y_cor += dy;
}
void print() {
cout << "(" << x_cor << ", " << y_cor << ")";
}
};
大域解決演算子(::)を使ったもの。
class Point {
public:
int x_cor, y_cor;
void move(int dx, int dy);
void print(void);
};
void Point::move(int dx, int dy) {
x_cor += dx;
y_cor += dy;
}
void Point::print() {
cout << "(" << x_cor << ", " << y_cor << ")";
}
プログラム
#include <iostream.h>
// このあいだに 上の Pointクラスの定義を入れる
int main() {
Point p; // クラス名がそのまま型名
Point *pt = &p; // 説明のため無理矢理ポインタにする
// こちら側はポインタを使った同じ処理
p.x_cor = 10; // pt->x_cor = 10;
p.y_cor = 20; // pt->y_cor = 20;
p.move(1, -1); // pt->move(1, -1);
p.print(); // pt->print();
cout << endl;
}
class ColorPoint : public Point {
public:
char color; // 0-黒 1-赤 2-緑 3-黄 4-青 5-紫 6-水 7-白
void print(void);
};
void ColorPoint::print() {
cout << "\033[" << color+30 << "m";
cout << "(" << x_cor << ", " << y_cor << ")";
cout << "\033[0m";
}
class ColorPoint : public Point {
private:
char color; // 0-黒 1-赤 2-緑 3-黄 4-青 5-紫 6-水 7-白
public:
void print(void);
void setColor(char c);
char getColor(void);
};
class Point {
public:
int x_cor, y_cor;
void move(int dx, int dy);
virtual void print(void);
};