Polymorphism in C ++ Learning

Source: Internet
Author: User

C ++'s class mechanism supports polymorphism technology to solve abstract programming. It uses a lagging bundling technology,
By pre-configuring the virtual function properties of the member functions, this technology enables any undefined object operations bound to the member functions to be compiled, all are encoded with an uncertain pointer,
When it comes to running, the real behavior of an object of a specific type is suddenly formulated. That is, when the runtime lags behind, the member function is bound according to the specific type of object. In this way, you can identify the object type without the need to do it.

Virtual Functions)
1. Polymorphism Condition)
In the use of class programming, abstract programming should be implemented without modifying the class. The class mechanism must solve this problem. In C ++, It is the virtual function mechanism. The operation of the base class and the derived class with the same name. As long as it is marked with virtual, the Operation has polymorphism.
The following code is used:

[Cpp]
# Include <iostream>
Using namespace std;
 
Class Base
{
Public:
Virtual void fun ()
{
Cout <"In Base class" <endl;
}
};
 
Class Sub: public Base
{
Public:
Virtual void fun ()
{
Cout <"In Sub class" <endl;
}
};
 
Void test (Base & B)
{
B. fun ();
}
 
Int main ()
{
Base bc;
Sub SC;
Test (bc );
Test (SC );
 
Return 0;
}
The code output is as follows:

[Plain]
In Base class
In Sub class

 

In the above Code, fun is a virtual function of the Base class. Once the Base class function is marked as a virtual function, all member functions with the same name in the inherited class are programmed with virtual functions, in the test function, B is the reference parameter of the Base class. Both the Base class object and Sub class object can be passed as real parameters to the form parameter B.

 

If the function parameter is transmitted by the actual copy action, the subclass object is completely changed to the base class object, so that there will be no polymorphism. For example, if you change the test function parameter in the above Code to the following format,

The following code is used:

[Cpp]
Void test (Base B)
{
B. fun ();
}
 
Int main ()
{
Base bc;
Sub SC;
Test (bc );
Test (SC );
 
Return 0;
}

The output is changed:

[Plain]
In Base class
In Base class

This is because the transfer process of this parameter has definitely changed the nature of the object, and there is no choice for the object to be determined. Therefore, for polymorphism, only indirect access to object pointers and references can cause polymorphism.

 

2. virtual function mechanism

In the above Code, B. fun () function calls show diversity. The exact location of fun cannot be determined immediately after compilation, that is, whether the Base-class Base fun function is called or the Sub-class fun function is called.

When the compiler sees the virtual function mark of fun, it puts it into the virtual function table and waits until B is encountered. when calling the virtual function fun (), the bundling operation lags behind the running, and the corresponding member function operations are actually bundled based on the actual object type. Of course, it is impossible for the compiler to trace the running program. Instead, it avoids function calling in the bundling operation B. fun () and only performs indirect access to a member function pointing to the actual object. In this way, if the actual object is a base class, the base class member function is called. if the object is a subclass, the member function of the subclass is called. Of course, each actual object must have an additional pointer space to point to the virtual function table in the class. As shown in.

 

You can see that the class of the virtual function is used, and the object space of the virtual function is more than the space of a pointer than that of the class without the virtual function, because it involves other operations, this includes indirect access to virtual functions and calculation of pointer offsets of objects. Therefore, when a virtual function is applied, the program running efficiency will be affected.

 

3. Spread of virtual functions

In the inheritance hierarchy, virtual functions are automatically transmitted from the base class. Therefore, the virtual Description of the member function fun in the Sub class in the above Code can be omitted,

For example:

In a plane shape system, the base shape class has two sub-classes: Circle class and rectangular Retangle class, when the Area function is set to virtual in the base class, the subclass' response to the same name function is virtualized.

[Plain]
# Include <iostream>
# Include <cmath>
 
Using namespace std;
 
Class Shape
{
Protected:
Double xCoord, yCoord;
Public:
Shape (double x, double y)
{
XCoord = x;
YCoord = y;
}
Virtual double area () const
{
Return 0.0;
}
};
 
Class Circle: public Shape
{
Protected:
Double radius;
Public:
Circle (double x, double y, double r): Shape (x, y)
{
Radius = r;
}
Double area () const
{
Return 3.14 * radius;
}
};
 
Class Rectangle: public Shape
{
Protected:
Double x2Coord, y2Coord;
Public:
Rectangle (double x1, double y1, double x2, double y2): Shape (x1, y1)
{
X2Coord = x2;
Y2Coord = y2;
}
Double area () const;
};
 
Double Rectangle: area () const
{
Return abs (xCoord-x2Coord) * (yCoord-y2Coord ));
}
 
Void fun (const Shape & sp)
{
Cout <sp. area () <endl;
}
 
Int main ()
{
Fun (Circle (2, 5, 4 ));
Fun (Rectangle (2, 4, 1, 2 ));
 
Return 0;
}

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.