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.
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.
A virtual function is a member function that is declared asvirtual
. 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; } };
Override virtual functions in derived classes:
class Derived : public Base { public: virtual void display() { // 重写 display 方法 cout << "Derived class display" << endl; } };
Inheritance allows derived classes to inherit data and methods from base classes.
Inheritance syntax:
class Derived : public Base { // 派生类声明 };
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: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:
#includeusing 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; }
Output:
Drawing circle Drawing square
In this example, theCircle
andSquare
classes inherit thedraw
virtual function from the base classShape
. When thedraw
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!