The "polymorphism" of C + +

Source: Internet
Author: User

Overview of polymorphism first, the definition1, polymorphism: refers to the same message is accepted by different types of objects resulting in a completely different behavior. Polymorphism is divided into multi-state at compile time and runtime polymorphism.
2. Message: Refers to a call to a member function of a class.
3, compile-time polymorphism: is the process of compiling the same name to determine the operation of the specific object.
4, the operation of the polymorphic: in the process of running the program to dynamically determine the specific object of the operation.
5, the binder: Determine the operation of the specific object of the process, also known as binding.
6. Static linking: When compiling and connecting.
7, dynamic linking: at run time.

comparison of static and dynamic- linking seriesthe static binder executes efficiently, executes fast, and is flexible in dynamic linking, but slow in execution.


operator Overloading One, operator overloading1, definition: This mechanism that causes different behavior when the same operator is acting on different types of data.
2, Advantages: extended the function of the operator, than the general function call more concise, intuitive, increase the readability of the program.

second, operators that do not have overloading1, the member selection operator.
2, Member pointer operator. *
3. Scope Resolution::
4, trinocular selection operator? :
5. Calculate Data size operator sizeof

second, overloaded rules for operators1. The precedence and binding of the operator after overloading does not change.
2. The number of operands of the original operator cannot be changed.
3. You cannot overload an operator that is not in C + +.
4, the original semantics of the operator cannot be changed.

Three, the friend function of overloading as class1. Definition: A friend function that overloads an operator as a class to declare the operator function as a friend function in a class by using the Friend keyword.
2. Function: You can freely access any data member of the class.
Four, overloading is a member function of a class1. Definition: Overloading an operator function as a member function of a class.
2. Function: You can freely access the data members of this class.

The characteristics of the member functions of the overload class1. When the binocular operator is overloaded as a member function of a class, the function explicitly describes only one parameter, which is the right operand of the operator.
2. When the predecessor operator is overloaded as a member function of a class, you do not need to explicitly describe the parameter, that is, the function has no formal parameters.
3. When the post-order operator is overloaded as a member function of a class, the function must have an integer parameter in order to distinguish it from the preceding single-mesh operator.

The comparison between the member function and the friend function of the overload class1. The monocular operator is best overloaded as a member function of a class; The binocular operator is best overloaded as a friend function of a class.
2, =, (), [], and the binocular operator cannot be overloaded as a friend function of a class.
3. A type conversion function can only be defined as a member function of a class and cannot be defined as a friend function of a class.
4, the operation of an operator needs to modify the state of the object, select overload for the member function is better.
5. The operand required by the operator expects an implicit type to be loaded, only the friend function is selected.
6. When an operation that requires overloading operators is commutative, select overload as a friend function.



Virtual Functions first, the definitionallowing the linkage between function calls and function bodies to be established at run time is the basis for implementing dynamic linking.
second, the characteristics1, virtual function has a function body.
2. Virtual function is used as a general member function when statically linked.
3, virtual functions can be derived, if the virtual function is not redefined in the derived class, the virtual function acts as a virtual function of the derived class. 4. Virtual function is not a static member function, nor is it a friend function.
5. Virtual functions are defined inside the class.
6. Only the member function of a class can be described as a virtual function, and 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 class first, abstract classAbstract class is a special class, it is established for abstract purposes, through its polymorphic use of the member functions, for a class family to provide a unified interface.
second, pure virtual function1, definition: is a virtual function described in the base class.
2. Function: Provide a consistent interface for derived classes.
three, void functiona virtual function in which the body of a function is empty.

the difference between pure virtual function and void function1, pure virtual function has no function body; void function body is empty.
2, the pure virtual function is the class is abstract class, can not be directly instantiated; The class where the void function resides can be instantiated.

The "polymorphism" of C + +

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.