Demo:
class Animal
{
public:
Animal()
{
cout << "animal..." << endl;
}
virtual ~Animal()
{
cout << "~animal..." << endl;
}
virtual void ShowAnimal() = 0;
};
class Cat
{
public:
Cat()
{
cout << "Cat..." << endl;
}
virtual ~Cat()
{
cout << "~Cat..." << endl;
}
virtual void ShowCat() = 0;
};
class Tiger :public Animal, public Cat
{
public:
Tiger()
{
cout << "Tiger..." << endl;
}
~Tiger()
{
cout << "~Tiger..." << endl;
}
void ShowAnimal() override
{
cout << "老虎是动物..." << endl;
}
void ShowCat() override
{
cout << "老虎是猫科类..." << endl;
}
void ShowTiger()
{
cout << '\n';
cout << "老虎是什么?" << endl;
ShowAnimal();
ShowCat();
cout << '\n';
}
};
int main()
{
Cat *cat = new Tiger();
Tiger *tiger = dynamic_cast<Tiger *>(cat);
tiger->ShowTiger();
delete cat;
system("pause");
return EXIT_SUCCESS;
}
输出:
animal...
Cat...
Tiger...
老虎是什么?
老虎是动物...
老虎是猫科类...
~Tiger...
~Cat...
~animal...
可见delete cat;
时,两个基类都被析构了。