Home  >  Article  >  Backend Development  >  What is the difference between C++ function overloading and rewriting?

What is the difference between C++ function overloading and rewriting?

WBOY
WBOYOriginal
2024-04-13 18:06:01851browse

C Function overloading and rewriting: Overloading: The function with the same name has different parameter types or numbers, and the appropriate version is selected during compilation. Rewriting: A function in a derived class with the same name as the base class overrides the base class implementation and provides a specific implementation for the derived class. Overloading characteristics: different parameter types or numbers, bound at compile time. Overriding characteristics: Same parameter type and number, runtime binding, inheritance required.

C++ 函数重载与重写有何区别?

C function overloading and rewriting: concepts and differences

Overloading

  • Definition: Functions with the same function name but different parameter types or numbers.
  • Semantics: The compiler selects the appropriate function version based on the argument type or number at call time.
  • Syntax:
int sum(int a, int b);
double sum(double a, double b);

Override

  • Definition: Derived A function in a class with the same name and signature as the base class.
  • Semantics: Overridden functions in a derived class will override functions of the same name in the base class, thereby providing a specific implementation of the derived class.
  • Grammar:
class Base {
public:
    virtual int add(int a, int b);
};

class Derived : public Base {
public:
    int add(int a, int b) override;
};

Difference

Features Overload Override
Access Level Public or Private Public or Protected
Inherited Not required Required
Parameters Parameters Different types or numbers Same parameter type and number
Binding Compile time Run time
Scope Within the same class In base class and derived class

Practical case

Overload: Calculate the sum of numbers of different types

#include <iostream>

using namespace std;

int sum(int a, int b) {
    return a + b;
}

double sum(double a, double b) {
    return a + b;
}

int main() {
    cout << "整数之和:" << sum(1, 2) << endl;
    cout << "浮点数之和:" << sum(1.5, 2.5) << endl;
    return 0;
}

Override: Calculate the area of ​​various shapes using polymorphism

#include <iostream>

using namespace std;

class Shape {
public:
    virtual double area() = 0; // 纯虚函数
};

class Rectangle : public Shape {
public:
    Rectangle(double length, double width) : length(length), width(width) {}

    double area() override {
        return length * width;
    }

private:
    double length;
    double width;
};

class Circle : public Shape {
public:
    Circle(double radius) : radius(radius) {}

    double area() override {
        return 3.14 * radius * radius;
    }

private:
    double radius;
};

int main() {
    Shape* shapes[] = {new Rectangle(5, 10), new Circle(4)};

    for (int i = 0; i < 2; i++) {
        cout << "面积:" << shapes[i]->area() << endl;
    }

    return 0;
}

The above is the detailed content of What is the difference between C++ function overloading and rewriting?. 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