多态按字面的意思就是多种形态。当类之间存在层次结构,并且类之间是通过继承关联时,就会用到多态。
C++ 多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。
下面的实例中,基类 Shape 被派生为两个类,如下所示:
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape( int a=0, int b=0)
{
width = a;
height = b;
}
int area()
{
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public Shape{
public:
Rectangle( int a=0, int b=0):Shape(a, b) { }
int area ()
{
cout << "Rectangle class area :" <<endl;
return (width * height);
}
};
class Triangle: public Shape{
public:
Triangle( int a=0, int b=0):Shape(a, b) { }
int area ()
{
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
};
// 程序的主函数
int main( )
{
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);
// 存储矩形的地址
shape = &rec;
// 调用矩形的求面积函数 area
shape->area();
// 存储三角形的地址
shape = &tri;
// 调用三角形的求面积函数 area
shape->area();
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
Parent class area
Parent class area
导致错误输出的原因是,调用函数 area() 被编译器设置为基类中的版本,这就是所谓的静态多态,或静态链接 - 函数调用在程序执行前就准备好了。有时候这也被称为早绑定,因为 area() 函数在程序编译期间就已经设置好了。
但现在,让我们对程序稍作修改,在 Shape 类中,area() 的声明前放置关键字 virtual,如下所示:
class Shape {
protected:
int width, height;
public:
Shape( int a=0, int b=0)
{
width = a;
height = b;
}
virtual int area()
{
cout << "Parent class area :" <<endl;
return 0;
}
};
修改后,当编译和执行前面的实例代码时,它会产生以下结果:
Rectangle class area
Triangle class area
正如您所看到的,每个子类都有一个函数 area() 的独立实现。这就是多态的一般使用方式。有了多态,您可以有多个不同的类,都带有同一个名称但具有不同实现的函数,函数的参数甚至可以是相同的。
虚函数
虚函数 是在基类中使用关键字 virtual 声明的函数。在派生类中重新定义基类中定义的虚函数时,会告诉编译器不要静态链接到该函数。
我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数,这种操作被称为动态链接,或后期绑定。
纯虚函数
您可能想要在基类中定义虚函数,以便在派生类中重新定义该函数更好地适用于对象,但是您在基类中又不能对虚函数给出有意义的实现,这个时候就会用到纯虚函数。
我们可以把基类中的虚函数 area() 改写如下:
class Shape {
protected:
int width, height;
public:
Shape( int a=0, int b=0)
{
width = a;
height = b;
}
// pure virtual function
virtual int area() = 0;
};
= 0 告诉编译器,函数没有主体,上面的虚函数是纯虚函数。
多态性:指一个名字,多种语义;或界面相同,多种实现。
重载函数是多态性的一种简单形式。
虚函数允许函数调用与函数体的联系在运行时才进行,称为动态联编。
虚函数:
冠以关键字 virtual 的成员函数称为虚函数。基类指针
实现运行时多态的关键首先是要说明虚函数,另外,必须用调用派生类的不同实现版本。
虚函数与基类指针
基类指针虽然获取派生类对象地址,却只能访问派生类从基类继承的成员
例:
- using namespace std ;
- class Base
- { public : Base(char xx) { x = xx; }
- void who() { cout << "Base class: " << x << "\n" ; }
- protected: char x;
- } ;
- class First_d : public Base
- { public : First_d(char xx, char yy):Base(xx) { y = yy; }
- void who() { cout << "First derived class: "<< x << ", " << y << "\n" ; }
- protected: char y;
- } ;
- class Second_d : public First_d
- { public :
- Second_d( char xx, char yy, char zz ) : First_d( xx, yy ) { z = zz; }
- void who() { cout << "Second derived class: "<< x << ", " << y << ", " << z << "\n" ; }
- protected: char z;
- } ;
- int main()
- { Base B_obj( 'A' ) ; First_d F_obj( 'T', 'O' ) ; Second_d S_obj( 'E', 'N', 'D' ) ;
- Base * p ;
- p = & B_obj ; p -> who() ;
- p = &F_obj ; p -> who() ;
- p = &S_obj ; p -> who() ;
- F_obj.who() ;
- ( ( Second_d * ) p ) -> who() ;
- }
执行结果:
Base class: A
Base class: T
Base class: E
First derived class: T.O
Second derived class: E. N . D
[注意]
Ø 一个虚函数,在派生类层界面相同的重载函数都保持虚特性
Ø 虚函数必须是类的成员函数
Ø 不能将友元说明为虚函数,但虚函数可以是另一个类的友元
Ø 析构函数可以是虚函数,但构造函数不能是虚函数
虚函数的重载特性
Ø 在派生类中重载基类的虚函数要求函数名、返回类型、参数个数、
参数类型和顺序完全相同
Ø 如果仅仅返回类型不同,C++认为是错误重载
如果函数原型不同,仅函数名相同,丢失虚特性
例:
- class base
- {
- public :
- virtual void vf1 ( ) ;
- virtual void vf2 ( ) ;
- virtual void vf3 ( ) ;
- void f ( ) ;
- } ;
- class derived : public base
- {
- public :
- void vf1 ( ) ; // 虚函数
- void vf2 ( int ) ; // 重载,参数不同,虚特性丢失
- char vf3 ( ) ; // error,仅返回类型不同
- void f ( ) ; // 非虚函数重载
- } ;
- void g ( )
- {
- derived d ;
- base * bp = & d ; // 基类指针指向派生类对象
- bp -> vf1 ( ) ; // 调用 deriver :: vf1 ( )
- bp -> vf2 ( ) ; // 调用 base :: vf2 ( )
- bp -> f ( ) ; // 调用 base :: f ( )
- } ;
虚析构函数
构造函数不能是虚函数。建立一个派生类对象时,必须从类层次的根开始,沿着继承路径逐个调用基类的构造函数。
析构函数可以是虚的。虚析构函数用于指引 delete 运算符正确析构动态对象。
例:普通析构函数在删除动态派生类对象的调用情况
- using namespace std ;
- class A
- {
- public:
- ~A(){ cout << "A::~A() is called.\n" ; }
- } ;
- class B : public A
- {
- public:
- ~B(){ cout << "B::~B() is called.\n" ; }
- } ;
- int main()
- {
- A *Ap = new B ;
- B *Bp2 = new B ;
- cout << "delete first object:\n" ;
- delete Ap;
- cout << "delete second object:\n" ;
- delete Bp2 ;
- }
执行结果:
delete first object:
A::~A()is called.
deletesecond object:
B::~B()is called.
A::~A()is called.
[说明]
(1)派生类应该从它的基类公有派生。
(2)必须首先在基类中定义虚函数。
(3)派生类对基类中声明虚函数重新定义时,关键字virtual可以不写。
(4)一般通过基类指针访问虚函数时才能体现多态性。
(5)一个虚函数无论被继承多少次,保持其虚函数特性。
(6)虚函数必须是其所在类的成员函数,而不能是友元函数,也不能是静态函数。
(7)构造函数、内联成员函数、静态成员函数不能是虚函数。
(虚函数不能以内联的方式进行处理)
(8)析构函数可以是虚函数,通常声明为虚函数。
6.成员函数调用虚函数(采用动态联编)
- class A
- { public:
- virtual double funA(double x)
- { cout<<"funA of class A called."<<endl;
- return x*x; }
- double funB(double x)
- { return funA(x)/2; }
- };
- class B:public A
- { public:
- virtual double funA(double x)
- { cout<<"funA of class B called."<<endl;
- return 2*x*x; }
- };
- class C:public B
- { public:
- virtual double funA(double x)
- { cout<<"funA of class C called."<<endl;
- return 3*x*x;
- }
- };
- void main()
- {
- C objc;
- cout<<objc.funB(3)<<endl;
- B objb;
- cout<<objb.funB(3)<<endl;
- }
执行结果:
funA of class C called.
13.5
funA of class B called.
9
纯虚函数和抽象类
(1)概念:纯虚函数是一种特殊的虚函数,
(2)功能:在许多情况下,在基类中不能对虚函数给出有意义的实现,而把它声明为纯虚函数,它的实现留给该基类的派生类去做。
(3)纯虚函数是一个在基类中说明的虚函数,在基类中没有定义,要求任何派生类都定义自己的版本。
纯虚函数为各派生类提供一个公共界面
纯虚函数说明形式:
virtual 类型 函数名(参数表)= 0 ;
一个具有纯虚函数的基类称为抽象类。
例:
- using namespace std ;
- class Number
- {
- public : Number (int i) { val = i ; }
- virtual void Show() = 0 ;
- protected: int val ;
- };
- class Hex_type : public Number
- {
- public: Hex_type(int i) : Number(i) { }
- void Show() { cout << "Hexadecimal:" << hex << val << endl ; }
- };
- class Dec_type : public Number
- {
- public: Dec_type(int i) : Number(i) { }
- void Show() { cout << "Decimal: " << dec << val << endl ; }
- };
- class Oct_type : public Number
- {
- public: Oct_type(int i) : Number(i) { }
- void Show() { cout << "Octal: " << oct << val << endl ; }
- };
- void fun( Number & n ) // 抽象类的引用参数
- {
- n.Show() ;
- }
- int main()
- {
- Dec_type n1(50);
- fun(n1); // Dec_type::Show()
- Hex_type n2(50);
- fun(n2); // Hex_type::Show()
- Oct_type n3(50);
- fun(n3); // Oct_type::Show()
- }