基类与子类的调用关系:
子类与基类的构造函数的调用:先调用基类的构造函数然后在返回到子类的构造函数中调用对基类指针但是实质为子类的虚函数的调用:如果是正常函数就调用基类的,如果是虚函数就调用子类的
#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;
}
运行结果
转载请注明原文地址: https://lol.8miu.com/read-6119.html