Usage and difference between C ++ virtual functions and pure virtual functions

Source: Internet
Author: User

1. virtual functions and pure virtual functions can be defined in the same class. Classes containing pure virtual functions are called abstract classes, and classes containing only virtual functions are called abstract classes) cannot be called abstract class ).

  

2. virtual functions can be directly used, or they can be called in the form of polymorphism after being overloaded by subclasses. Pure virtual functions must be called in sub classes) this function can be used only when it is implemented, because pure virtual functions are not defined only when they are declared in the base class.

 

3. Both virtual and pure virtual functions can be overloaded in sub class and called in the form of polymorphism.

 

4. virtual functions and pure virtual functions are usually stored in abstract base class-ABC. The inherited subclass is overloaded to provide a unified interface.

 

5. virtual function definition form: Virtual {method body}

Definition of pure virtual functions: Virtual {} = 0;

There cannot be a static identifier in the definition of virtual and pure virtual functions. The reason is very simple. The static-modified functions require bind in the early stage during compilation, however, virtual functions are dynamically bound (run-time bind), and the life recycle function modified by the two is different.

 

6. The virtual function must be implemented. If it is not implemented, the compiler reports an error. The error message is:

Error lnk ***: unresolved external symbol "public: Virtual void _ thiscall
Classname: virtualfunctionname (void )"

7. For virtual functions, both the parent class and subclass have their own versions. Dynamic binding when called in a multi-state mode.

 

8. Implement the subclass of the pure virtual function. The pure virtual function is programmed in the subclass, And the subclass of the subclass can be overwritten by the Child class.

This virtual function is dynamically bound when called in multi-state mode.

9. virtual functions are used in C ++ to implement polymorphism. The core concept is to access the derived class defined by the base class

Function

10. polymorphism refers to different implementation actions taken when the same object receives different messages or when different objects receive the same message. C ++ supports two types of polymorphism: compile-time polymorphism and Runtime polymorphism.
A. polymorphism during compilation: implemented through overload Functions
B Runtime polymorphism: implemented through virtual functions.

 

11. if a class contains pure virtual functions, any statements that attempt to instantiate the class will produce errors, because the abstract base class (ABC) cannot be directly called. The quilt class must inherit from the overload and then call its subclass method as required.

 

 1   //father class 2  3   class Virtualbase 4  5   { 6  7   public: 8  9   virtual void Demon()= 0; //prue virtual function10 11   virtual void Base() {cout<<"this is farther class"<};12 13   }14 15   //sub class16 17   class SubVirtual :public Virtualbase18 19   {20 21   public:22 23   void Demon() { cout<<" this is SubVirtual!"< 24 25   void Base() { cout<<"this is subclass Base"< 26 27   }28 29   /*30 31   instance class and sample32 33   */34 35   void main()36 37   {38 39   Virtualbase* inst = new SubVirtual(); //multstate pointer40 41   inst->Demon();42 43   inst->Base();44 45   // inst = new Virtualbase();46 47   // inst->Base()48 49   return ;50 51 }

 

 

Refer:

1. http://blog.csdn.net/wuchuanpingstone/article/details/6742465

This article analyzes the underlying implementation of virtual functions.

2. http://blog.sina.com.cn/s/blog_4b44e1c001000avp.html

This article is simple and clear.

3. http://www.vckbase.com/document/viewdoc? Id = 950

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.