"Polymorphism" and Polymorphism of C ++

Source: Internet
Author: User

"Polymorphism" and Polymorphism of C ++
Summary of PolymorphismI. Definition1. Polymorphism: it means that the same message is completely different when it is accepted by different types of objects. Polymorphism can be divided into polymorphism during compilation and polymorphism during runtime.
2. Message: refers to the call to the member functions of the class.
3. polymorphism during compilation: the specific operation object for the same name operation is determined during compilation.
4. polymorphism during running: it is used to dynamically determine the specific object for the operation only when the program is running.
5. Association: The process of determining the specific object of an operation, also known as binding.
6. Static concatenation: this can be performed during compilation and connection.
7. Dynamic Editing: this can be performed at runtime.

Ii. Comparison between static Association and Dynamic AssociationThe static concatenation program has high execution efficiency and fast execution speed. The dynamic concatenation program is flexible, but the execution speed is slow.


Operator overloadI. Operator Overloading1. Definition: This mechanism causes different behaviors when the same operator acts on different types of data.
2. Advantages: extends the functions of operators, which are simpler, more intuitive, and more readable than ordinary functions.

2. Do not have heavy-duty operators1. member selection operator.
2. member pointer operator .*
3. Scope delimiters ::
4. What are the three choice operators? :
5. Calculate the data size operator sizeof

Ii. Operator Overloading rules1. The priority and associativity of the operators after overloading will not change.
2. The number of operands of the original operator cannot be changed.
3. operators not available in C ++ cannot be overloaded.
4. The original semantics of operators cannot be changed.

Iii. overload the membership function of the class1. Definition: To overload an operator as a class, you must use the friend keyword in the class to declare the operator as a friend function.
2. Role: You can freely access any data member of the class.
4. Reload a member function as a class1. Definition: Reload an operator function as a member function of the class.
2. Role: You can freely access data members of this category.

5. Features of member functions with heavy loads as classes1. When binary operators are overloaded as class member functions, the function explicitly specifies only one parameter, which is the right operand of the operator.
2. When the pre-single-object operator is overloaded as a class member function, parameters do not need to be explicitly stated, that is, the function does not have a form parameter.
3. When the postpaid single-object operator is overloaded as a class member function, to distinguish it from the prepaid single-object operator, the function must have an integer parameter.

Vi. Comparison between member functions and friend functions with heavy-duty classes1. It is best to reload the single object operator as a member function of the class; the binary operator is better to overload as a friend function of the class.
2. The =, (), [], and-> binary operators cannot be used as class-related friend functions.
3. A type conversion function can only be defined as a member function of a class, rather than a friend function of a class.
4. An operator operation needs to modify the object state. It is better to select reload as a member function.
5. If you want to replace the operands required by the operator with an implicit type, you can only select a friend function.
6. When the operation that requires the overload operator is interchangeable, select the overload as a friend function.



Virtual FunctionsI. DefinitionThe connection between function calls and function bodies is established at runtime, which is the basis for implementing dynamic Association.
Ii. Features1. virtual functions have function bodies.
2. virtual functions are used as common member functions during static Association.
3. A virtual function can be derived. If a virtual function is not redefined in a derived class, the virtual function acts as a virtual function of the derived class. 4. virtual functions are neither static member functions nor friend functions.
5. virtual functions are defined within the class.
6. Only a member function of a class can be described as a virtual function. Its Declaration can only appear in the definition of a class.
7. The constructor cannot be a virtual function, and the Destructor can be a virtual function.

Abstract classI. abstract classesAn abstract class is a special class created for the purpose of abstraction. It uses member functions in polymorphism to provide a unified operation interface for a class family.
Ii. Pure virtual functions1. Definition: a virtual function described in the base class.
2. Purpose: provide a consistent interface for the derived class.
Iii. Empty FunctionsA virtual function with an empty function body.

Iv. Differences between pure virtual functions and empty Functions1. Pure virtual functions do not have function bodies; empty function bodies are empty.
2. classes where pure virtual functions are located are abstract classes and cannot be instantiated directly. classes where empty functions are located can be instantiated.

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.