虚函数的用法以及作用 .

虚函数可以让成员函数操作一般化,用基类的指针指向不同的派生类的对象时,基类指针调用其虚成员函数,则会调用其真正指向对象的成员函数,而不是基类中定义的成员函数(只要派生类改写了该成员函数)。若不是虚函数,则不管基类指针指向的哪个派生类对象,调用时都会调用基类中定义的那个函数。

 

看看下面的例子

 

1.不用虚函数时

 

#include<iostream.h>
class Point
{
public:
Point(double i,double j){x=i;y=j;}
double Area()const{return 0;}
// virtual double Area()const{return 0;}
private:
double x,y;
};
class Rectangle:public Point
{
public:
Rectangle(double i,double j,double k,double l);
double Area()const{return w*h;}
private:
double w,h;
};
Rectangle::Rectangle(double i,double j,double k,double l):Point(i,j)
{
w=k;h=l;
}
class Tri:public Point
{
public:
Tri(double i,double j,double k,double l);
double Area()const{return 2*w*h;}
private:
double w,h;
};
Tri::Tri(double i,double j,double k,double l):Point(i,j)
{
w=k;h=l;
}
void fun(Point &s)
//void fun(Rectangle &s)
{
cout<<s.Area()<<endl;
}
void main()
{
Rectangle rec(3.0,5.2,15.0,25.0);
Tri tri(3.0,5.2,15.0,25.0);
fun(rec);
fun(tri);
}

 

输出结果:

 

0

 

0

 

这说明,Point的两个派生类中重新定义的Area()函数并没有起到应有的作用,fun(rec),fun(tri)调用的都是基类中的Area(),因为这里是静态联编的,不会调用派生类中的成员函数(虽然rec和tri是派生类的对象),我们可以通过修改基类中Area()函数的定义来实现我们的目的,我们把基类中的Area()定义成虚函数,即Area()函数在派生类中可以有不同的实现,程序如下所示:

#include<iostream.h>
class Point
{
public:
Point(double i,double j){x=i;y=j;}
// double Area()const{return 0;}
virtual double Area()const{return 0;} //定义Area()为虚函数
private:
double x,y;
};
class Rectangle:public Point              //定义Rectangle类,公有继承Point
{
public:
Rectangle(double i,double j,double k,double l);
double Area()const{return w*h;}                     //Area()的不同实现方式之一
private:
double w,h;
};
Rectangle::Rectangle(double i,double j,double k,double l):Point(i,j)
{
w=k;h=l;
}
class Tri:public Point                         //定义Tri类,公有继承Point
{
public:
Tri(double i,double j,double k,double l);
double Area()const{return 2*w*h;}                    //Area()的不同实现方式之二

private:
double w,h;
};
Tri::Tri(double i,double j,double k,double l):Point(i,j)
{
w=k;h=l;
}
void fun(Point &s)                                 //定义的是基类引用
{
cout<<s.Area()<<endl;
}
void main()
{
Rectangle rec(3.0,5.2,15.0,25.0);
Tri tri(3.0,5.2,15.0,25.0);
fun(rec);                                         //调用不同的对象的实现
fun(tri);
}

 

输出结果如下:

 

375

 

750

 

即实现了虚函数让成员函数操作一般化,用基类的指针指向不同的派生类的对象时,基类指针调用其虚成员函数,则会调用其真正指向对象的成员函数,而不是基类中定义的成员函数

 

动态联编是指在程序运行时进行的联编,也称晚期联编。 动态联编要求在运行时解决程序中的函数调用与执行该函数代码间的关系。
使用虚函数可实现动态联编,不同联编可以选择不同的实现,这便是多态性。
继承是动态联编的基础,虚函数是动态联编的关键。

虚函数是一种非静态的成员函数,定义格式如下:

virtual <类型说明符> <函数名>(<参数表>)
{
//<函数体>
}

其中,virtual 是关键字。
如果某个类中的一个成员函数被说明为虚函数,该成员函数可能在派生类中存在着不同的实现版本。
由于存在虚函数,编译器将进行动态联编,使调用虚函数的对象在运行时确定,以实现动态联编的多态性。
例如:使用虚函数将上例改为动态联编的情况,将得到不同的结果

#include <iostream.h>
class Base
{
protected:
int x;
public:
Base(int a)
{ x=a; }
virtual void print()
{ cout<<“Base ”<<x<<“
”; }
};
class First_d: public Base
{
public:
First_d(int a): Base(a) { }
virtual void print()
{ cout<<“First derivation
”<<x<<“
”; }
};

class Second_d: public Base
{
public:
Second_d(int a):Base(a) { }
virtual void print()
{ cout<<“Second derivation
”<<x<<“
”; }
};
void main()
{
Base *p;
Base obj1(1);
First_d obj2(2);
Second_d obj3(3);
p=&obj1;
p->print();
p=&obj2;
p->print();
p=&obj3;
p->print();
obj2.print();
obj3.print();
}
运行结果:

Base 1
First derivation 2
Second derivation 3
First derivation 2
Second derivation 3

程序中,p->print();出现了三次,由于p 指向的对象不同,每次执行了print() 的不同实现版本。实现了“单界面、多实现版本”的思想。

posted on 2012-11-10 12:00  阿振  阅读(456)  评论(0编辑  收藏  举报

导航