virtual关键字在子类

运行结果是什么?

]

father *pf = new father();
pf->f();
delete pf;
father *ps = new son();
ps->f();
delete ps;
son *ps1 = new son();
ps1->f();
delete ps1;
son *pss = new sonson();
pss->f();
delete pss;

运行结果

father::f()
father::f()
son::f()
sonson::f()

解释与结论

id 指针名 指针的类型 实际的类型 运行结果 解释
1 pf father father father::f()
2 ps father son father::f() 实际调用的是指针的类型的函数,说明没有多态
3 ps1 son son son::f()
4 pss son sonson sonson::f() 实际调用的是对象的类型的函数,说明有多态的效果

结论:

  1. 如果virtual只写在派生类中,而没有写在基类中,则不会有多态的效果

  2. 如果在某一层的派生类中加了virtual标签,那么从这一层开始以后的每一层,这个函数都会有多态的效果。

其它测试

virtual关键字在父类

析构函数的virtual在子类

间接调用虚函数

完整代码

#include <iostream>
using namespace std;

class father
{
public:
  void f()
  {
    cout<<"father::f()"<<endl;
  }
};

class son : public father
{
public:
  virtual void f()
  {
    cout<<"son::f()"<<endl;
  }
};

class sonson : public son
{
public:
  void f()
  {
    cout<<"sonson::f()"<<endl;
  }
};

int main()
{
    father *pf = new father();
    pf->f();
    delete pf;
    father *ps = new son();
    ps->f();
    delete ps;
    son *ps1 = new son();
    ps1->f();
    delete ps1;
    son *pss = new sonson();
    pss->f();
    delete pss;
    return 0;
}
发布了407 篇原创文章 · 获赞 328 · 访问量 111万+

猜你喜欢

转载自blog.csdn.net/mishifangxiangdefeng/article/details/100583967