Virtual functions in C + +

Source: Internet
Author: User

Parent FC Fatherclass sub-class SC Sonclass

  

One, virtual function

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:

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

Second, pure virtual function  

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":

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

Three , common functions

  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 only related to the literal value of the class object.

Instance

In the definition of a class object, there are four combinations of

    1. FC Fc=new FC;
    2. FC FC = new SC;
    3. SC Sc=new SC;
    4. SC SC =new FC;

Where 4 is wrong, a parent class object is not allowed to be assigned to a subclass object.

But allow, assign a subclass object to the parent class object, as shown in 1.

Of course, this is not the case in 3, because if FC contains pure virtual function, then in fact an abstract class, can not use abstract class to new an object,

Verify the program as follows:

1 //============================================================================2 //Name:vf.cpp3 //Fcuthor:lian4 //Version:5 //copyright:your Copyright Notice6 //Description:hello World in C + +, Fcnsi-style7 //============================================================================8 9#include <iostream>Ten using namespacestd; One  A  - classFC - { the  Public: -     Virtual voidOUT1 () =0;///Subclass Implementation -     Virtual~FC () {}; -     Virtual voidOut2 ()///Default Implementation +     { -cout<<"FC (OUT2)"<<Endl; +     } A     voidOUT3 ()///Force Implementation at     { -cout<<"FC (OUT3)"<<Endl; -     } - }; -  - classSc: PublicFC in { -  Public: to     Virtual~SC () {}; +     voidout1 () -     { thecout<<"SC (OUT1)"<<Endl; *     } $     voidOut2 ()Panax Notoginseng     { -cout<<"SC (OUT2)"<<Endl; the     } +     voidout3 () A     { thecout<<"SC (OUT3)"<<Endl; +     } - }; $  $ intMain () - { -FC *fs=NewSC; theFs->out1 (); -Fs->out2 ();WuyiFs->out3 (); thecout<<"************************"<<Endl; -SC *ss=NewSC; WuSs->out1 (); -Ss->out2 (); AboutSs->out3 (); $  -     DeleteFS; -     DeleteSS; -     return 0; A}

In line 51, this is the normal function, which only calls its own normal function based on the pointer or the literal "Fs->out3" class object.

So it calls the normal function of the parent class, Output FC (OUT3)

The output of the program is

SC (OUT1) SC (out2) FC (OUT3) ************************SC (OUT1) SC (OUT2) SC (OUT3)

  

Virtual functions in C + +

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.