return 0;
}
运行结果如下:
ZooAnimal Constructor
ToyAnimal Constructor
Character Constructor
BookCharacter Constructor
Bear Constructor
TeddyBear Constructor
TeddyBear Destructor
Bear Destructor
BookCharacter Destructor
Character Destructor
ToyAnimal Destructor
ZooAnimal Destructor
Terminated with return code 0
Press any key to continue ...
#include
class Character {
public:
Character() {
std::cout << "Character Constructor" << std::endl;
}
~Character() {
std::cout << "Character Destructor" << std::endl;
}
};
class BookCharacter: public Character {
public:
BookCharacter() {
std::cout << "BookCharacter Constructor" << std::endl;
}
~BookCharacter() {
std::cout << "BookCharacter Destructor" << std::endl;
}
};
class ZooAnimal {
public:
ZooAnimal() {
std::cout << "ZooAnimal Constructor" << std::endl;
}
~ZooAnimal() {
std::cout << "ZooAnimal Destructor" << std::endl;
}
};
class Bear: public virtual ZooAnimal {
public:
Bear() {
std::cout << "Bear Constructor" << std::endl;
}
~Bear() {
std::cout << "Bear Destructor" << std::endl;
}
};
class ToyAnimal {
public:
ToyAnimal() {
std::cout << "ToyAnimal Constructor" << std::endl;
}
~ToyAnimal() {
std::cout << "ToyAnimal Destructor" << std::endl;
}
};
class TeddyBear: public BookCharacter, public Bear, public virtual ToyAnimal {
public:
TeddyBear() {
std::cout << "TeddyBear Constructor" << std::endl;
}
~TeddyBear() {
std::cout << "TeddyBear Destructor" << std::endl;
}
};
int main () {
TeddyBear tb;
return 0;
}
运行结果如下:
ZooAnimal Constructor
ToyAnimal Constructor
Character Constructor
BookCharacter Constructor
Bear Constructor
TeddyBear Constructor
TeddyBear Destructor
Bear Destructor
BookCharacter Destructor
Character Destructor
ToyAnimal Destructor
ZooAnimal Destructor
Terminated with return code 0
Press any key to continue ...