Virtual functions and Polymorphism of C ++ from scratch (1): virtual function table pointer, virtual destructor, object slicing and virtual function

Source: Internet
Author: User

I. Polymorphism

Polymorphism is one of the important features of object-oriented programming.
Polymorphism means that sending the same message to receive different types of objects may lead to completely different behaviors.
Implementation of polymorphism:


Function overload

Operator overload

Template

Virtual Functions

(1) Static binding and dynamic binding

Static binding

The binding process occurs in the compilation phase and the function to be called is determined during the compilation phase.
Dynamic binding

The binding process is executed when the program is running, and the function to be called is determined only when the program is running.

 


Ii. Virtual Functions

Concept of virtual functions: virtual member functions with keywords in the base class
Definition of virtual functions:


Virtual function type function name (parameter list );

If a function is declared as a virtual function in the base class, it is a virtual function in all the derived classes.

Dynamic binding can be triggered only when virtual functions are called through base class pointers or references.
Virtual functions cannot be declared as static

 

 

(1) virtual function table pointer

Dynamic binding of virtual functions is implemented through virtual function tables. (Function pointer for storing virtual functions in the virtual function table)
The class containing the first 4 bytes of the virtual function stores the pointer to the virtual function table.


Note: if it is not a virtual function, the common function will not appear in the virtual function table, because it does not need to be indirectly accessed through the virtual function table pointer.

 

 

C ++ Code 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Include <iostream>
Using namespace std;


Class Base
{
Public:
Virtual void Fun1 ()
{
Cout <"Base: Fun1..." <endl;
}

Virtual void Fun2 ()
{
Cout <"Base: Fun2..." <endl;
}

Void Fun3 () // hidden after being inherited by Derived
{
Cout <"Base: Fun3..." <endl;
}
};

Class Derived: public Base
{
Public:
/* Virtual */
Void Fun1 ()
{
Cout <"Derived: Fun1..." <endl;
}

/* Virtual */void Fun2 ()
{
Cout <"Derived: Fun2..." <endl;
}

Void Fun3 ()
{
Cout <"Derived: Fun3..." <endl;
}
};

Int main (void)
{
Base * p;
Derived d;

P = & d;
P-> Fun1 (); // Fun1 is a virtual function. The base class Pointer Points to the object of the derived class and calls the virtual function (indirect) of the object of the derived class)
P-> Fun2 ();
P-> Fun3 (); // Fun3 is a non-virtual function. It calls the member functions of the corresponding class based on the actual type of p pointer (direct)

Base & bs = d;
Bs. Fun1 ();
Bs. Fun2 ();
Bs. Fun3 ();

D. Fun1 ();
D. Fun2 ();
D. Fun3 ();

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.