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
- FC Fc=new FC;
- FC FC = new SC;
- SC Sc=new SC;
- 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 + +