Home >Backend Development >C++ >C++ function overloading and overriding

C++ function overloading and overriding

王林
王林Original
2024-04-12 12:12:02940browse

Overloading and overwriting are different concepts in C. Overloading allows the creation of a function with the same name, with a different parameter list, while overriding allows a derived class function to override a base class function with the same name. In overloading, the function name is the same but the parameter list is different, in overriding the function name and parameter list must be the same and the derived class function must use the override keyword.

C++ 函数的重载和覆盖

Overloading and overwriting of C functions

Overloading and overwriting

Overloading and overriding are two different concepts in C. Overloading allows the creation of functions with the same name but different parameter lists, while overriding allows a function in a derived class to override a function of the same name in a base class.

Overloaded

Suppose we have a function named area that calculates the areas of different shapes based on different input parameters:

int area(int length, int width);  // 矩形面积
double area(double radius);  // 圆形面积

These functions are overloaded with the area names because they have different parameter lists. They are able to process different types of data and perform different calculations.

Override

Now, assume we have a base class Shape, which has an area virtual function:

class Shape {
public:
    virtual double area() const = 0;
};

We can create derived classes Rectangle and Circle and override the area function:

class Rectangle : public Shape {
public:
    double area() const override { return length * width; }
    int length, width;  // 矩形属性
};

class Circle : public Shape {
public:
    double area() const override { return Math.PI * radius * radius; }
    double radius;  // 圆形属性
};

In the derived class, we Explicitly override the area function of the parent class through the override keyword. The overridden function must have the same return type and name as the parent function.

Practical case

We create a program to demonstrate overloading and overwriting:

#include <iostream>
#include <cmath>

using namespace std;

class Shape {
public:
    virtual double area() const = 0;
};

class Rectangle : public Shape {
public:
    double area() const override { return length * width; }
    int length, width;
};

class Circle : public Shape {
public:
    double area() const override { return Math.PI * radius * radius; }
    double radius;
};

int main() {
    // 创建矩形对象
    Rectangle rectangle;
    rectangle.length = 5;
    rectangle.width = 3;

    // 创建圆形对象
    Circle circle;
    circle.radius = 2;

    // 计算并输出矩形和圆形的面积
    cout << "矩形面积:" << rectangle.area() << endl;
    cout << "圆形面积:" << circle.area() << endl;
}

Output results:

矩形面积:15
圆形面积:12.566370614359172

This example Shows how to create functions with different functionality through overloading and overriding.

The above is the detailed content of C++ function overloading and overriding. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn