多态

2019-05-10  本文已影响0人  ustclcl

多态 polymorphism

基类成员函数添加了virtual关键字,该函数在执行时绑定。否则在编译时绑定。所以

基类希望其派生类进行覆盖的函数,定义为虚函数

例子

#include <iostream>
using namespace std;

class Animal{
public:
  void sound(){
    cout << "??" << endl;
  }
};



class Cat : public Animal{
public:
  void sound(){
    cout << "Miao Miao" << endl;
  }
};

class Dog : public Animal{
public:
  void sound(){
    cout << "Wang Wang" << endl;
  }
};

int main(int argc, char **argv){
  Animal *animal;
  animal = new Cat;
  animal->sound();
  animal = new Dog;
  animal->sound();
}

输出

lcl@FX50JK:~/Documents/hub/learning-record/cpp$ ./polymorphism
??
??

可见animal指针虽然指向子类Cat和Dog,但是并没用他们的sound方法。基类使用virtual关键字

class Animal{
public:
  virtual void sound(){
    cout << "??" << endl;
  }
};

输出

lcl@FX50JK:~/Documents/hub/learning-record/cpp$ ./polymorphism
Miao Miao
Wang Wang

上一篇下一篇

猜你喜欢

热点阅读