使用说明4 10.虚函数只能定义为其他类的友元,而不能定义为当 前类的友元(友元非当前类的成员)。即不能同时用 virtual和friend定义函数。 11.虚函数能根据对象类型适当地绑定函数成员,且绑 定函数成员的效率非常之高,因此,最好将普通函 数成员全部定义为虚函数。 12.注意:虚函数主要通过基类和派生类对象表现出多 态特性,由于union既不能定义基类又不能定义派生 类,故不能在union中定义虚函数。 转7.2 7
7 10. 虚函数只能定义为其他类的友元,而不能定义为当 前类 的友元(友元非当前类的成员)。即不能同时用 virtual 和friend 定义函数。 11. 虚函数能根据对象类型适当地绑定函数成员,且绑 定函数成员的效率非常之高,因此,最好将普通函 数成员全部定义为虚函数。 12. 注意:虚函数主要通过基类和派生类对象表现出多 态特性,由于union既不能定义基类又不能定义派生 类,故不能在union中定义虚函数。 使用说明4 转7.2
【例7.1】定义父类PONT和子类CIRCLE的绘图函数成员show #include <iostream.h> class POINT{ int x,y; public: int getx(){return x; int gety(){return y; virtual void show(){cout<<"Show a pointin"; POINT(int x,int y){POINT:x=x;POINT:y=y;} }; class CIRCLE:public POINT int r; public: int getr()return r; void show(){cout<<"Show a circleln";} CIRCLE(int x,int y,int r):POINT(x,y){CIRCLE::r=r; Return定义 8
8 【例7.1】定义父类POINT 和子类CIRCLE 的绘图函数成员 的绘图函数成员show #include <iostream.h> class POINT{ int x, y; public: int getx( ) { return x; } int gety( ) { return y; } virtual void show( ) { cout<<"Show a point\n"; } POINT(int x, int y) { POINT::x=x; POINT::y=y; } }; class CIRCLE: public POINT{ int r; public: int getr( ) { return r; } void show( ) { cout<<"Show a circle\n"; } CIRCLE(int x, int y, int r):POINT(x, y) { CIRCLE::r=r; } }; Return定义
void main(void) CIRCLE c(3,7,8); PONT*p=&c;//父指针p实际指向的是子类Circle对象 cout<<"The circle with radius "<<c.getr(); cout<<"is at("<<p->getx()<<","<<p->gety()<<")In"; p->show(); /Ip->show()动态绑定,并调用相应的虚函数 输出: The circle with radius 8 is at(3,7) Show a circle 考虑:若去掉virtual,结果如何? Return说明2 9
9 void main(void) { CIRCLE c(3, 7, 8); POINT *p=&c; cout<<"The circle with radius "<<c.getr( ); cout<<" is at ("<<p->getx( )<<", "<<p->gety( )<<")\n"; p->show( ); } //父指针p实际指向的是子类Circle对象 //p->show( ) 动态绑定,并调用相应的虚函数 考虑:若去掉virtual,结果如何? 输出: The circle with radius 8 is at (3, 7) Show a circle Return说明2
【例7.2】虚函数的使用方法 void main(void) #include <iostream.h> struct A{ C c; virtual void f1(){cout<<"A::fl\n"; virtual void f2(){cout<<"A::f2\n"; A*p=(A*)&c; virtual void f3(){cout<<"A:f3\n"; p->f1(); /调用B:f1() virtual void f40){cout<<"A::f4\n"; p->f2(); /调用B:f2() p->f3(); /调用A:f3() class B:Af p->f4); /调用C:f4() virtual void fl() irtual可省略 p->A:f2();1/调用A:f2() cout<<"B::flln";} void f2() /2自动成为虚函数 输出: B::f1 { cout<<"B::f2\n";} 语法检查静 ; B::f2 态进行,考 class C:B A::f3 虑(对否): void f4() /f4自动成为虚函数 C::f4 c.f1();/? { cout<<"C::f4\n";} A::f2 c.f2();1/? Return说明2 10
10 【 例7.2 】虚函数的使用方法 虚函数的使用方法 #include <iostream.h> struct A{ virtual void f1( ){ cout<<"A::f1\n"; } virtual void f2( ){ cout<<"A::f2\n"; } virtual void f3( ){ cout<<"A::f3\n"; } virtual void f4( ){ cout<<"A::f4\n"; } }; class B: A{ virtual void f1( ) //virtual可省略 { cout<<"B::f1\n"; } void f2( ) //f2自动成为虚函数 { cout<<"B::f2\n"; } }; class C: B{ void f4( ) //f4自动成为虚函数 { cout<<"C::f4\n"; } }; void main(void) { C c; A *p=(A *)&c; p->f1( ); p->f2( ); p->f3( ); p->f4( ); p->A::f2( ); } //调用B::f1( ) //调用B::f2( ) //调用A::f3( ) //调用C::f4( ) //调用A::f2( ) 输出:B::f1 B::f2 A::f3 C::f4 A::f2 Return说明 2 语法检查静 态进行,考 虑 (对否 ): c.f1( ); //? c.f2( ); //?