Home > Backend Development > C++ > How does polymorphism interact with function overloading in C++?

How does polymorphism interact with function overloading in C++?

WBOY
Release: 2024-06-05 18:03:05
Original
626 people have browsed it

Polymorphism and function overloading interact: create overloaded functions in the base class, and redefine these function versions in the derived class. The parent class pointer or reference can point to the subclass object and call different methods according to the actual type of the object. In the sample code, the Shape base class declares the area() pure virtual function, and the derived classes Rectangle and Circle redefine the area() method to calculate their respective areas.

C++ 中多态性如何与函数重载相互作用?

Interaction of Polymorphism and Function Overloading in C++

In C++, Function OverloadingAllows the creation of different versions of a function with the same function name, with different parameter lists.

PolymorphismAllows subclass objects to be processed as the type of their parent class. This allows the parent class to point to a pointer or reference to the child class object so that different methods can be called depending on the actual type of the object.

When polymorphism and function overloading are used together, you can create overloaded functions in the base class and redefine these function versions in the derived class.

Sample code:

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, _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(4.5, 3.2),
        new Circle(2.5)
    };

    for (Shape* shape : shapes) {
        cout << shape->area() << endl;  // 调用正确的 area() 方法
    }

    return 0;
}
Copy after login

Output:

14.4
19.625
Copy after login

In this example, Shape base class A area() pure virtual function is declared. Derived classes Rectangle and Circle redefine the area() method to calculate their area differently.

main() The function creates an array of Shape pointers and stores the derived class object in it. Loop through the array and call the area() method, calling the appropriate method based on the actual type of the object pointed to.

The above is the detailed content of How does polymorphism interact with function overloading in C++?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template