C + + polymorphic

Source: Internet
Author: User

C + + polymorphism is summed up in a nutshell: by adding the virtual keyword to a function in the base class, overriding the function in a derived class, the runtime will invoke the corresponding function based on the actual type of the object. If the object type is a derived class, the function of the derived class is called, and if the object type is a base class, the function of the base class is called .

1: The function declared by the virtual keyword is called virtual function , the virtual function is definitely the member function of the class;

2: A class with a virtual function has a one-dimensional virtual function table called a virtual table, and the object of the class has a virtual pointer to the beginning of the virtual table. The virtual table is corresponding to the class, and the virtual table pointer is corresponding to the object;

3: Polymorphism is a multi-interface implementation, is the core of object-oriented, divided into the polymorphism of the class and the polymorphism of the function;

4: Multi-state with virtual function to achieve, combined with dynamic binding (late binding);

5: Pure virtual function is virtual function plus = 0;

6: abstract class refers to a class that includes at least one pure virtual function.

Pure virtual function: virtual void fun () = 0; abstract Class! This function must be implemented in a subclass, that is, there is a name first, no content, and the content is implemented in the derived class.

Let's look at an example first.

1 #include "stdafx.h" 2 #include <iostream>  3 #include <stdlib.h> 4 using namespace std;  5  6 Class Father 7 {8 public:9     void Face ()     {one         cout << "Father ' s Face" << endl;12     }13  +     void Say ()     {         cout << "Father Say Hello" << endl;17     }18};19 class Son:public Father22 {All public:     Say ()     {         cout << "Son Say Hello" << endl;27     }28};2 9 void Main () {     son;33     Father *pfather=&son;//implicit type conversion     Pfather->say (); 35}

The result of the output is:

We first defined a son class in the main () function, and then defined a pointer variable pfather to the Father class, and then called Pfather->say () with that variable. It is estimated that many people tend to confuse this with C + + polymorphism, Think son is actually the son class object, should be called son class say, output "son say hello", but the result is not.

From the compilation point of view:

When compiling , the C + + compiler determines the address of the function (non-virtual function) called by each object , which is called early binding, and when we assign the address of son to Pfather, theC + + compiler makes a type conversion , the C + + compiler considers the variable pfather to be the address of the Father object, and when executing Pfather->say () in the main function, the Say function of the Father object is of course called.

From the memory point of view:

    

The memory model of the Son class object such as

When we construct the object of the son class, we first call the constructor of the father class to construct the object of the Father class, and then call the son class's constructor to complete the construction of its own part, thus stitching up a complete son class object. When we convert the Son class object to the Father type, the object is considered to be the upper part of the entire memory model of the original object, that is, the "father of the memory" of the object, so when we use the type-converted object Pointer to invoke its method, of course, it is called in the memory of its method, Therefore, the output "Father Say Hello", it is also logical.

As many people think, in the code above, we know that Pfather actually points to the son class object, and we want the result of the output to be the say method of the son class, so it is necessary to use the virtual function in the thought of achieving this result.

The result of the previous output is because the compiler at the time of compilation, has determined the address of the function called by the object , to solve this problem is to use late binding, when the compiler uses late binding, will be at run time to determine the type of the object and the correct call function, but to let the compiler use late binding, To use the virtual keyword when declaring a function in a base class, such a function is called a virtual function, and once a function is declared as virtual in the base class, the function is virtual in all derived classes, and it does not need to be explicitly declared as virtual.

Change the code a little bit and look at the results of the run

  

1 #include "stdafx.h" 2 #include <iostream>  3 #include <stdlib.h> 4 using namespace std;  5  6 Class Father 7 {8 public:9     void Face ()     {one         cout << "Father ' s Face" << endl;12     }13     virtual void Say ()     {         cout << "Father Say Hello" << endl;17     }18};19 class Son:pu Blic Father22 {All public:     $     void Say ()     {         cout << "Son Say Hello" << endl;27     } };29-void Main ()-{+     -Son son;33     Father *pfather=&son;//implicit type conversion     Pfather->say (); 35}

We find that the result is "Son say hello", which is called the correct function based on the type of object, so what happens behind us when we declare say () as virtual.

  at compile time, the compiler discovers that there are virtual functions in the Father class, at which point the compiler creates a virtual table (that is, vtable) for each class that contains the virtual function, which is a one-dimensional array in which the address of each virtual function is stored .

  

So how do you locate a virtual table ? The compiler also provides a virtual table pointer (that is, vptr) for each object , which points to the virtual table of the class to which the object belongs, and initializes the vptr, depending on the type of object, when the program is run. So that vptr correctly points to the virtual table of the owning class, in order to call the virtual function, can find the correct function, for the second Code program, because Pfather actually pointed to the object type is son, so vptr point to the son class vtable, When Pfather->son () is called, the Say () function of the Son class is found based on the address of the function in the virtual table.

It is because the virtual function that each object calls is indexed by the virtual table pointer, which determines the correct initialization of the virtual table pointer is very important, in other words, we can not call virtual function before the virtual table pointer is not properly initialized, then when or where the virtual table pointer is initialized? ?

The answer is: to create a virtual table in the constructor and initialize the virtual table pointer , when constructing the subclass object, the constructor of the parent class is called, at which point the compiler only "sees" the parent class, and does not know if there is any successor, it initializes the virtual table pointer of the parent class object. The virtual table pointer points to the virtual table of the parent class, and when the constructor for the subclass is executed, the virtual table pointer of the subclass object is initialized, pointing to its own virtual table.

  

Summary (the base class has virtual functions):

1: Each class has a virtual table.

2: Virtual table can inherit, if the subclass does not override the virtual function, then the sub-class virtual table will still have the address of the function, but this address is the virtual function of the base class implementation, if the base class has 3 virtual functions, then the base class of the virtual table has three (virtual function address), the derived class will be virtual table, At least three items, if the corresponding virtual function is overridden, then the address in the virtual table will change, pointing to its own virtual function implementation, if the derived class has its own virtual function, then the virtual table will add the item.

3: The order of the virtual address in the virtual table of the derived class is the same as the virtual function address in the virtual table of the base class.

This is the C + + polymorphism, when the C + + compiler at compile time, found that the Father class say () function is a virtual function, this time C + + will use late binding technology, that is, the compiler does not determine the specific call function, but at run time, depending on the type of object to confirm which function is called, This ability is called C + + polymorphism, and when we do not add the virtual keyword before the Say () function, the C + + compiler determines which function is called, which is called early binding.

The polymorphism of C + + is realized by late binding technology .

C + + polymorphism is summed up in a nutshell: Before the function of the base class with the virtual keyword, override the function in a derived class, the runtime will invoke the corresponding function according to the actual type of the object, if the object type is a derived class, call the function of the derived class, if the object type is a base class, call the base class function.

A virtual function is defined in a base class to not determine the specific behavior of its derived class, for example:

Define a base class: Class Animal//animal, its function is breathe ()

Then define a class fish//fish. Its function is also breathe ()

Define a class Sheep//sheep, and its function is also breathe ()

The fish,sheep is defined as a derived class of animal, but fish is different from the breathe of sheep, one is to breathe through water in water, one to breathe directly, so the base class cannot determine how to define breathe, so only one virtual is defined in the base class. Breathe, it is an empty virtual function, the specific function is defined in the subclass, the program is generally run, find the class, if it has a base class, then find its base class, and finally run the function in the base class, then it found in the base class is the virtual identity of the function, It will go back to the sub-class to find the same name function, the derived class is also called the subclass, the base class is also called the parent class, this is the creation of virtual function, and the embodiment of the polymorphism of the class.

Polymorphism here refers to the polymorphism of a class.

The polymorphism of a function is a function that is defined as a function of several different parameters. When you call this function, different functions with the same name are called.

In general (no virtual functions are involved), when we invoke a function with a pointer/reference, the function being called depends on the type of the pointer/reference.

When designing to polymorphism, virtual functions and dynamic bindings are used, at which point the call is not determined at compile time but at runtime. Do not consider the pointer/reference type alone but look at the type of the pointer/reference object to determine the invocation of the function, based on the address of the function in the virtual table pointed to by the virtual pointer in the object, determining which function to call

Now let's look at an example of C + + polymorphism, and look at the output:

 1 #include "stdafx.h" 2 #include <iostream> 3 #include <stdlib.h> 4 using namespace std; 5 6 Class CA 7 {8 public:9 void F () {cout << "Ca f ()" << Endl; rtual void ff () (cout << "CA ff ()" << Endl; + f (); 17} 18}; Class Cb:public CA (public:23 virtual void f (), {cout << "CB f ()" << en dl () () () cout << "CB ff ()" << Endl; F (); Ca::ff (); 32} 33}; Cc:public CB + public:37 virtual void f () cout << "C f ()" << Endl; 4 0} 41}; A. int main () () () (): CB B; CA *ap = &b; CC c; &br cb = c; CB *BP = &c; 50 Wuyi ap->f (); cout << endl;53 B.f (); cout << endl;56 br.f (); cout << endl;59 BP-&GT;f (); cout << endl;62 ap->ff (); cout << endl;65 bp->ff (); cout << endl;68 return 0;  70}

Output Result:

C + + polymorphic

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.