#include <iostream>
#include <cstdlib>
using namespace std;
class A
{
public:
int a=10;
//void print() ---------------------------------方案1
virtual void print() ------------------------------方案2
{
cout << "it is A" << endl;
}
};
class B:A
{
public:
int b=20;
void print()
{
cout << "it is B" << endl;
}
};
int main()
{
B bb;
A a;
B * b=&bb;
b->print();
b=(B*)&a;
b->print();
//cout << "Hello world!" << endl;
return 0;
}
result:
方案1:
it is B
it is B
方案2:
it is B
it is A
在链表或动态数组中存储数据类型选择基类的指针来存贮堆空间的数据,派生类的数据地址也可以存储在基类中。
virtual关键字一定要加在基类函数前面,因为虚函数指针是在基类头部存放的。派生类中可加可不加
作用:
如果函数的功能类似.基类,各个派生类都可以根据自身设计相应的函数(重写,覆盖函数)。
程序的后面省掉了判断,虚函数自动识别类型,大大的方便了.
1、虚函数:虚函数一定是重写函数,在基类重写函数前加virtual关键字就是虚函数。
a)类的对象:使用基类和派生类的对象调用重写函数,用什么对象调用就调用什么类的重写函数。
b)基类指针:当调用普通重写函数时,是什么类指针调用就执行什么类重写函数。
c)基类指针:当调用虚拟函数时,指针指向的是哪个类的对象的地址,就调用哪个类的重写函数。
d)基类引用变量:当调用普通重写函数时,调用基类的普通重写函数。
e)基类引用变量:当调用虚拟函数时,引用的是哪个类的对象,就调用哪个类的重写函数。
+int main()
{
CBase b, *pb;
CDerived d;
TestByObj(d);
b.RealFunc(); //CBase
b.VirtualFunc(); //CBase
d.RealFunc(); //CDerive
d.VirtualFunc(); //CDerive
pb = &b;
pb->RealFunc(); //CBase
pb->VirtualFunc(); //CBase
pb = &d; //
pb->RealFunc(); //CBase
pb->VirtualFunc();//CDerive
CBase &b1 = b;
b1.RealFunc(); //CBase
b1.VirtualFunc();//CBase
CBase &b2 = d;
b2.RealFunc(); // CBase
b2.VirtualFunc(); //CDerive
TestByPointer();
TestByRef();
TestByObj();
return 0;
}
void TestByPointer(CBase *pb) //&d
{
pb->RealFunc(); //CBase
pb->VirtualFunc();//CDerived
}
void TestByRef(CBase &bRef) //&d
{
bRef.RealFunc(); //CBase
bRef.VirtualFunc(); //CDerive
}
void TestByObj(CBase b) // d 重新建立了一个对象
{
b.RealFunc(); //CBase
b.VirtualFunc(); //CBase
}