Home  >  Article  >  Backend Development  >  How do pure virtual functions promote polymorphism in C++?

How do pure virtual functions promote polymorphism in C++?

PHPz
PHPzOriginal
2024-06-03 20:01:591017browse

In C++, pure virtual functions are declared but not implemented in the base class, forcing derived classes to implement specific behavior to promote polymorphism. Derived classes must implement all pure virtual functions, otherwise they must also become abstract classes. Pure virtual functions ensure polymorphism by ensuring that only classes that implement all pure virtual functions can be used as pointers or references to abstract classes. In practical cases such as graphics drawing, it can ensure that all shape objects can respond to draw() calls, achieve polymorphism, and improve the scalability and maintainability of the code.

C++ 中纯虚函数如何促进多态性?

Pure virtual functions in C++: the driving force of polymorphism

In C++, pure virtual functions are a special member function, which is declared but not implemented in the base class. It is used in inheritance hierarchies to force derived classes to implement specific behavior.

How pure virtual functions work

When a class contains one or more pure virtual functions, it is called Abstract class. Derived classes mustimplement all pure virtual functions of the base class, otherwise they must also become abstract classes.

If a derived class does not implement all pure virtual functions of its base class, instantiating the derived class will generate an error. This ensures polymorphism because only classes that implement all pure virtual functions can be used as pointers or references to abstract classes.

C++ Code Example

Consider a shape inheritance hierarchy with a Drawable base class:

class Drawable {
public:
    virtual void draw() = 0;  // 纯虚函数
};

class Circle : public Drawable {
public:
    void draw() override;  // 实现 draw()
};

class Square : public Drawable {
public:
    void draw() override;  // 实现 draw()
};

inDrawable In the base class, draw() is a pure virtual function. This means that derived classes Circle and Square must implement draw() before they can be instantiated.

Practical case: Graphic drawing

Assume that the following drawing application is designed, in which one window contains objects of different shapes:

#include <vector>

// 创建一个形状向量
std::vector<Drawable*> shapes;

// 添加形状到向量
shapes.push_back(new Circle());
shapes.push_back(new Square());

// 循环遍历形状并绘制它们
for (Drawable* shape : shapes) {
    shape->draw();
}

Becausedraw() is a pure virtual function, and the program can ensure that all shape objects can respond to draw() calls, thus achieving polymorphism.

Conclusion

Pure virtual functions are a powerful tool for promoting polymorphism in C++. They ensure that derived classes implement all required behavior, thereby increasing the scalability and maintainability of your code. They play a key role in inheritance-based design patterns and are used in many real-world applications.

The above is the detailed content of How do pure virtual functions promote polymorphism in C++?. 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