Analysis of C + + virtual function and pure virtual function

Source: Internet
Author: User

Summary

In this paper, we first describe the definition of virtual function and pure virtual function, then analyze and compare the difference and relation (DWS).

Body

1) What is the difference between virtual function and pure virtual function?
Virtual Functions , does not represent a function that is not implemented, in order to allow a pointer to a base class to invoke this function of a subclass, or to allow a member function to be redefined by its subclasses.
Pure virtual function , just to represent the function is not implemented, in order to implement an interface, play a normative role, the specification inherits this class programmer must implement this function.
2) Virtual is virtual in the so-called " postponement of the joint compilation "or" Dynamic linking "On, the invocation of a class function is not determined at compile time, but is determined at run time." It is a "virtual" function because it is not possible to determine the function of the base class or the derived class when the code is written.
3) Pure virtual function definition
a pure virtual function is a virtual function declared in a base class that is not defined in the base class, but requires that any derived class define its own implementation method. The method of implementing a pure virtual function in a base class is to add "=0" after the function prototype
virtual void Funtion1 () = 0;

4) defines a pure virtual function Purpose : Makes a derived class simply an interface to an inherited function. All class objects (primarily derived class objects) can perform actions of pure virtual functions, but the class cannot provide a reasonable default implementation for pure virtual functions. So the declaration of a class pure virtual function is to tell the designer of the subclass, "You must provide a pure virtual function implementation, but I do not know how you will implement it."

The introduction of individual task pure virtual function is for two purposes
① for security, because avoid any unknown result that needs to be clarified but due to carelessness, remind subclasses to do the implementation that should be done
② for efficiency, not the efficiency of program execution, but the efficiency of coding.

5) The most notable feature of pure virtual functions is that they must re-declare the function in the inheriting class (not the subsequent = 0, otherwise the derived class cannot instantiate), and they are often not defined in the abstract class.


Deep Summary of virtual functions and pure virtual functions
1) The pure virtual function declaration is as follows: virtual void funtion1()=0; a pure virtual function must not be defined, pure virtual functions are used to standardize the behavior of derived classes, that is, interfaces. A class that contains a pure virtual function is an abstract class, and an abstract class cannot define an instance, but it can declare a pointer or reference to a concrete class that implements the abstract class.

2) The virtual function declaration is as follows: virtual ReturnType FunctionName(Parameter); virtual functions must implement , if it is not implemented, the compiler will give an error, with a false hint:error LNK****: unresolved external symbol "public: virtual void __thiscall ClassName::virtualFunctionName(void)"

3) For virtual functions, both the parent and child classes have their own versions. Dynamically bound when called by polymorphic mode.
3) For virtual functions, both the parent and child classes have their own versions. dynamically bound when called by polymorphic mode.
4) realizes the subclass of pure virtual function, the pure virtual function programmed virtual function in subclass, subclass of subclass that grandson class can override this virtual function, and dynamically bind when called by polymorphic mode.
5) virtual function is a mechanism used in C + + for implementing polymorphism (polymorphism). The core idea isA function defined by a derived class is accessed through a base class.
6) in the dynamic allocation of memory on the heap, the destructor must be virtual function, but there is no need to be pure virtual.
7)friends are not member functions, only member functions can be virtual, so friends cannot be virtual functions. However, you can solve the virtual problem of friends by having the friend function call the virtual member function.

8) The destructor should be a virtual function , the destructor for the corresponding object type is called, so if the pointer is to a child class object, the destructor for the subclass is called and the destructor for the base class is automatically called.
a pure virtual function cannot instantiate an object

#include <iostream>using namespace Std;class shape{public    :        Shape () {}        ~shape () {}        virtual void Draw () = 0;   }; int main () {    Shape s1;}

Error: Because shape cannot instantiate an object, it is changed to virtual function virtual void Draw () {};


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Analysis of C + + virtual function and pure virtual function

Related Article

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.