How to Implement C ++ polymorphism through Functions

Source: Internet
Author: User

C ++ polymorphism is mainly reflected in virtual function calls. In C ++, virtual function calls use dynamic binding, and some people say it is late binding, that is, the function that is called is determined only when the program runs. Even if the programmer does not know the object type, the program can still make a behavior suitable for this object type.

Simply put, a virtual function is to add the virtual keyword before the function prototype. Once a function is declared as a virtual function, it is still a virtual function even if the class does not declare it as a virtual function when it is rewritten. If a function in the base class is declared as a virtual function and has different implementations in several subclasses, when we create different objects in several subclasses, you can use a base class pointer or reference to specify the subclass object and call the function of the corresponding subclass.

The virtual function is defined as follows:

◆ Static member functions of the class cannot be defined as virtual functions;
◆ Class constructors cannot be defined as virtual functions;
◆ Non-class functions cannot be defined as virtual functions.

A pure virtual function is a function initialized to 0 when a virtual function is declared.

 
 
  1. #include<memory> 
  2. #include<iostream> 
  3. using namespace std;  
  4. Virtual void print() const = 0;

Abstract class

In short, classes with one or more unimplemented pure virtual functions are abstract classes. If a class inherits from an abstract class but does not fully implement all pure virtual functions in the parent class, those pure virtual functions are still pure virtual in the subclass, this subclass is still an abstract class and cannot be instantiated.

An abstract class is a special class that can only be used as a base class. Its pure function implementation is provided by the derived class. Although the abstract class cannot be instantiated, we can declare the pointer and reference of an abstract class. when instantiating an object, we can use different subclasses to implement the multi-polymorphism operation. Only when the derived class implements all pure virtual functions in the base class, it is no longer an abstract class.

C ++ Polymorphism

Broadly speaking, C ++ polymorphism refers to the ability of a program to process multiple types of objects. In C ++, polymorphism can be implemented through forced polymorphism, heavy load polymorphism, type parameterized polymorphism, and inclusion polymorphism. The so-called polymorphism means that through the inheritance of classes, the same function can make different responses based on the type of the called object. It also inherits and reloads to form three Object-Oriented Programming features.

C ++ polymorphism is implemented through virtual functions. When we call virtual functions using a base class pointer, the program selects its own function implementation based on the object attributes. Even if the programmer does not know the object type, the program can still make the action that suits the object type.

C ++ supports two types of polymorphism: one is the polymorphism static polymorphism at compile time) and the other is the polymorphism dynamic polymorphism at runtime ). Polymorphism during compilation is implemented through static association, while polymorphism during runtime is implemented through dynamic Association.

  1. Differences between standard input implementation methods in C and C ++
  2. How does the C ++ compiler allocate storage space for Const constants?
  3. Basic Conception and method of C ++ Class Library Design
  4. Several Methods for converting C ++ Language
  5. How to better compile C ++ code

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.