Reprint: What is virtual function

Source: Internet
Author: User

    A virtual function is associated with polymorphism, and polymorphism is associated with inheritance. So this article is on the inheritance level of the fuss. Without inheritance, nothing is to be talked about. The following is an understanding of the virtual function of C + +. One, what is a virtual function (if you do not know what the virtual function is, but there is an urgent need to know, then you should start from here) simply say, those who are modified by the Virtual keyword member function, is the virtual function. The function of virtual function, the use of professional terminology to explain is to achieve polymorphism (polymorphism), polymorphism is the interface and implementation of the separation, with the image of the language is to achieve a common approach, but because of individual differences and adopt different strategies. Let's look at a simple code class a{public:void print () {cout<< "This is A" <<endl;}}; Class B:public A{public:void print () {cout<< "This is B" <<endl;}}; int main () {///in order to make it easier to distinguish later, my main () code is called main1a A; B b;a.print (); B.print ();} With the print () interface of Class A and class B, we can see that these two classes adopt different strategies depending on the individual differences, and the output is what we expect, respectively, that is a and this is B. But is this really polymorphism? No, polymorphism There is also a key point is everything with a pointer to the base class or reference to manipulate the object. Now change the code at main (). int main () {//main2a A; b b; A * p1=&a; A * P2=&b;p1->print ();p 2->print ();} Run a look at the results, Yo Ah, suddenly looking back, the result is two A. The problem is, P2 clearly points to the object of Class B but is called the print () function of class A, which is not the result we expect, then we need to solve this problem to use the virtual function class a{public:virtual void print () {cout  << "This is A" &LT;&LT;ENDL;}  Now it's a virtual function.};class b:public a{public:void Print () {cout<< "This is B" &LT;&LT;ENDL;} Here we need to add a keyword to the front VirtuaL? There is no doubt that the member function print () of Class A has become a virtual function, so is the print () of class B a virtual function? The answer is yes, we simply set the member function of the base class to virtual, and the corresponding function of its derived class automatically becomes a virtual function. Therefore, the print () of Class B also becomes a virtual function. If you need to modify the virtual keyword before the corresponding function of the derived class, it is your own problem. Now rerun the MAIN2 code so that the result of the output will be this is a and this is B. Now to digest, I make a simple summary, the pointer to the base class when manipulating its Polymorphic class object, according to the different class object, the corresponding function is called, this function is a virtual function.
From Baidu  Onlinewan

Reprint: What is virtual function

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.