c++ 虚拟方法

From , 3 Years ago, written in C++, viewed 99 times.
URL https://pastebin.vip/view/537de305
  1. #include <iostream>
  2. using std::cout;
  3.  
  4. class Mammal {
  5. public:
  6.         Mammal() :
  7.                         itsAge(1) {
  8.                 cout << "Mammal constructor...\n";
  9.         }
  10.         virtual ~Mammal() {
  11.                 cout << "Mammal destructor...\n";
  12.         }
  13.         void Move() const {
  14.                 cout << "Mammal move one step\n";
  15.         }
  16.         virtual void Speak() const {
  17.                 cout << "Mammal speak!\n";
  18.         }
  19.  
  20. protected:
  21.         int itsAge;
  22. };
  23.  
  24. class Dog: public Mammal {
  25. public:
  26.         Dog() {
  27.                 cout << "Dog Constructor...\n";
  28.         }
  29.         virtual ~Dog() {
  30.                 cout << "Dog destructor...\n";
  31.         }
  32.         void WagTail() {
  33.                 cout << "Wagging Tail...\n";
  34.         }
  35.         void Speak() const {
  36.                 cout << "Woof!\n";
  37.         }
  38.         void Move() const {
  39.                 cout << "Dog moves 5 steps...\n";
  40.         }
  41. };
  42.  
  43. int main() {
  44.         Mammal *pDog = new Dog;
  45.         pDog->Move();
  46.         pDog->Speak();
  47.  
  48.         return 0;
  49. }
  50.  

Reply to "c++ 虚拟方法"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website