测试一道题:



#include <iostream>

using namespace std;

class Parent
{
public:
    Parent(){}
    virtual void doit(){ cout << "this is parent" << endl;}
    virtual void printOut() { doit();}
    void printOut1(){doit();cout << "printOut1" << endl;}
};

class Son : public Parent
{
public:
    Son(){}
    void doit() { cout << "this is son" << endl;}
    void printOut() {doit(); cout << "this is son a " << endl;}
};

int main(int argc, char *argv[])
{
    Parent* p = new Son();
    p->doit();
    p->printOut();
    p->printOut1();
    return 0;
}

输出结果:
this is son
this is son
this is son a
this is son
printOut1

如果:
void printOut1(){doit();cout << "printOut1" << endl;}
改为:
void printOut1(){this->doit();cout << "printOut1" << endl;}
结果是一样的;此处考察的知识点是多态,不要误以为在积累中调用就不会调用到子类的函数; 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
  • 2021-08-25
猜你喜欢
  • 2022-02-13
  • 2022-12-23
  • 2021-10-18
  • 2022-12-23
  • 2021-07-26
  • 2021-12-07
  • 2022-12-23
相关资源
相似解决方案