多态性CC++中实现运行时多态性的方法

2024-01-17 0 1,138

一.概述

C++中的多态性意味着同一函数可以用多种不同的方式实现,并且在运行时根据实际情况选择和执行。在 C++中实现多态有两种方式:静态多态和动态多态。静态多态是指在编译时确定函数的实现,包括函数重载和模板函数;动态多态是指在运行时根据对象的实际类型来确定函数的实现,包括虚函数和抽象类。

第二,静态多态性

1.函数重载

函数重载是指在同一个范围内定义多个同名函数,并且它们的参数列表是不同的。编译器将根据函数的参数列表唯一地确定要调用的函数。函数重载可以通过编译时的函数匹配来实现,实现起来相对简单。以下是函数重载的示例代码:


#include <iostream>

void print(int i) {
    std::cout << "This is an integer: " << i << std::endl;
}

void print(float f) {
    std::cout << "This is a float: " << f << std::endl;
}

int main() {
    print(42);
    print(3.14f);
    return 0;
}

在上面的代码中,我们定义了两个同名的函数 print,但它们的参数列表是不同的,一个接受整数,另一个接受浮点数。调用 print 函数时,编译器会根据参数的类型自动选择调用哪个函数。

2.模板功能

模板函数是指在定义函数时使用类型参数,这可以使函数适用于许多不同的类型。编译器将在编译时根据参数类型生成特定的函数实现。模板功能的实现可以通过编译时的模板实例化来实现。以下是模板函数的示例代码:


#include <iostream>
#include <cstdlib>

template <typename T>
T max(T a, T b) {
    return a > b ? a : b;
}

int main() {
    int x = 42, y = 23;
    float f = 3.14f, g = 2.71f;
    std::cout << "Max of " << x << " and " << y << " is " << max(x, y) << std::endl;
    std::cout << "Max of " << f << " and " << g << " is " << max(f, g) << std::endl;
    return 0;
}

在上面的代码中,我们定义了一个模板函数 max,它可以对整数和浮点数等各种类型进行操作。当调用函数 max 时,编译器将根据参数类型自动推断使用哪个特定的函数实现。

三是动态多态性

1.虚拟功能

虚函数是指基类中定义的可以被派生类重写的函数。通过将函数声明为虚函数,我们可以在运行时根据对象的实际类型来确定要调用的函数实现。在 C++中,只要将函数声明为虚函数,就可以实现动态多态。以下是虚函数的示例代码:


#include <iostream>

class Shape {
public:
    virtual float calculateArea() { return 0; }
};

class Square : public Shape {
public:
    Square(float l) : _length(l) {}
    virtual float calculateArea() { return _length * _length; }
private:
    float _length;
};

class Circle : public Shape {
public:
    Circle(float r) : _radius(r) {}
    virtual float calculateArea() { return 3.14f * _radius * _radius; }
private:
    float _radius;
};

int main() {
    Shape *s1 = new Square(5);
    Shape *s2 = new Circle(3);
    std::cout << "Area of square is " << s1->calculateArea() << std::endl;
    std::cout << "Area of circle is " << s2->calculateArea() << std::endl;
    delete s1;
    delete s2;
    return 0;
}

在上面的代码中,我们定义了一个基类 Shape 和两个派生类 Square 和 Circle,它们都实现了函数 calculateArea。当调用函数 calculateArea 时,我们将基类指针指向派生类对象,我们可以看到派生类的实现函数实际上是在运行时调用的。

2.抽象类

抽象类是指至少包含一个纯虚函数的类。此类不能实例化,只能用作基类来派生其他类。在 C++中,抽象类可以通过将函数声明为纯虚函数来实现。以下是抽象类的示例代码:


#include <iostream>

class Shape {
public:
    virtual float calculateArea() = 0;
};

class Square : public Shape {
public:
    Square(float l) : _length(l) {}
    virtual float calculateArea() { return _length * _length; }
private:
    float _length;
};

class Circle : public Shape {
public:
    Circle(float r) : _radius(r) {}
    virtual float calculateArea() { return 3.14f * _radius * _radius; }
private:
    float _radius;
};

int main() {
    // Shape *s = new Shape();  // error: cannot instantiate abstract class
    Shape *s1 = new Square(5);
    Shape *s2 = new Circle(3);
    std::cout << "Area of square is " << s1->calculateArea() << std::endl;
    std::cout << "Area of circle is " << s2->calculateArea() << std::endl;
    delete s1;
    delete s2;
    return 0;
}

在上面的代码中,我们将基类“Shape”中的函数“calculateArea”声明为纯虚函数,从而实现了抽象类。抽象类不能实例化,只能用作基类来派生其他类。当调用函数 calculateArea 时,我们将基类指针指向派生类对象,我们可以看到派生类的实现函数实际上是在运行时调用的。

第四,总结

本文介绍了在 C++中实现运行时多态的两种方法:静态多态和动态多态。静态多态包括函数重载和模板函数,动态多态包括虚函数和抽象类。通过学习这些知识点,我们可以更好地理解 C++中的多态性,并更灵活地将其应用到实际程序开发中。

本文章已结束,如转载请注明:汇站网 » 多态性 CC++中实现运行时多态性的方法

收藏 (0)

微信支付 微信扫一扫

支付宝支付 支付宝扫一扫

打赏二维码
点赞 (1)

站长资源下载中心-找源码上汇站

常见问题
  • 如果付款后没有弹出下载页面,多刷新几下,有问题联系客服!
查看详情
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情

相关文章

联系官方客服

为您解决烦忧 - 24小时在线 专业服务