Differences between virtual functions and pure virtual functions

Source: Internet
Author: User

First, emphasize a concept.
Defining a function as a virtual function does not mean that the function is not implemented.
It is defined as a virtual function to allow the base class pointer to call this function of the subclass.
Defining a function as a pure virtual function means that the function is not implemented.
A pure virtual function is defined to implement an interface and act as a standard. programmers who inherit this class must implement this function.
1. Introduction
Suppose we have the following class layers:

Class A {public: Virtual void Foo () {cout <"A: Foo () is called" <Endl ;}}; Class B: Public A {public: void Foo () {cout <"B: Foo () is called" <Endl ;}; int main (void) {A * A = new B (); a-> Foo (); // here, although a points to a, the called function (FOO) is B! Return 0 ;}

This example is a typical application of virtual functions. Through this example, you may have some concepts about virtual functions. It is virtual on the so-called "Deferred Association" or "Dynamic Association". The call of a class function is not determined at the time of compilation, it is determined at the runtime. When writing the code, it cannot be determined whether the base class function or the derived class function is called, so it becomes a "virtual" function.
Virtual functions can achieve polymorphism only by means of pointers or references.
C ++ pure virtual functions
I. Definition
A pure virtual function is a virtual function declared in the base class. It is not defined in the base class, but any derived class must define its own implementation method. To implement pure virtual functions in the base class, add "= 0" after the function prototype"
Virtual void funtion1 () = 0
Ii. Reasons for introduction
1. 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 to generate objects. For example, an animal can be derived from sub-classes such as tigers and peacocks as a base class, but the object generated by the animal itself is obviously unreasonable.
To solve the above problem, the concept of pure virtual function is introduced, and the function is defined as a pure virtual function (method: Virtual returntype function () = 0 ;), the compiler requires that the class must be rewritten to implement polymorphism. Classes that contain pure virtual functions are called abstract classes and cannot generate objects. In this way, the above two problems are well solved.
The class that declares pure virtual functions is an abstract class. Therefore, you cannot create a class instance, but only an instance of its derived class.
The most significant feature of pure virtual functions is that they must re-declare the functions in the inheritance class (do not add 0 to them; otherwise, the derived class cannot be instantiated ), they are often not defined in abstract classes.
The purpose of defining a pure virtual function is to make the derived class only inherit the function interface.
The meaning of pure virtual functions enables all class objects (mainly derived class objects) to execute pure virtual functions, but classes cannot provide a reasonable default implementation for pure virtual functions. Therefore, the declaration of pure virtual functions is to tell the subclass designer, "you must provide the implementation of a pure virtual function, but I don't know how you will implement it ".

Introduction to abstract classes
An abstract class is a special class that is created for the purpose of abstraction and design. It is in the upper layer of the hierarchy of inheritance.
(1) abstract class definition: class with pure virtual function is called abstract class.
(2) Role of abstract classes:
The main function of an abstract class is to organize related operations as result interfaces in an inheritance hierarchy to provide a public root for the derived classes, A derived class is implemented as an interface in its base class. Therefore, a derived class actually depicts the General Semantics of a group of subclass operation interfaces. These semantics are also passed to the subclass. The subclass can implement these semantics or pass these semantics to its own subclass.
(3) When using abstract classes, note the following:
• An abstract class can only be used as a base class. The implementation of its pure virtual function is provided by the derived class. If the pure virtual function is not redefined in the derived class, but only inherits the pure virtual function of the base class, the derived class is still an abstract class. If the derived class provides the implementation of the basic class pure virtual function, the derived class is no longer an abstract class, it is a specific class that can create objects.
• Abstract classes cannot define objects.

Summary:
1. The pure virtual function declaration is as follows: Virtual void funtion1 () = 0; pure virtual functions are not defined. Pure virtual functions are used to regulate the behavior of derived classes, that is, interfaces. Classes that contain pure virtual functions are abstract classes. abstract classes cannot define instances, but can declare pointers or references to specific classes that implement this abstract class.
2. The virtual function declaration is as follows: Virtual returntype functionname (parameter); virtual functions must be implemented. If not, the compiler reports an error. The error message is:
Error lnk ***: unresolved external symbol "public: Virtual void _ thiscall classname: virtualfunctionname (void )"
3. for virtual functions, both the parent class and Child class have their own versions. Dynamic binding when called in a multi-state mode.
4. Implement the subclass of the pure virtual function. The pure virtual function is programmed in the subclass, And the subclass of the subclass, that is, the grandson class, can overwrite the virtual function, dynamic binding when called in a multi-state mode.
5. virtual functions are used in C ++ to implement polymorphism. The core concept is to access the functions defined by the derived class through the base class.
6. When there is a dynamic allocation of heap memory, the Destructor must be a virtual function, but there is no need to be pure virtual.
7. youyuan is not a member function. Only member functions can be virtual. Therefore, youyuan cannot be a virtual function. However, you can solve the virtual problem of youyuan by calling the virtual member function.
8. The Destructor should be a virtual function and will call the corresponding object type destructor. Therefore, if the pointer points to a subclass object, the destructor of the subclass will be called, then, the destructor of the base class is automatically called.

Classes with pure virtual functions are abstract classes. Objects cannot be generated and can only be derived. The pure virtual function of the derived class is not rewritten, so its derived class is still an abstract class.
The pure virtual function is defined to make the base class uninstantiated.
This is because instantiation of such abstract data structures is meaningless.
Or the implementation is meaningless.
In fact, I personally think that the introduction of pure virtual functions is for two purposes.
1. To ensure security, avoid any unknown results that need to be clarified but caused by carelessness, and remind subclass to implement the desired implementation.
2. for efficiency, it is not for program execution efficiency, but for encoding efficiency.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.