C + + polymorphism, virtual function, pure virtual function, abstract class, virtual base class

Source: Internet
Author: User

One, C + + polymorphic

The polymorphism of C + + includes static polymorphism and dynamic polymorphism. Static polymorphism includes function overloading and generic programming, and dynamic polymorphism includes virtual functions. Static polymorphism means that during compilation, dynamic polymorphism is defined only when the program is running.

Second, virtual function

1, the virtual function is a non-static member function of the class, access permissions are generally public. When a function is declared, the virtual keyword is added before the return value, and the function definition does not need to be added to virtual. The virtual function defined by the parent class, when the subclass inherits, can redefine the virtual function, of course, the function of the subclass should be the same as the parent virtual function, but the function implementation is not the same. We use the pointer of the parent class to point to an instance of the subclass, and then we can invoke the member function of the actual subclass through a pointer to the parent class.

2. The constructor cannot be a virtual function

3, when there are virtual functions in the base class, the destructor of the general base class is also defined as the virtual destructor. If you do not define a virtual destructor, when you delete a pointer to a derived class object, the destructor for the base class is called, and the destructor for the derived class is not called, causing a memory leak. After defining a virtual destructor, the destructor for the bottommost derived class is called first, and then the destructor for each base class is called.

Three, pure virtual function

A pure virtual function is a virtual function at the time of Declaration, the end of the function is added = 0, the pure virtual function has no function definition, and its subclasses need to inherit the pure virtual function of all base classes and give the definition. The purpose of pure virtual functions is to provide a consistent interface for derived classes.

Iv. Abstract class

Abstract classes are classes that contain pure virtual functions, which cannot create objects, but can declare pointers and references.

Five, virtual base class

1. If a derived class has more than one direct base class, and these direct base classes have a common base class, then in the final derived class, multiple copies of the same name members of the indirect common base class data member are preserved. When referencing these members with the same name, you must increase the direct base class name after the derived class object name to avoid ambiguity.

Example:

Class A
{

Public

int m_nnum;

};
Class B:p ublic A
{...};
Class C:p ublic A
{...};

Class D:p ublic b,public C
{
};

D D1;

D1. A::m_nnum;

2, C + + provides virtual base class, so that when inheriting the indirect common base class, only one member is reserved.

Example:

Class A
{

Public

int m_nnum;

};
Class B:virtual Public A
{...};
Class C:virtual Public A
{...};

Class D:p ublic b,public C
{
};

D D1;

D1.m_nnum;

3, in the last derived class not only to be responsible for the initialization of the direct base class, but also responsible for the virtual base class initialization. The C + + compilation system only performs calls to the constructor of the virtual base class from the last derived class, while ignoring other derived classes of the virtual base class (b and C) calls to the virtual base class's constructors, guaranteeing that the virtual base class member is not initialized more than once.

For example:

Class D:p ublic b,public C
{
D (int n): A (n), B (n), C (n) {}
};

The order of construction is to construct the topmost base class first, and then the parent class who first inherits who first constructs, the order of the destructor and the constructor order in reverse.

C + + polymorphism, virtual function, pure virtual function, abstract class, virtual base class

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.