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

1234567891011121314151617181920212223242526272829303132333435
  1. #include <iostream>
  2. using namespace std;
  3. class Mammal {
  4. public:
  5. Mammal() {
  6. cout<<"Mammal constructor\n";
  7. }
  8. virtual ~Mammal () {
  9. cout<<"Mammal destructor\n";
  10. }
  11. virtual void speak() const {
  12. cout<<"Mammal speak!\n";
  13. }
  14. };
  15. class Dog: public Mammal {
  16. public:
  17. Dog() {
  18. cout<<"Dog constructor\n";
  19. }
  20. ~Dog() {
  21. cout<<"Dog destructor\n";
  22. }
  23. void speak() const {
  24. cout<<"wang wang\n";
  25. }
  26. };
  27. int main() {
  28. Mammal*pDog=new Dog ;
  29. pDog->speak();
  30. delete pDog;
  31. return 0;
  32. }