狗的定义和使用(继承中的构造和析构).cpp 475 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <iostream>
  2. using namespace std;
  3. class Mammal {
  4. public:
  5. Mammal() {
  6. cout << "Mammal constructor" << endl;
  7. }
  8. virtual ~Mammal() {
  9. cout << "Mammal destructor" << endl;
  10. }
  11. };
  12. class Dog : public Mammal {
  13. public:
  14. Dog() {
  15. cout << "Dog constructor" << endl;
  16. }
  17. virtual ~Dog() {
  18. cout << "Dog destructor" << endl;
  19. }
  20. void speak() {
  21. cout << "wang wang" << endl;
  22. }
  23. };
  24. void creating() {
  25. Dog dog;
  26. dog.speak();
  27. }
  28. int main() {
  29. creating();
  30. return 0;
  31. }