C++ | C++多态、虚函数、纯虚函数

本文探讨了C++中多态的概念,包括静态多态(通过基类指针调用固定版本函数)和动态多态(通过虚函数实现运行时选择)。通过实例展示了如何使用纯虚函数和虚函数创建灵活的继承结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++ | C++多态

C++ 多态

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

C++ 多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。

实例1

/*******************************************************************
 *   > File Name: classPolymorphism.cpp
 *   > Create Time: 2021年09月 8日  9:05:12
 ******************************************************************/
#include <iostream>
using namespace std;

class Shape{
protected:
    int width, height;
public:
    Shape(int a=0, int b=0)
    {
        cout << "calling constructor." << endl;
        width = a;
        height = b;
    }
    int area()
    {
        cout << "Parent class area: " << endl;
        return (width*height);
    }
};

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(void)
{
    Shape *shape;
    Rectangle rec(10,7);
    Triangle tri(10,5);

    shape = &rec; /* 存储矩形的地址 */
    shape->area();

    shape = &tri; /* 存储三角形的地址 */
    shape->area();

    return 0;
}

编译、运行:

PS E:\fly-prj\cplusplus\day8> make 
g++ -o classPolymorphism classPolymorphism.cpp -g -Wall
PS E:\fly-prj\cplusplus\day8> .\classPolymorphism.exe
calling constructor.
calling constructor.
Parent class area:
Parent class area:

导致错误输出的原因是,调用函数 area() 被编译器设置为基类中的版本,这就是所谓的静态多态,或静态链接 - 函数调用在程序执行前就准备好了。有时候这也被称为早绑定,因为 area() 函数在程序编译期间就已经设置好了。

实例2

/*******************************************************************
 *   > File Name: .\classPolymorphismVirtual.cpp
 *   > Create Time: 2021年09月 8日 13:00:02
 ******************************************************************/
#include <iostream>
using namespace std;

class Shape{
protected:
    int width, height;
public:
    Shape(int a=0, int b=0)
    {
        cout << "calling constructor." << endl;
        width = a;
        height = b;
    }
    virtual int area()
    {
        cout << "Parent class area: " << endl;
        return (width*height);
    }
};

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(void)
{
    Shape *shape;
    Rectangle rec(10,7);
    Triangle tri(10,5);

    shape = &rec; /* 存储矩形的地址 */
    shape->area();

    shape = &tri; /* 存储三角形的地址 */
    shape->area();

    return 0;
}

编译、运行:

PS E:\fly-prj\cplusplus\day8> make 
g++ -o classPolymorphismVirtual classPolymorphismVirtual.cpp -g -Wall
PS E:\fly-prj\cplusplus\day8> .\classPolymorphismVirtual.exe
calling constructor.
calling constructor.
Rectangle class area :
Triangle class area :

此时,编译器看的是指针的内容,而不是它的类型。因此,由于 tri 和 rec 类的对象的地址存储在 *shape 中,所以会调用各自的 area() 函数。

正如您所看到的,每个子类都有一个函数 area() 的独立实现。这就是多态的一般使用方式。
有了多态,您可以有多个不同的类,都带有同一个名称但具有不同实现的函数,函数的参数甚至可以是相同的。


虚函数

虚函数 是在基类中使用关键字 virtual 声明的函数。
在派生类中重新定义基类中定义的虚函数时,会告诉编译器不要静态链接到该函数。

我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数,这种操作被称为动态链接,或后期绑定

纯虚函数

您可能想要在基类中定义虚函数,以便在派生类中重新定义该函数更好地适用于对象,但是您在基类中又不能对虚函数给出有意义的实现,这个时候就会用到纯虚函数。

实例3:

/*******************************************************************
 *   > File Name: .\classPolymorphismVirtual1.cpp
 *   > Create Time: 2021年09月 8日 13:33:44
 ******************************************************************/
#include <iostream>
using namespace std;

class Shape{
protected:
    int width, height;
public:
    Shape(int a=0, int b=0)
    {
        cout << "calling constructor." << endl;
        width = a;
        height = b;
    }
    // pure virtual function
    // = 0 告诉编译器,函数没有主体,上面的虚函数是纯虚函数。
    virtual int area() = 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(void)
{
    Shape *shape;
    Rectangle rec(10,7);
    Triangle tri(10,5);

    shape = &rec; /* 存储矩形的地址 */
    shape->area();

    shape = &tri; /* 存储三角形的地址 */
    shape->area();

    return 0;
}

编译、运行:

PS E:\fly-prj\cplusplus\day8> make 
g++ -o classPolymorphismVirtual1 classPolymorphismVirtual1.cpp -g -Wall
PS E:\fly-prj\cplusplus\day8> .\classPolymorphismVirtual1.exe    
calling constructor.
calling constructor.
Rectangle class area :
Triangle class area :
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值