博客
关于我
(c++)第五章多态性和虚函数
阅读量:722 次
发布时间:2019-03-21

本文共 2419 字,大约阅读时间需要 8 分钟。

5.1 静态联编与动态联编

在编程中,多态性是指同一符号或名字在不同情境下具有不同实现的现象。在C++中,有两种多态性:编译时的多态性和运行时的多态性。函数联编是指在函数调用时,根据具体情况选择适当的函数体进行链接。C++支持两种联编方式:静态联编和动态联编。

静态联编(早期联编)

静态联编是在程序编译阶段完成的。其特点是速度快,但缺乏灵活性。编译时的多态性也称为静态多态性。以下示例展示了静态联编的工作原理:

#include 
const double PI = 3.14;using namespace std;class Figure { public: Figure() {} double area() const { return 0.0; }};class Circle : public Figure { public: Circle(double myr) { R = myr; } double area() const { return PI * R * R; } protected: double R;};class Rectangle : public Figure { public: Rectangle(double myl, double myw) { L = myl; W = myw; } double area() const { return L * W; } private: double L, W;};int main() { Figure fig; double area; area = fig.area(); cout << "Area of figure is " << area << endl;}

运行结果:

Area of figure is 0Area of circle is 28.26Area of rectangle is 20

静态联编的优点是运行速度快。根据对象赋值兼容原则,基类对象可以与派生类对象兼容,基类指针可以指向派生类对象,基类引用也可以引用派生类对象。

动态联编(晚期联编)

动态联编发生在程序运行时,适用于函数的多态性。动态联编需要满足三个条件:类型兼容原则、声明虚函数以及通过指针、引用或函数调用访问虚函数。

虚函数

虚函数是动态联编的基础。虚函数在基类中声明,并可由派生类重新定义。虚函数需要满足以下条件:

  • 基类中的虚函数具有自动传递虚特性向下。
  • 派生类可通过覆盖基类虚函数来实现动态联编。

以下示例展示了虚函数的使用:

#include 
using namespace std;class Base { public: virtual ~Base() {}};class Subclass : public Base { public: Subclass() {} ~Subclass() {}};int main() { Subclass sc; cout << "对象创建成功" << endl;}

运行结果:

对象创建成功

虚函数与一般函数重载的区别

虚函数与一般函数重载有以下区别:

  • 函数名和签名需完全一致。
  • 仅限成员函数,且为非静态成员函数。

继承虚属性向下

基类中的虚函数自动传给派生类,其虚特性保持不变。派生类可通过覆盖基类虚函数来维持多态性。

以下示例展示了不恰当虚函数的错误:

class Base {    public:    virtual int func(int x) {        cout << "This is Base class";        return x;    }};class Subclass : public Base {    public:    virtual int func(float x) {        cout << "This is Sub class";        return x;    }};运行结果:

This is Base classx=5This is Sub classx=5

以下示例展示了不正确覆盖虚函数的错误:```cppclass Subclass : public Base {    public:    virtual int func(int x) {        cout << "This is Sub class";        return x;    }}int main() {    Base bp;    cout << bp.func(5) << endl;}

运行结果:

0

成员函数中调用虚函数

成员函数可直接调用类等级中的虚函数:

class Base {    public:    virtual void func1() {        cout << "This is Base class func1()" << endl;    }};class Subclass : public Base {    public:    virtual void func1() {        cout << "This is Sub class func1()" << endl;    }};int main() {    Subclass sc;    sc.func1();}

运行结果:

This is Sub class func1()

在满足公有继承关系的情况下,成员函数中调用虚函数将采用动态联编。

转载地址:http://abirz.baihongyu.com/

你可能感兴趣的文章
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>