検討:
class Mobile {
double memory_util;
public:
virtual void power_save(double duration) = 0;
};
class Laptop : public Mobile {
bool is_unlocked;
protected:
bool is_charged;
public:
void power_save(double duration);
virtual double remaining_time();
};
class NegativeNumber {};
class IPad : public Laptop {
int generation;
public:
void power_save(double duration);
bool isJailBroken();
};
class HPLaptop : public Laptop {
int warranty_years;
public:
void extend_warranty(int years);
};
class HPdv6 : public HPLaptop {
bool repaired;
public:
double remaining_time(){ return HPLaptop::remaining_time(); }
bool is_repaired { return repaired; }
};
そして、あなたは次のことをしたいと思っていました:
int main () {
Mobile* d = new HPdv6();
Laptop *s = d;
d->power_save(100);
cout << “remaining operation time: ” <<
s->remaining_time() << endl;
return 0;
}
ここで実際に呼び出されるメソッドはどれですか? Mobile が仮想関数であることは理解していますが、このようなポインターがある場合にクラス階層を処理する方法がわかりません。さまざまな継承されたクラスを扱う問題を理解しやすくする、クラス階層に関するヒントはありますか?
ありがとうございました。