c++多态与虚函数



多态按字面的意思就是多种形态。当类之间存在层次结构,并且类之间是通过继承关联时,就会用到多态。

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

此时,编译器看的是指针的内容,而不是它的类型。因此,由于 tri 和 rec 类的对象的地址存储在 *shape 中,所以会调用各自的 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 的成员函数称为虚函数。基类指针

 实现运行时多态的关键首先是要说明虚函数,另外,必须用调用派生类的不同实现版本。

虚函数与基类指针

基类指针虽然获取派生类对象地址,却只能访问派生类从基类继承的成员

例:

  1. #include<iostream>
  2. using namespace std ;
  3. class Base
  4. { public : Base( char xx) { x = xx; }
  5. void who() { cout << "Base class: " << x << "\n" ; }
  6. protected: char x;
  7. } ;
  8. class First_d : public Base
  9. { public : First_d( char xx, char yy):Base(xx) { y = yy; }
  10. void who() { cout << "First derived class: "<< x << ", " << y << "\n" ; }
  11. protected: char y;
  12. } ;
  13. class Second_d : public First_d
  14. { public :
  15. Second_d( char xx, char yy, char zz ) : First_d( xx, yy ) { z = zz; }
  16. void who() { cout << "Second derived class: "<< x << ", " << y << ", " << z << "\n" ; }
  17. protected: char z;
  18. } ;
  19. int main()
  20. { Base B_obj( 'A' ) ; First_d F_obj( 'T', 'O' ) ; Second_d S_obj( 'E', 'N', 'D' ) ;
  21. Base * p ;
  22. p = & B_obj ; p -> who() ;
  23. p = &F_obj ; p -> who() ;
  24. p = &S_obj ; p -> who() ;
  25. F_obj.who() ;
  26. ( ( Second_d * ) p ) -> who() ;
  27. }


执行结果:
Base class: A

Base class: T

Base class: E

First derived class: T.O

Second derived class: E.  N . D

[注意]

Ø  一个虚函数,在派生类层界面相同的重载函数都保持虚特性

Ø   虚函数必须是类的成员函数

Ø   不能将友元说明为虚函数,但虚函数可以是另一个类的友元

Ø   析构函数可以是虚函数,但构造函数不能是虚函数

虚函数的重载特性

Ø  在派生类中重载基类的虚函数要求函数名、返回类型、参数个数、

           参数类型和顺序完全相同

Ø   如果仅仅返回类型不同,C++认为是错误重载

 如果函数原型不同,仅函数名相同,丢失虚特性

 

例:

  1. class base
  2. {
  3. public :
  4. virtual void vf1 ( ) ;
  5. virtual void vf2 ( ) ;
  6. virtual void vf3 ( ) ;
  7. void f ( ) ;
  8. } ;
  9. class derived : public base
  10. {
  11. public :
  12. void vf1 ( ) ; // 虚函数
  13. void vf2 ( int ) ; // 重载,参数不同,虚特性丢失
  14. char vf3 ( ) ; // error,仅返回类型不同
  15. void f ( ) ; // 非虚函数重载
  16. } ;
  17. void g ( )
  18. {
  19. derived d ;
  20. base * bp = & d ; // 基类指针指向派生类对象
  21. bp -> vf1 ( ) ; // 调用 deriver :: vf1 ( )
  22. bp -> vf2 ( ) ; // 调用 base :: vf2 ( )
  23. bp -> f ( ) ; // 调用 base :: f ( )
  24. } ;


虚析构函数

构造函数不能是虚函数。建立一个派生类对象时,必须从类层次的根开始,沿着继承路径逐个调用基类的构造函数。

析构函数可以是虚的。虚析构函数用于指引 delete 运算符正确析构动态对象。

例:普通析构函数在删除动态派生类对象的调用情况

  1. #include<iostream>
  2. using namespace std ;
  3. class A
  4. {
  5. public:
  6. ~A(){ cout << "A::~A() is called.\n" ; }
  7. } ;
  8. class B : public A
  9. {
  10. public:
  11. ~B(){ cout << "B::~B() is called.\n" ; }
  12. } ;
  13. int main()
  14. {
  15. A *Ap = new B ;
  16. B *Bp2 = new B ;
  17. cout << "delete first object:\n" ;
  18. delete Ap;
  19. cout << "delete second object:\n" ;
  20. delete Bp2 ;
  21. }

执行结果:
          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.成员函数调用虚函数(采用动态联编)


  1. #include <iostream.h>
  2. class A
  3. { public:
  4. virtual double funA(double x)
  5. { cout<< "funA of class A called."<< endl;
  6. return x*x; }
  7. double funB(double x)
  8. { return funA(x)/ 2; }
  9. };
  10. class B: public A
  11. { public:
  12. virtual double funA(double x)
  13. { cout<< "funA of class B called."<< endl;
  14. return 2*x*x; }
  15. };
  16. class C: public B
  17. { public:
  18. virtual double funA(double x)
  19. { cout<< "funA of class C called."<< endl;
  20. return 3*x*x;
  21. }
  22. };
  23. void main()
  24. {
  25. C objc;
  26. cout<<objc.funB( 3)<< endl;
  27. B objb;
  28. cout<<objb.funB( 3)<< endl;
  29. }

执行结果:

funA of class C called.

13.5

funA of class B called.

9

纯虚函数和抽象类

1)概念:纯虚函数是一种特殊的虚函数,

2)功能:在许多情况下,在基类中不能对虚函数给出有意义的实现,而把它声明为纯虚函数,它的实现留给该基类的派生类去做。

3)纯虚函数是一个在基类中说明的虚函数,在基类中没有定义,要求任何派生类都定义自己的版本。

纯虚函数为各派生类提供一个公共界面

  纯虚函数说明形式:

                virtual 类型  函数名(参数表)= 0 ;

 一个具有纯虚函数的基类称为抽象类。

  例:


  1. #include<iostream>
  2. using namespace std ;
  3. class Number
  4. {
  5. public : Number ( int i) { val = i ; }
  6. virtual void Show() = 0 ;
  7. protected: int val ;
  8. };
  9. class Hex_type : public Number
  10. {
  11. public: Hex_type( int i) : Number(i) { }
  12. void Show() { cout << "Hexadecimal:" << hex << val << endl ; }
  13. };
  14. class Dec_type : public Number
  15. {
  16. public: Dec_type( int i) : Number(i) { }
  17. void Show() { cout << "Decimal: " << dec << val << endl ; }
  18. };
  19. class Oct_type : public Number
  20. {
  21. public: Oct_type( int i) : Number(i) { }
  22. void Show() { cout << "Octal: " << oct << val << endl ; }
  23. };
  24. void fun( Number & n ) // 抽象类的引用参数
  25. {
  26. n.Show() ;
  27. }
  28. int main()
  29. {
  30. Dec_type n1(50);
  31. fun(n1); // Dec_type::Show()
  32. Hex_type n2(50);
  33. fun(n2); // Hex_type::Show()
  34. Oct_type n3(50);
  35. fun(n3); // Oct_type::Show()
  36. }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值