虚函数可以让成员函数操作一般化,用基类的指针指向不同的派生类的对象时,基类指针调用其虚成员函数,则会调用其真正指向对象的成员函数,而不是基类中定义的成员函数(只要派生类改写了该成员函数)。若不是虚函数,则不管基类指针指向的哪个派生类对象,调用时都会调用基类中定义的那个函数。
看看下面的例子
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
即实现了虚函数让成员函数操作一般化,用基类的指针指向不同的派生类的对象时,基类指针调用其虚成员函数,则会调用其真正指向对象的成员函数,而不是基类中定义的成员函数