#include<iostream>
using namespace std;
class CParent
{
public:
void print1();
virtual void print2();
};
class CChild:public CParent
{
public:
void print1();
virtual void print2();
};
void CParent::print1()
{
printf("wo shi parent print1\n");
}
void CParent::print2()
{
printf("wo shi parent print2\n");
}
void CChild::print1()
{
printf("wo shi child print1\n");
}
void CChild::print2()
{
printf("wo shi child print2\n");
}
int main()
{
CParent p;
CChild c;
CParent *p1;
CChild *c1;
p1=dynamic_cast<CParent *>(&c);
p1->print1();
p1->print2();
return 0;
}
结果图:
虚函数,运行时,动态绑定