listr302.cpp

来自「teach yourself C++ in 21 days 第五版」· C++ 代码 · 共 44 行

CPP
44
字号
#include<iostream>
using namespace std;
class Animal
{
  public:
    virtual void Speak() 
       { cout << "Animal  Speaks" << endl; }
};

class Dog : public Animal
{
  public:
    void Speak() { cout << "Dog Speaks" << endl; }
};


class Cat : public Animal
{
  public:
    void Speak() { cout << "Cat Speaks" << endl; }
};

void DoIt(Cat*);
void DoIt(Dog*);

int main()
{
   Animal * pA = new Dog;
   DoIt(pA);
   return 0;
}

void DoIt(Cat * c)
{
   cout << "They passed a cat!" << endl << endl;
   c->Speak();
}

void DoIt(Dog * d)
{
   cout << "They passed a dog!" << endl << endl;
   d->Speak();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?