Implementation of Polymorphism

Source: Internet
Author: User

The polymorphism forms include:
Overwrite: the input parameters are the same, but different Derived classes have different implementations. The polymorphism is reflected in the runtime stage, that is, a parent class pointer can point to different derived class objects, call different functions with the same name.
Overload: A member function of a class can have multiple functions with the same name with different parameters, and its polymorphism is reflected.


I. Polymorphism

Polymorphism literally means "multiple shapes ". Reference Charlie Calverts's description of polymorphism-polymorphism is a technology that allows you to set a parent object to be equal to one or more of its sub-objects, the parent object can operate in different ways based on the features assigned to its sub-objects (from "delphi4 Programming Technology insider "). To put it simply, you can assign a pointer of the subclass type to a pointer of the parent class. Polymorphism is implemented by virtual functions in Object Pascal and C ++.

Polymorphism is a technique that allows the parent object to be set to be equal to one or more of its sub-objects, such as parent: = Child; polymorphism allows the same class (base class) type pointer to reference objects of different classes, and perform the same operation in different ways according to different referenced objects.
The role of polymorphism: Different subclass objects are treated as parent classes, which can shield the differences between different subclass objects and write common Code Make general programming to adapt to the changing needs.
After a value is assigned, the parent object can operate in different ways based on the features of the sub-objects assigned to it. That is to say, a father acts like a son, rather than a father.
For example, a virtual command is returned from a base class to generate different results.
For example, if multiple objects are inherited from a base class, the base class has a virtual method tdoit, and its subclass also has this method, but the behavior is different, then any of these sub-objects can be attached to their base classes, so that the objects of their base classes can perform different operations. In fact, you are accessing its sub-objects through its base class. All you need to do is assign values.
The result of inheritance is that you can create a class family. When you know the class family, You can regard the exported class object as the base class object. This kind of understanding is also called upcasting. The importance of this understanding is that we can write only a section for the base class Program But it can adapt to the family of this class, because the compiler will automatically find the appropriate object to execute the operation. This phenomenon is also known as polymorphism. The method for realizing polymorphism is also called dynamic binding ).
To put it simply, to create a parent class variable, its content can be the parent class or its sub-class. When the sub-class has the same function as the parent class, when this variable is used to call this function, the class that defines this variable, that is, the parent class, will be called for the function with the same name, when the virtual keyword is added before the function in the parent class, the function with the same name of the subclass will be called.
Class {
A (){}
Virtual void Foo (){
Cout <"This Is A." <Endl;
Class B: Public {
B (){}
Void Foo (){
Cout <"This is B." <Endl;
Int main (INT argc, char * argv []) {
A * A = new B ();
A-> Foo ();
Return 0;
This will show:
This is B.
If the virtual is removed, the following information is displayed:
This is.
The preceding polymorphism implementation uses abstract classes and defines virtual methods.

Ii. Heavy Load

Overload decision is a compile-time mechanism. It is used to select an optimal function member to implement the call when a parameter list and a group of candidate function members are given. Function overloading means that there are several functions with the same name in a class, but the parameter table is different:

The overload is divided into the overload of common methods and the overload of the Virtual Methods of the base class (that is, the parent class!

A normal method overload refers to two or more methods in the class (including hidden inherited methods). The names are the same, but the type or number of parameters used are different!

Overloading of base-class methods is another special form of function overloading. Redefine this virtual function in a derived class! Method Name, return value type, number of parameters in the parameter table, type, sequence must be exactly the same as the virtual functions in the base class! To declare an overload of a virtual method in a derived class, you must add the override keyword to the Declaration, and there cannot be a new, static, or virtual modifier! (This is reposted by others. In fact, this is override)

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: 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.