C + + polymorphism and other knowledge points

Source: Internet
Author: User

Distinguish between virtual and pure virtual functions:
(1). Virtual function is a function before the keyword virtual, the general definition format is:
Virtual type function name (parameter table)

function body

(2). The life format of the pure virtual function is:
Virtual type function name (parameter table) = 0;
That is, there is no function body.

(3). Classes with pure virtual functions are abstract classes:
Cases:
Class name

Virtual type function name (parameter table) = 0;
} is an abstract class.

About Abstract Classes:
(1). An abstract class cannot define an object.
(2). Abstract classes can only be used as base classes.
(3). Constructors cannot be pure virtual functions, destructors can be pure virtual functions.

About virtual functions:
(1). Only member functions of a class can be described as virtual functions, because virtual functions only apply to
The class object of the inheritance relationship, so normal cannot be described as a virtual function.
(2). An inline function cannot be a virtual function, because an inline function determines its location at compile time.
(3). The constructor cannot be a virtual function, because the object is still an amorphous space when constructed.
(4). Destructors can be virtual functions, and they are usually declared as virtual functions.

Summarize:
(1). When a member function is defined as a virtual function in a base class, the virtual function defined in its derived class
Must have the same name as a virtual function in the base class, and the type, order, and number of arguments must correspond to one by one.
(2). To achieve this dynamic polymorphism, you must use a pointer variable or reference of the base class type to make the
Pointers point to objects of different derived classes and can achieve dynamic polymorphism by invoking a virtual function that the pointer refers to.


Basic steps to achieve polymorphism:
(1). In the base class, define member functions as virtual functions (virtual);
(2). Defines the public (common) derived class of the base class.
(3). "Overloads" the virtual function in the public derivation of the base class.
(4). Defines a pointer variable that points to the base class, which points to the object of the base class's public derived class.

Note: Overloading a virtual function is not a generic overloaded function, it requires a function name, return type, number of arguments
The parameter types and order are exactly the same.

C + + polymorphism and other knowledge points

Related Article

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.