class Animal {
public:
Animal()
{
cout << "Animal constructing" << endl;
}
virtual ~Animal() {
cout << "Animal destructing" << endl;
}
virtual void MakeSound() const = 0 {}
};
class Cat :public Animal {
public:
Cat() {
cout << "Cat constructing" << endl;
}
~Cat() {
cout << "Cat destructing" << endl;
}
virtual void MakeSound() const{
cout << "Cat make sound." << endl;
Animal::MakeSound();
}
};
int main() {
Animal * pcat = new Cat;
pcat->MakeSound();
delete pcat;//如果基类的析构函数里没有使用 virtual ,这里会导致Cat的析构函数无法执行。
return 0;
}