Home > Backend Development > C++ > C++ Virtual Functions and Inheritance: Understanding the Mysteries of Polymorphic Inheritance

C++ Virtual Functions and Inheritance: Understanding the Mysteries of Polymorphic Inheritance

WBOY
Release: 2024-04-28 12:30:02
Original
711 people have browsed it

Virtual functions and inheritance implement polymorphism in object-oriented programming: declaring virtual functions allows derived classes to override base class methods and call them based on the runtime type of the object. Inheritance establishes a class hierarchy, and derived classes can access and extend base class data and methods. Polymorphic inheritance allows a derived class to inherit from multiple base classes, and most derived objects can use virtual functions of all base classes. The order in which virtual functions are called depends on the order in which the class is declared.

C++ 虚拟函数与继承:理解多态继承中的奥秘

C Virtual functions and inheritance: Understanding the mysteries of polymorphic inheritance

Overview

In object-oriented programming, virtual functions and Inheritance is a key element of polymorphism. Virtual functions allow a derived class to override a base class's methods and call the correct implementation at runtime. Inheritance establishes a hierarchical relationship between classes so that derived classes can access and extend data and methods in the base class. This article will delve into the mechanism of virtual functions and inheritance in C and illustrate it through practical cases.

Virtual function

A virtual function is a member function that is declared as virtual. When a virtual function is called from a derived class, the correct implementation of the function is called based on the object's actual runtime type.

Declare virtual functions:

class Base {
public:
    virtual void display() {
        cout << "Base class display" << endl;
    }
};
Copy after login

Override virtual functions in derived classes:

class Derived : public Base {
public:
    virtual void display() { // 重写 display 方法
        cout << "Derived class display" << endl;
    }
};
Copy after login

Inheritance

Inheritance allows derived classes to inherit data and methods from base classes.

Inheritance syntax:

class Derived : public Base {
    // 派生类声明
};
Copy after login

Derived classes can access and use all non-private members in the base class, including virtual functions. It can also override the virtual functions of the base class in the derived class.

Polymorphic inheritance

Polymorphic inheritance: When a derived class inherits from multiple base classes.

In polymorphic inheritance, most derived class objects can access and use all virtual functions of the base class. The order of calls across multiple inheritance hierarchies depends on the declaration order of the classes.

Practical case:

Consider the following code, demonstrating the use of polymorphic inheritance:

#include <iostream>

using namespace std;

class Shape {
public:
    virtual void draw() {
        cout << "Drawing shape" << endl;
    }
};

class Circle : public Shape {
public:
    virtual void draw() {
        cout << "Drawing circle" << endl;
    }
};

class Square : public Shape {
public:
    virtual void draw() {
        cout << "Drawing square" << endl;
    }
};

int main() {
    Shape* shapes[] = { new Circle(), new Square() };

    for (Shape* shape : shapes) {
        shape->draw();
    }

    return 0;
}
Copy after login

Output:

Drawing circle
Drawing square
Copy after login

In this example, the Circle and Square classes inherit the draw virtual function from the base class Shape. When the draw function is called, it calls the appropriate implementation based on the type of the actual object. This demonstrates how polymorphic inheritance allows a derived class to override virtual functions inherited from a base class and provide a correct object-specific implementation at runtime.

The above is the detailed content of C++ Virtual Functions and Inheritance: Understanding the Mysteries of Polymorphic Inheritance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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