Virtual functions and polymorphism pure virtual functions

Source: Internet
Author: User
Polymorphism)

A name with multiple semantics;

One interface and multiple methods

• Polymorphism means that when different objects receive the same message,

Different actions are generated.

• C ++ polymorphism is embodied in Run and compileThere are two aspects:

The polymorphism during program running is embodied by inheritance and virtual functions;

During program compilation, polymorphism is reflected in the overloading of functions and operators.

Ø virtual functions are member functions with the keyword virtual in the base class.

It provides an interface.

Ø virtual functions can be redefined in one or more Derived classes. Ø the key to implementing Runtime polymorphism is to describe virtual functions first,

The base class pointer must be used to call different implementation versions of the derived class.

Functions of virtual functions:

You can redefine the virtual functions of the base class in the derived class.

Ø member functions of the class can be declared using virtual, and can only be used in the inheritance hierarchy of the class.

Common functions (Friends) and static member functions outside the class cannot be declared as virtual functions.

The constructor cannot be declared as a virtual function!

Define a new virtual function in a derived class. The function name, function type, and function parameter are required.

The number and type are all identical to the virtual functions of the base class.

Otherwise, polymorphism cannot be implemented, which is a function overload.

After a member function is declared as a virtual function, a non-virtual function with the same parameter and return type cannot be defined in the same type of family as the virtual function. The virtual functions can be redefined in the derived class as needed to maintain the features of the virtual functions;

If the virtual function of the base class is not redefined, the derived class simply inherits the virtual function of the base class.

Define a new virtual function in a derived class

The function name, return type, number of parameters, parameter type, and sequence are identical.

If different parameters are defined for the virtual function in the derived class, the polymorphism mechanism cannot be implemented.

Reload functions with the same name of the base class in the derived class

If the function prototype is different and only the function name is the same, the virtual feature is lost.

The function in the base class is virtual void show () const,

The function in the derived class is void show (),

The constructor cannot be a virtual function.

When creating a derived class object, it must begin with the root of the class hierarchy,

Call the constructors of the base class one by one along the inheritance path

The Destructor can be a virtual function.

The virtual destructor is used to guide the delete operator to correctly analyze dynamic objects.

 

L functions of pure virtual functions

Reserve the name of a function for its derived class in the base class,

So that the derived class can be defined as needed.

Exist as an interface

L pure virtual functions do not have functions, and generally cannot be called directly.

Pure virtual functions inherited from the base class are still virtual functions in the derived class.

L if a class contains at least one pure virtual function, this class is called abstract class ).

Abstract classes include not only pure virtual functions, but also virtual functions.

L abstract classes must be used as the base classes for deriving other classes, rather than directly creating object instances.

However, you can still use pointers to abstract classes to support Runtime polymorphism.

L The pure virtual function in the base class should be overloaded in the derived class, otherwise it will still be treated as an abstract class. L abstract classes cannot be used as function parameter types, function return value types, or explicitly converted types.

The pointer and reference of an abstract class can be declared.

L The abstract class destructor can be declared as pure virtual functions. Virtual functions and polymorphism enable member functions to generate no

The same action.

Polymorphism is particularly suitable for software systems that implement layered structures,

It is easy to define commonalities during problem abstraction, and define differences during implementation.

(4) The abstract base class is the public interface of this class family.

That is, multiple classes derived from the same base class have the same interface.

(5) If a virtual function is declared in the base class, all functions in the derived class that have the same function name, function type, number of parameters, and type as the function are virtual functions.

 

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.