Home  >  Article  >  Backend Development  >  Detailed explanation of C++ function inheritance: What is the essence of polymorphism?

Detailed explanation of C++ function inheritance: What is the essence of polymorphism?

WBOY
WBOYOriginal
2024-05-04 21:30:021086browse

Overview of function inheritance: Function inheritance in C is implemented through the override keyword, allowing subclasses to override parent class functions, making polymorphism possible, that is, objects show different behaviors at runtime, even if they belong to the same parent class.

C++ 函数继承详解:多态的本质是什么?

C Detailed explanation of function inheritance: The essence of polymorphism

In object-oriented programming, inheritance is an important mechanism, which allows subclasses to inherit from parent classes Data members and member functions. Function inheritance refers to the inheritance of member functions from parent class to child class.

Introduction to Polymorphism

Polymorphism is a key concept in object-oriented programming that allows objects to behave differently at runtime, even if they belong to the same parent class. Function inheritance is a way to achieve polymorphism.

Function inheritance

In C, function inheritance is implemented through the override keyword. When a child class defines a function with the same name and signature as the parent class, the function is marked override. This tells the compiler that the subclass is overriding the parent class's functions.

class Base {
public:
    virtual void print() {
        std::cout << "Base class" << std::endl;
    }
};

class Derived : public Base {
public:
    virtual void print() override {
        std::cout << "Derived class" << std::endl;
    }
};

In this example, the Base class defines a print() function, and the Derived class passes override keyword overrides this function. Therefore, when the print() function of a Derived class object is called, it will print "Derived class", not "Base class".

Virtual function

override The function must be a virtual function. Virtual functions are declared with the virtual keyword, which allows subclasses to override parent class functions. In the above example, the print() function is virtual.

Practical case

The following is a code example that demonstrates function inheritance and polymorphism:

#include <iostream>

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

class Rectangle : public Shape {
public:
    double width, height;
    Rectangle(double w, double h) : width(w), height(h) {}
    double getArea() override { return width * height; }
};

class Circle : public Shape {
public:
    double radius;
    Circle(double r) : radius(r) {}
    double getArea() override { return 3.14 * radius * radius; }
};

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

    for (int i = 0; i < 2; i++) {
        std::cout << "Area of " << (i == 0 ? "Rectangle" : "Circle") << ": " << shapes[i]->getArea() << std::endl;
    }

    return 0;
}

In this case, Shape is a shape class Base class, which defines a pure virtual function getArea(). The Rectangle and Circle classes inherit from the Shape class and override the getArea() function to calculate their respective areas.

In the main() function, using polymorphism, a base class array is used to store Rectangle and Circle objects. Since the getArea() function is overridden, the correct area is printed based on the object type.

Through function inheritance and polymorphism, we can build flexible and extensible programs with different behaviors.

The above is the detailed content of Detailed explanation of C++ function inheritance: What is the essence of polymorphism?. 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