Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the definition of pure virtual function

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What is the definition of pure virtual function? I believe many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

A pure virtual function is a virtual function declared in the base class, which is not defined in the base class, but requires any derived class to define its own implementation method. The way to implement a pure virtual function in the base class is to add "= 0" after the function prototype.

Virtualvoidfuntion1 () = 0

Introduction reason

1. In order to facilitate the use of polymorphism, we often need to define virtual functions in the base class.

2. In many cases, it is unreasonable for the base class itself to generate objects. For example, as a base class, animals can derive subclasses such as tigers and peacocks, but the objects generated by the animals themselves are obviously unreasonable.

In order to solve the above problems, the concept of pure virtual function is introduced, and the function is defined as pure virtual function (method: virtualReturnTypeFunction () = 0;), then the compiler requires that it must be rewritten in derived classes to achieve polymorphism. A class that also contains pure virtual functions is called an abstract class, and it cannot generate objects. In this way, the above two problems are solved very well.

A class that declares a pure virtual function is an abstract class. Therefore, the user cannot create an instance of a class, only an instance of its derived class

The most striking feature of pure virtual functions is that they must redeclare the function in the inherited class (not followed by = 0, otherwise the derived class cannot be instantiated), and they are often not defined in the abstract class.

The purpose of defining pure virtual functions is to make derived classes merely interfaces to inheriting functions.

The meaning of pure virtual function makes all class objects (mainly derived class objects) can perform the action of pure virtual function, but the class can not provide a reasonable default implementation for pure virtual function. So the declaration of a class pure virtual function is telling the designer of the subclass, "you must provide an implementation of a pure virtual function, but I don't know how you will implement it."

Introduction to Abstract Class

Abstract class is a special class, which is established for the purpose of abstraction and design, and it is at the upper level of the inheritance hierarchy.

(1) the definition of abstract class: a class with pure virtual function is called abstract class.

(2) the function of the abstract class: the main function of the abstract class is to organize the relevant operations as a result interface in an inheritance hierarchy, which provides a common root for the derived class. the derived class will implement the operation as the interface in its base class. So the derived class actually depicts the general semantics of the operation interface of a set of subclasses, and these semantics are also passed to the subclasses. The subclasses can specifically implement these semantics, or they can pass these semantics to their own subclasses.

(3) Note when using abstract classes: a, abstract classes can only be used as base classes, and the implementation of their pure virtual functions is given by derived classes. If the derived class does not redefine the pure virtual function, but only inherits the pure virtual function of the base class, the derived class is still an abstract class. If the implementation of the pure virtual function of the base class is given in the derived class, then the derived class is no longer an abstract class, it is a concrete class that can create objects. B. abstract classes cannot define objects.

Summary:

Virtualvoidfuntion1 () = 0; pure virtual functions must not be defined, and pure virtual functions are used to regulate the behavior of derived classes, that is, interfaces. A class that contains pure virtual functions is an abstract class, and an abstract class cannot define an instance, but you can declare a pointer or reference to the concrete class that implements the abstract class.

After reading the above, have you mastered the definition of pure virtual function? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report