The difference between virtual function, pure virtual function and ordinary function in C + + class in inheritance

Source: Internet
Author: User

1. Virtual function (impure virtual)

The virtual function of C + + has the function of " runtime polymorphism ", which provides the implementation of virtual function in the parent class, and provides the default function implementation for subclasses .

  Subclasses can override the virtual functions of the parent class to implement the specialization of subclasses.

The following is a virtual function in a parent class:

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

Class a{public:virtual void Out2 (string s) {cout<< "A (OUT2):" <<s<<endl; }};

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

2. Purely virtual functions (pure virtual)

A class containing pure virtual functions in C + + is referred to as an "abstract class." Abstract classes cannot use the new out object, only subclasses that implement the pure virtual function can be new out of the object.

Pure virtual Functions in C + + are more like "provide only declaration, no implementation", are the constraints of a subclass, and are " interface inheritance ".

Pure virtual Functions in C + + are also a "run-time polymorphism".

If the following class contains pure virtual functions, it is "abstract class":

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

Class a{public:virtual void Out1 (string s) = 0;    virtual void Out2 (string s) {cout<< "A (OUT2):" <<s<<endl; }};

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

3. Common function (no-virtual)

  Normal functions are statically compiled, with no run-time polymorphism, and only call their own normal functions based on pointers or referenced "literal" class objects .

The normal function is the " enforced implementation " provided by the parent class for the child class.

Therefore, in an inheritance relationship, subclasses should not override the normal functions of the parent class, because the invocation of the function is related to the literal value of the class object.

4. Program Synthesis Examples

650 "this.width=650;" src= "http:// Common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

#include  <iostream>using namespace std;class A{public:     VIRTUAL&NBSP;VOID&NBSP;OUT1 () =0;  ///implements     virtual ~a () {};   by subclasses   virtual void out2 ()  ///default implementation     {         cout<< "A (out2)" <<endl;    }    void &NBSP;OUT3 ()  ///Mandatory implementation         {         cout<< "A (OUT3)" <<endl;    }};class b:public a{public:     virtual ~b () {};&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;OUT1 ()     {        cout<< "B (OUT1)" <<endl;     }        void out2 ()      {        cout<< "B (out2)" <<endl;    }      &NBSP;&NBSP;&NBSP;VOID&NBSP;OUT3 ()     {         cout<< "B (OUT3)" <<endl;    }};int main () {    a * AB=NEW&NBSP;B;&NBSP;&NBSP;&NBSP;&NBSP;AB-&GT;OUT1 ();     ab->out2 ();    &NBSP;AB-&GT;OUT3 ();    cout<< "************************" <<endl;   &NBSP;&NBSP;B&NBSP;*BB=NEW&NBSP;B;&NBSP;&NBSP;&NBSP;&NBSP;BB-&GT;OUT1 ();     bb->out2 (); &NBSP;&NBSP;&NBSP;&NBSP;BB-&GT;OUT3 ();     delete ab;    delete  bb;        return 0;}

650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>

Execution Result:
650) this.width=650; "src=" Http://images.cnitblog.com/blog/466768/201402/262307107295135.x-png "style=" border:0px ; "/>


This article from the "GDT Commentary" blog, reproduced please contact the author!

The difference between virtual function, pure virtual function and ordinary function in C + + class in inheritance

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.