Multi-State and virtual function __ function

Source: Internet
Author: User

Polymorphism: Inheritance is a way for subclasses to use the parent class, and polymorphism is the method by which the parent class uses subclasses.

In C + +, there are two kinds of polymorphism, one is function overload, the other is virtual function. The function overload occurs at compile time, and its function arguments are not the same. While a virtual function occurs at run time, its function prototype is the same, depending on the point of the pointer.

There is a very good article about polymorphism and virtual functions. Send a link here.

http://blog.csdn.net/augusdi/article/details/38271009

Such a large pile of nouns, in fact, around one thing, is polymorphic, the other three nouns are for the implementation of C + + polymorphic mechanism of some of the rules proposed, the following two parts introduced, the first part of the introduction of "polymorphism", the second part of "virtual function, pure virtual function, abstract class"

One, "polymorphic"

The concept of polymorphism: about polymorphism, several sayings, good and bad, respectively:

1, refers to the same function of a variety of forms.

Personally think this is a high hand of the master like to say, for the general developer is a poor can not be bad concept, is simply misleading people, but people can easily rely on the function overload.

The following are two statements that the individual thinks is better explained:

2. Polymorphism is a feature of the ability to perform multiple forms, which in OO means that the language has the ability to handle it in different ways depending on the type of object, especially the overloaded method and the inherited class.

This is a bit of a detour, think carefully, this is what C + + to tell us.

3. Polymorphism is a technique that allows you to set a parent object to be equal to one or more of his child objects, and after assigning a value, the parent object can operate in a different way depending on the attributes of the child object that is currently assigned to it. To put it simply, it is a sentence that allows a pointer to a subclass type to be assigned to a pointer to a parent class type. Polymorphism is achieved through virtual functions (virtual function) in both Object Pascal and C + +.

This argument seems to be understandable, and comprehensive one, especially the last sentence, directly point out the relationship between the virtual function and polymorphism, if you still do not understand, it does not matter, and then 3 read two times, have an impression, look back.

Second, "virtual function, pure virtual function, abstract class"

Polymorphism just said a concept, what use has not been said to enter the second part of the. Look at the last sentence of concept 3, the virtual function is for polymorphic, the introduction of the role of polymorphism and virtual function is simply too big, put together to say it.

Polymorphism: Inheritance is a way for subclasses to use the parent class, and polymorphism is the method by which the parent class uses subclasses. This is a vernacular, polymorphism is used to invoke the parent class (specifically the object name of the parent class) to call the subclass's method, for example:

"Example One"[CPP] view plain copy class a {      public:      A ()  {}       (virtual)  void print ()  {      cout <<   "This is a."  << endl;     }     };      class  B : public A {      public:      B ()  {}       void print ()  {      cout <<  "This is  b. "  << endl;     }     };      int  Main (int argc, char* argv[])  {          B b;       a a;  a = b;a.print;----------------------------------------  make1     //&NBSP;A&NBSP;&AMP;A&NBSP;=&NBsp;b; a->print ();----------------------------------make2       //a *a  = new b () A->print ();--------------------------------make3      return  0;     }  

This will show:

This is B.

If you remove the virtual, it will show:

This is A.

(make1,2,3 is the three ways to correspond to the compatibility rules (described later), the result of the call is the same)

Plus virtual polymorphism, print in B is invoked, which means that the parent class can be implemented using subclasses.

After a preliminary understanding of the role of polymorphism, a more official and more accurate description of polymorphic effects is presented:

Polymorphism makes it possible to use pointers of the same class (base class) type to refer to objects of different classes, and to perform the same operations in different ways depending on the object being referenced. To look at the different subclass objects as the parent class, we can mask the differences between different subclass objects, write common code, and make general programming to meet the changing requirements. After assignment, the parent object can operate in a different way depending on the attributes of the child object that is currently assigned to it (that is, you can invoke an improved method of the related function of the parent object in the child object).

So why is it that the above example does not call the method in B to remove the virtual? It's obvious that the object of B is assigned to pointer A, because C + + defines a set of compatible rules for assigning objects, meaning that in the case of public derivation, an object of a derived class can be used as a base class object for certain situations. Specifically, the following three scenarios are:

Class A;

Class B:publ

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.