探究基类与子类之间的调用关系

it2023-06-23  86

基类与子类的调用关系: 子类与基类的构造函数的调用:先调用基类的构造函数然后在返回到子类的构造函数中调用对基类指针但是实质为子类的虚函数的调用:如果是正常函数就调用基类的,如果是虚函数就调用子类的#include <iostream> using namespace std; class Base{ public: Base(){ cout<<"this is Base's Constroctor"; cout<<endl; } void fun1(){ cout<<"this is fun1()"<<endl; } virtual void fun2(){ cout<<"this is fun2()"<<endl; } }; class A:public Base{ public: A() { cout << "this is A's constuctor" << endl; } void fun1() { cout << "A 's fun1()" << endl; } virtual void fun2() { cout << "A's fun2()" << endl; } }; int main(){ Base *base; base=new A();//与构造函数有关但是与 base->fun1(); base->fun2(); system("pause"); return 0; } /* 输出结果: this is fun1()-->调用 A's fun2() */ 运行结果
最新回复(0)