Home  >  Article  >  Backend Development  >  The embodiment of polymorphism in C++ function overloading and rewriting

The embodiment of polymorphism in C++ function overloading and rewriting

PHPz
PHPzOriginal
2024-04-20 11:03:02535browse

Polymorphism in C: Function overloading allows multiple functions with the same name but different argument lists, with the function chosen to be executed based on the argument types when called. Function overriding allows a derived class to redefine methods that already exist in the base class, thereby achieving different types of behavior, depending on the type of object.

C++ 函数重载和重写中多态性的体现

The embodiment of polymorphism in C function overloading and rewriting

Polymorphism is a key concept in object-oriented programming one. It allows objects of different types (derived classes) to respond differently to the same function call. C implements polymorphism through function overloading and overriding.

Function overloading

Function overloading refers to multiple functions with the same name but different parameter lists. The compiler will choose the correct function based on the argument types when it is actually called. For example, the following code overloads the area() function, which can calculate the area of ​​a circle or rectangle:

class Circle {
public:
    double area(double radius) {
        return 3.14159 * radius * radius;
    }
};

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

Override

Override It refers to redefining methods in the derived class that already exist in the base class. It allows derived classes to provide their own implementations, enabling different types of behavior. For example, the following code overrides the area() method of the base class Rectangle in the derived class Square to calculate the area of ​​a square:

class Rectangle {
public:
    virtual double area(double length, double width) {
        return length * width;
    }
};

class Square : public Rectangle {
public:
    virtual double area(double side) override {
        return side * side;
    }
};

Practical case

Consider a graphics library with a Shape base class and Circle, Rectangle and Square Derived classes. We want to create a function draw() to draw different graphics. By using overloads, we can provide a different draw() method to handle each shape type:

struct IShape {
    virtual void draw() = 0;
};

struct Circle : public IShape {
    void draw() override {
        // 代码绘制圆
    }
};

struct Rectangle : public IShape {
    void draw() override {
        // 代码绘制矩形
    }
};

struct Square : public Rectangle {
    void draw() override {
        // 代码绘制正方形
    }
};

When calling the draw() method, C will Choose the correct function version based on the type of actual object. This allows us to write generic code to handle different types of graphics without the need for explicit conversions or casts.

The above is the detailed content of The embodiment of polymorphism in 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