C++之虚函数
<span style="font-size:18px;">#include <iostream>
using namespace std ;
class AA
{
public:
int a ;
//虚函数
virtual void say_hello(void)
{
cout << "this is your parent " << endl ;
}
};
class BB : public AA
{
public:
int b ;
//假设子类没有实现虚函数,则多态中会调用父类的虚函数
//假设子类有又一次实现虚函数,则多态中会调用子类的虚函数
//__weak 类似
// void say_hello(void)
// {
// cout << "this is your son " << endl ;
// }
};
int main(void)
{
BB bb ;
AA * aa = NULL ;
aa = (AA *)&bb ;
aa->say_hello();
return 0 ;
}
</span>
执行结果:

浙公网安备 33010602011771号