純粋仮想関数は仮想関数の宣言の最後に =0という指定をつけたもの
純粋仮想関数を1つでも持つ抽象クラスはオブジェクトを作成できない。
class Vehicle {
protected:
int speed;
public:
void setSpeed(int s);
virtual void show() = 0;
};
class Car : public Vehicle {
private:
int num;
double gas;
public:
Car(int n, double g);
void show();
};
class RacingCar : public Car {
private:
int course;
public:
RacingCar();
void setCourse(int c);
void show();
};
class Plane : public Vehicle {
private:
int flight;
public:
Plane(int f);
void show();
};
void Vehicle::setSpeed(int s){
speed = s;
cout << "速度を" << speed << "にしました。\n";
}
Car::Car(int n, double g) {
num = n;
gas = g;
cout << "ナンバー" << num << "ガソリン量" << gas << "の車を作成しました。\n";
}
void Car::show(){
cout << "車のナンバーは" << num << "です。\n";
cout << "ガソリン量は" << gas << "です。\n";
cout << "速度は" << speed << "です。\n";
}
Plane::Plane(int f) {
flight = f;
cout << "便" << flight << "の飛行機を作成しました。\n";
}
void Plane::show() {
cout << "飛行機の便は" << flight << "です。\n";
cout << "速度は" << speed << "です。\n";
}
int main() {
Vehicle* pVc[2];
Car car1(1234, 20.5);
pVc[0] = &car1;
pVc[0]->setSpeed(60);
Plane pln1(232);
pVc[1] = &pln1;
pVc[1]->setSpeed(500);
for(int i=0; i<2; i++){
pVc[i]->show();
}
return 0;
}
typeid演算子でオブジェクトのclassを調べることができる。
ヘッダにtypeinfoをインクルードする
int main() {
Vehicle* pVc[2];
Car car1(1234, 20.5);
pVc[0] = &car1;
Plane pln1(232);
pVc[1] = &pln1;
for(int i=0; i<2; i++) {
if(typeid(*pVc[i]) == typeid(Car))
cout << (i+1) << "番目は" << typeid(Car).name() << "です。\n";
else
cout << (i+1) << "番目は" << typeid(Car).name() << "ではありません。" << typeid(*pVc[i]).name() <<"です。\n";
}
return 0;
}
$ g++ -o sample sample.cpp && ./sample
ナンバー1234ガソリン量20.5の車を作成しました。
便232の飛行機を作成しました。
1番目は3Carです。
2番目は3Carではありません。5Planeです。
typeid(Car).name()だとoutputがおかしい。