11. 运算符成员函数的设计双目运算符 B
如果要重载 B 为类成员函数,使之能够实现表达式 oprd1 B oprd2,其中 oprd1 为A 类对象,则 B 应被重载为 A 类的成员函数,形参类型应该是 oprd2 所属的类型。
经重载后,表达式 oprd1 B oprd2 相当于 oprd1.operator B(oprd2)
43. #include //例5.4虚函数的定义举例。
class Grandam{ //声明基类Grandam
public:
virtual void introduce_self() //定义虚函数introduce_self
{ cout<<"I am grandam."<
44. int main()
{ Grandam *ptr; //定义指向基类的对象指针ptr
Grandam g; //定义基类对象g
Mother m; //定义派生类对象m
Daughter d; //定义派生类对象d
ptr=&g; //对象指针ptr指向基类对象g
ptr->introduce_self(); //调用基类Grandam的虚函数
ptr=&m; //对象指针ptr指向派生类对象m
ptr->introduce_self(); //调用派生类Mother的虚函数
ptr=&d; //对象指针ptr指向派生类对象d
ptr->introduce_self(); //调用派生类Daughter的虚函数
return 0;
}本程序运行的结果如下:
I am grandam.
I am mother.
I am daughter.