Polymorphism and virtual function--c/c++ Learning notes

Source: Internet
Author: User

Polymorphism (polymorphism) that is , sending the same message to different objects, different objects will have different behavior when they are received . the so-called message is a function call. in C + +, polymorphism is: functions with different functions can have the same function name. that is, the implementation uses the same function name to invoke different function contents .
C + + polymorphism is divided into: static polymorphism and dynamic polymorphism
Static polymorphism : through function overloading or operator overloading implementations , the full invocation of a function is known at compile-time, and is also referred to as the compiler-state polymorphism. Determines which function to perform based on the context of the expression.
Advantages: Fast call speed, high efficiency; disadvantage: lack of flexibility.
Dynamic polymorphism : The polymorphic that is implemented at run time, that is, the object to which the operation is to be known at run time. Implemented by virtual functions . ?
Dynamic Polymorphism The problem to solve is that the base class has multiple derived classes, and allows derived classes to have the same member name as the base class member (including the function name, variable name), when invoking the class object with the same member name at run time, Which object's members are called? Is it a member of a base class object or a member of a derived class object?

a derived class object can override a base class object's reference to a base class object to initialize or assign a value.
A virtual function is one in which the base class declares that a function is virtual, not a function that actually exists, and is formally defined in a derived class. The problem that the virtual function solves is: A pointer to a base class that accesses a function in a derived class with the same name as a base class when pointing to a derived class .
originally, if the declaration is not a virtual function, then a pointer to the base class, it is simply pointing to the base class object. If you use it to point to a derived class, an implicit type conversion occurs, and a pointer to the derived class is converted to a pointer to the base class. at this point, the base classthe pointer points to the part of the base class in the derived class object.
Note:non-virtual functions that are defined in the base class are redefined in the derived class. Then, if the member function is called with a pointer to the base class, the system invokes the member function of the base class part of the object, and if the member function is called with a pointer to a derived class, the system invokes the member function in the derived class object. This is not a polymorphic behavior because virtual functions are not used.
the key points of dynamic polymorphism are:
    1. virtual function, member function with the same name
    2. Base class pointers
    3. when you point to a different derived class, you are accessing the current of a derived class object. The member function.
function overloading differs from virtual function:
function overloading handles problems with the same name function at the same level. Transverse overloadingvirtual functions deal with the same-name function problem at different levels. Vertical overloadingAlso note: function overloading requires that the number of arguments to a function or function return type be different. virtual function requires that the function name, function type, function parameter number and type must be consistent with the base class virtual function .

The two points to note when using virtual functions are: 1, only the member functions of the class can be declared as virtual functions, and it is not possible to declare the ordinary functions outside the class as virtual functions. The reason for this is that virtual functions allow derived classes to redefine virtual functions, apparently only in the class's inheritance hierarchy. 2. After a member function is declared as a virtual function, it is not possible to define a non-virtual function with the same name again in the same class family (that is, the function name, number of arguments, type, whatever)
virtual function TableSpecial note: The use of virtual functions is a certain amount of space overhead.when a class has virtual functions, the compilation system constructs a virtual function table for the class (virtual functionstable,vtable), which is an array of pointers that holds the entry address for each virtual function .
Virtual destructor When an object of a derived class is purged from memory, the destructor of the derived class is typically called first, and then the destructor of the base class is called. But, however, the important thing to say three times. There is a situation where a temporary object is established with the new keyword, there is a destructor in the Destructors class, and a pointer variable is defined that points to the base class. When a delete is used to destroy the pointer object, a situation occurs: The system executes only the destructor of the base class, not the destructor of the derived class. Because it is not a virtual function, and there is no polymorphism, the pointer is assigned to a derived class object, but only to the base class part in the derived class. Therefore, only the destructor of the base class is executed.
It is better to declare the destructor of the base class as a virtual destructor if the destructor is performed safely and correctly in the Inheritance!!!
Pure virtual functionOnly the function prototype is given, there is no function body. A function that is initialized to "= 0" when the virtual function is declared. Declares the general form of a pure virtual function: virtual function type function name (argument list) = 0;Note: There is a semicolon at the end. A pure virtual function has no function body. = 0 is only an identity, tell the compiler that this is a pure virtual function, there is no practical meaning.
Pure virtual function only function name does not have function function, can not be called!!! The role is to preserve the function name for the derived class so that the derived class defines it as needed. Polymorphism is not possible if the function name is not reserved.
All classes that contain pure virtual functions are abstract classes. Abstract classes are not able to create objects. Because pure virtual functions cannot be called, they cannot be instantiated, that is, objects are created. However, you can create a pointer to an abstract class to implement a polymorphic call.










Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Polymorphism and virtual function--c/c++ Learning notes

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.