Polymorphism is an important feature of object-oriented, it is simple to say: "An interface, a variety of implementations," is the same thing shows a variety of forms.
Programming is actually a process of abstracting the concrete world, which is a manifestation of abstraction, abstracting the common denominator of a series of specific things, and then through this abstract thing, and dialogue with different concrete things.
Sending the same message to different classes of objects will have different behavior. For example, if your boss lets all employees start working at nine o'clock, he says "get started" at nine o'clock, instead of saying to the salesperson: "Start a sales job," say to the technician, "Start technical work", because "employee" is an abstract thing, so long as the employee can start to work, He knows that. As for each employee, of course, they do their job and do their jobs.
Polymorphism allows the object of a subclass to be used as an object of the parent class, a reference to a parent type to an object of its subtype, and the method called is the method of that subtype. The code that references and calls the method here is determined before it is compiled, and the object that the reference points to can be dynamically bound during the run. To give an example of a comparative image:
For example, there is a function called a person to eat, the function required to pass the parameter is a person's object, but came an American, you can see with a knife and fork in the meal, and a Chinese people you see may be with chopsticks in the meal, which reflects the same method can produce a different form, which is polymorphic!
The role of polymorphism:
1. The application does not have to write function calls for each derived class, only the abstract base class is processed. Greatly improve the reusability of the program. Inherited
2. The functionality of a derived class can be called by a method of a base class or a reference variable, which is called backwards compatibility, and can improve extensibility and maintainability. The true role of polymorphism, previously needed to be implemented with switch----------------------------------------------------
Polymorphism is one of the main differences between object-oriented programming and process-oriented programming, what is polymorphism? Remember in the csdn of a C + + polymorphic article has a word: "Dragon, sub-sub-different" polymorphism is the same processing means can be used to deal with a variety of different situations, in the money can teacher's "C + + Programming Tutorial" book has such an example:
Defines a class of pupils
[All code in this document is pseudo-code]
Class Student
{public:
Student () {}
~student () {}
void pay tuition () {}
//......
};
There is a "pay tuition" processing function, because the students and pupils in some similar situation, we derive from the primary school students class:
Class Acadstudent:public Student
{public:
Acadstudent () {}
~ acadstudent () {}
void pay tuition () {}
//.......
};
We know that the student payment and the student payment situation is different, so although we in the university students inherit the "pay tuition" operation, but we do not, to overload it, define the university student's own tuition fee operation, so when we define a pupil, a college student:
Student A;
Acadstudent B;
A. Pay tuition fees (); That is, to call pupils, B. Tuition fees (); Is the call college students, the function is realized, but you have to realize that, maybe not only these two, may n kinds such as: elementary school students, junior high school students, senior students, graduate students .....
They can all be based on the student[Elementary class.
What do you do if the system asks you to take out one of these students and pay the tuition?
:
A for the students who want to pay tuition
{switch (typeof (A))
{Case pupil: A. Pupil:: Pay tuition fee (); break;
Case Junior High School students: A. Beginner:: Pay tuition fee (); break;
Case teen: A. High student:: Pay tuition fee (); break;
Default
.............
}
}
First, we want to define a typeof () in each class to identify its type, and then to distinguish it in the program, so, although it is OK, but, if the additional type to change the switch, and go the way of the process-oriented, and want to work through a module to achieve it is also difficult.
So, in C + +, polymorphism is provided, that is, the dynamic distinction can be realized by the technology of late-linking.
Add a virtual to the base class before "pay tuition" to tell the system that the processing of this process waits until execution to determine which class to call. This will allow you to:
Void General tuition fee operation (Student &a)
{A. Pay tuition fees ();
}
All done, you add a new type I am not afraid!!! [Specific implementation principle reference: "Inside the C + + Object Model"]. Without the virtual statement, the system determines the operation before it executes, such as passing "college students" to
Void General tuition fee operation (Student &a)
{A. Pay tuition fees ();
}
, a. Pay tuition fee (); The call is the "pay tuition" in the class of college students inheriting from student class.
Therefore, the virtual function is necessary for the implementation of the polymorphic.
Why is there a pure virtual function?
If according to the above example programming, all types inherit pupils class, we will find a pupil of their own specific east [such as void on Art Class ();], also are inherited by college students, although not affect the operation of college students, but the longer the length of time, pupils in the category of their own specific things more and more, This goes on, the university student redundant data is more, what method can solve????
When defining a base class, define an abstract class, such as a student class, that is implemented in a student class.
。 This process is called decomposition.
This class is not obvious enough to explain the pure virtual function, for example:
Class Person ()
{public:
//......
void Eat ()
{people eat;
}
//......
Char *name;
int age;
};
Class Dog ()
{public:
//......
void Eat ()
{The dog eats excrement;
}
//......
Char *name;
int age;
};
Humans, dogs have some of the same things, such as name, age, eating action, some people think of the code for reuse, let the human inherit the dog class, but the data redundancy let this idea is finished, so someone came up with a way to define a class like this:
This class is between the human dog, there are some things that humans and dogs share, such as age, name, weight and so on, but it does not exist, and its significance is only to derive other classes, such as humans, dogs, so that the system can be cleared 、。。。。。 ,, anyway, many advantages.
In this world does not exist in the human dog between the East, so this "human dog class" In the "eat" is also meaningless, you also difficult for its content next definition, and there is no need, so define it as pure virtual function, the form is: virtual void eat () = 0; Used to tell the system:
1, this function can have no function definition;
2, the class that owns this function is abstract class;
You might say that the pure virtual function is meaningless, why do you want it, and what does it do?
To prepare for polymorphism!!!
Because an instance of an abstract class is meaningless, it is not allowed to define an instance of it in C + +. (If you define such an instance of a, then you call A. Eat () What to do?)
Of course, you can also in the base class, virtual Eat () {}
In this way, the base class is not an abstract class, there can be instances, and it has no effect on other aspects.
But you also have to admit that such objects are not aware of, its existence can only make you think on the increase of burden, in addition to the wrong to consider whether there are such objects in mischief, so C + + simply provide a "virtual function", abstract class, the mechanism, give us the operation of the restrictions can also be considered insurance [ Can not have abstract class object], imagine when the code becomes very large, its existence is how necessary AH!!!
Brother's a little muddy see, you heroes laughed at.
OK:
Msn:[email protected] Everybody discuss it together.
-------------------------------------------------------------------------------an interface, multiple implementations ", or" A variable in the parent class that points to a subclass object calls the Subclass method "... The most classical words in the book, from the code, inheriting class methods can be, from object-oriented, or from the design, analyst staff, is an interface, a variety of implementations. What is the role of polymorphism? "One interface, multiple implementations," or "A parent class variable pointing to a subclass object calls a subclass method" ... I think both of these expressions are not very useful.
Encapsulation is the code in the object reuse class. Inheritance is the code of a class that re-uses another class. What is the role of polymorphism?
Compile-time polymorphism: function overloading
Run-time polymorphism: virtual function overriding
This is not about how it is achieved, but about its role.
Interface (parent class member function) reuse? I don't know if it's appropriate.
Polymorphism is actually a kind of behavior encapsulation, you just need to know what you manipulate the object can do (interface), then you are needed
Time to call it to do, specifically how to do it by itself to decide, you do not need to know and no need to know
Dynamic linking, which is determined by the runtime to determine which interface is called
Polymorphic: Is the encapsulation of types that are affected by a common interface, which is usually defined in an abstract base class, where
virtual function in a class, the program at runtime according to the type of a dynamic invocation of the virtual function. But should try not to use "polymorphic", it will be in the space and time to draw additional burdens!!!
(I don't know if I can help you, but I have try my best!!! Laughing and joking)//performance, but a lot of time, the interface can be said to be a multi-state re-ascension
Polymorphism is ... For example, my men have three little boys, one day I said: "To collect the protection fee for me?gt;> flagellation Asher shun grin ⒘ take a stick, went to the first meal to mess, each other scared silly, into the hospital; B called 200 in his hands, went to the hall a sitting, the other side also frightened crazy, please his brother to the gear to eat a meal, He was diagnosed with a neurasthenia; C a person to go, with the other head of tea chat chit chat when, finally the other party was driven crazy, pay the money ... To get the same command, three people each have their own solution, which is polymorphic: multiplexing the same interface to achieve different operations. Polymorphism: Properties and services defined in a generic class do not change their names in special classes, but can have different data types or behave differently by their different implementations. I went on top:
What is the role of polymorphism? "One interface, multiple implementations"
My grandfather used a knife when he hit Japan, and my father used a 38-type rifle when he hit Japan. If we call this generation now, we'll use a missile.
It can be seen that our interface forms (member functions) are the same, but the way we operate them is different from the results.
Polymorphism is also the case. It is relative to inheritance. Form: the "virtual" keyword is used in the base class.
The pointer to the parent class is the one that can point to the child class
Does it use Lao Tzu's or son's behavior to call the same interface? If the interface function is a virtual function, then I'm sorry.
That's the son's call, isn't it, or is dad big?
Excuse me. Let everyone laughed at.
I add that I said, no inheritance there is no polymorphism ~ ~ ~ Polymorphism means fewer function names. Can have nothing to do with inheritance. Overload
The concept and role of polymorphism (in-depth understanding)