In object-oriented programming, a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature. This concept is a very important part of the polymorphism portion of object-oriented programming (OOP).
定义
纯虚函数是一种特殊的虚函数,它的一般格式如下:class <类名>
{
virtual <类型><函数名>(<参数表>)=0;
…
};
在许多情况下,在基类中不能对虚函数给出有意义的实现,而把它声明为纯虚函数,它的实现留给该基类的派生类去做。这就是纯虚函数的作用。
纯虚函数可以让类先具有一个操作名称,而没有操作内容,让派生类在继承时再去具体地给出定义。凡是含有纯虚函数的类叫做抽象类。这种类不能声明对象,只是作为基类为派生类服务。除非在派生类中完全实现基类中所有的的纯虚函数,否则,派生类也变成了抽象类,不能实例化对象。
引入原因
1、为了方便使用多态特性,我们常常需要在基类中定义虚函数。2、在很多情况下,基类本身生成对象是不合情理的。例如,动物作为一个基类可以派生出老虎、孔雀等子类,但动物本身生成对象明显不合常理。
为了解决上述问题,引入了纯虚函数的概念,将函数定义为纯虚函数(方法:virtual ReturnType Function()= 0;)。若要使派生类为非抽象类,则编译器要求在派生类中,必须对纯虚函数予以重写以实现多态性。同时含有纯虚函数的类称为抽象类,它不能生成对象。这样就很好地解决了上述两个问题。
多态性
指相同对象收到不同消息或不同对象收到相同消息时产生不同的实现动作。C++支持两种多态性:编译时多态性,运行时多态性。a.编译时多态性:通过重载函数实现
b运行时多态性:通过虚函数实现。
虚函数
虚函数是在基类中被声明为virtual,并在派生类中重新定义的成员函数,可实现成员函数的动态重载抽象类
包含纯虚函数的类称为抽象类。由于抽象类包含了没有定义的纯虚函数,所以不能定义抽象类的对象。#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
//抽象类定义
class abstractcls
{
public:
abstractcls (float speed, int total) //构造函数
{
(*this).speed=speed;
(*this).total=total;
}
virtual void showmember()=0; //纯虚函数的定义
protected:
float speed;
int total;
};
class car : public abstractcls //抽象类派生类
//class car public:abstractcls //抽象类派生类
{
public:
//派生类的构造函数,实现基类的构造函数
car(float speed, int total,int aird):abstractcls(speed,total)
{
this->aird=aird;
}
virtual void showmember() //派生类函数重载
{
cout<<"speed:"<<speed<<" | total--"<<total<<" | aird--"<<aird<<endl;
}
protected:
int aird;
};
/**************************************************
***************************************************/
class CPolygon
{
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area (void) =0;
virtual void show_area (void) =0;
void printarea (void)
{ cout << this->area() << endl; }
};
class CRectangle: public CPolygon
{
public:
int area (void)
{ return (width * height); }
void show_area(void)
{
cout<<"Rectange area is: "<<area()<<'\n';
}
};
class CTriangle: public CPolygon
{
public:
int area (void)
{ return (width * height / 2); }
void show_area(void)
{
cout<<"Triangle area is: "<<area()<<'\n';
}
};
void test_Polymorphism_1()
{
CRectangle rect;
CTriangle trgl;
CPolygon * ppoly1 = ▭
CPolygon * ppoly2 = &trgl;
ppoly1->set_values (1991,11);
ppoly2->set_values (1991,11);
cout << ppoly1->area() << endl;
cout << ppoly2->area() << endl;
ppoly1->show_area();
ppoly2->show_area();
}
void test_Polymorphism_2()
{
CPolygon * ppoly1 = new CRectangle;
CPolygon * ppoly2 = new CTriangle;
ppoly1->set_values (7,2013);
ppoly2->set_values (7,2013);
ppoly1->printarea();
ppoly2->printarea();
delete ppoly1;
delete ppoly2;
}
/**************************************************
***************************************************/
int main(void)
{
car b(250,150,4);
b.showmember();
cout<<"-----test_Polymorphism()_1------\n";
test_Polymorphism_1();
cout<<"-----test_Polymorphism()_2------\n";
test_Polymorphism_2();
return 0;
}
/**********************
运行结果:
speed:250 | total--150 | aird--4
-----test_Polymorphism()_1------
21901
10950
Rectange area is: 21901
Triangle area is: 10950
-----test_Polymorphism()_2------
14091
7045
Process returned 0 (0x0) execution time : 0.590 s
Press any key to continue.
***********************/