In C ++, Concatenation refers to the Association of different parts of a computer program. According to the different stages of association editing, there are two different association methods: static Association editing and Dynamic Association editing.
1. Static Association
Static Association refers to the completion of association compilation in the compilation phase. This association process is completed before the program runs, also known as early Association. To implement static concatenation, you must determine the relationship between the operation calls (such as function calls) in the program and the code that executes the operation in the compilation phase, the bundle during compilation is called static bundle. The function of static concatenation is based on the pointer to an object or the referenced type. Its advantage is high efficiency, but poor flexibility.
Example 1: static Association
#include "iostream.h"class A{public:void f() {cout<<"A"<<" ";}};class B:public A{public:void f() {cout<<"B"<<endl;}};void main(){ A *pa = NULL;A a; B b;pa=&a; pa- >f();pa=&b; pa- >f();}
}
The running result of this program is:
From the running result of the program in Example 1, we can see that the call of common member functions through object pointers is only related to the pointer type, but not to the object that the pointer is pointing to at the moment. To perform different operations when the Pointer Points to different objects, you must define the corresponding member functions in the base class as virtual functions for Dynamic Association.
2. Dynamic Association
Dynamic Association refers to the dynamic Association of functions when the program is running. It determines which function with the same name is called according to the current situation. In fact, it is the implementation of virtual functions during the runtime. Such association is also known as late association or dynamic bundle. Dynamic concatenation of member functions is based on the object type, different compilation results are made for different object types. In C ++, static Association is generally used, but dynamic association should be used when polymorphism and virtual functions are involved. The advantage of Dynamic Association editing is its high flexibility but low efficiency. Dynamic Association stipulates that virtual functions can only be called by referring to the pointer of the base class or the reference of the base class object. The format is: refers to the pointer variable name of the base class-> the reference name of the virtual function name (real parameter table) or base class object. virtual function name (real parameter table)
The following three conditions must be met to implement dynamic Association: ① The Dynamic Association behavior must be defined as a class virtual function. ② Classes must satisfy the Subtype Relationship. Generally, a class is derived from another class. ③ You must first use a base class pointer to point to a child type object, and then directly or indirectly use a base class pointer to call a virtual function.
Example 2: Dynamic Association
# Include "iostream. H "Class A {public: Virtual void F () // virtual function {cout <" A "<" ";}}; Class B: Public A {public: virtual void F () // virtual function {cout <"B" <Endl ;}}; void main () {A * pA = NULL; A; B; pa = & A; pa-> F (); Pa = & B; pa-> F ();}
}
The running result of this program is: A B
From the running result of the example 2 program, we can see that after defining function F in the base class A as a virtual function, different operations are performed when the Pointer Points to different objects, it implements Dynamic Association.
3. Dynamic Association Analysis
Dynamic concatenation requires that the virtual function in the derived class and the virtual function corresponding to the base class have the same name, the same number of parameters, the same corresponding parameter type, return value, or the same, or both return pointers or references, and the pointer or base type returned by the virtual function of the derived class is the pointer returned by the virtual function of the base class or the child type of the referenced base type. If these conditions are not met, the virtual functions in the derived class will lose their virtual features and conduct static association during the call.
Example 3: Use a pointer to the base class to call a virtual function
# Include "iostream. H "class base {public: Virtual void fun1 () {cout <" base fun1 "<Endl;} virtual void fun2 () {cout <"base fun2" <Endl;} void fun3 () {cout <"base fun3" <Endl;} void fun4 () {cout <"base fun4" <Endl ;}}; class derived: public base {public: Virtual void fun1 () {cout <"derived fun1" <Endl;} virtual void fun2 (int x) {cout <"derived fun2" <Endl;} virtual void fun3 () {cout <"derived fun3" <Endl;} void fun4 () {cout <"derived fun4" <Endl ;}}; void main () {base * pb; derived D; Pb = & D; // call the virtual function Pb by pointing to the base class pointer-> fun1 (); Pb-> fun2 (); pb-> fun3 (); Pb-> fun4 ();} running result of the program: derived fun1base fun2base fun3base fun4
In this example, the function fun1 uses the keyword virtual defined as a virtual function in the base class and the derived class derived, and these two virtual functions have the same number of parameters, parameter type, and return value type. Therefore, when the pointer petabytes accesses the fun1 function, dynamic Association is used. Function fun2 is defined as a virtual function in the base class and the derived class de-rived, but the two virtual functions have different parameter numbers. Function fun2 loses its virtual feature and performs static association during calling. Function fun3 is a general function in the base class. It is defined as a virtual function in the derived class de-rived. In this case,
The feature of the member functions described in the base class should be taken as the standard, that is, function fun3 is a general member function and uses static concatenation during calls. The function fun4 is described as a general function in the base class and the derived class derived. Therefore, the base class pointer Pb can only access members in the base class.
Example 4: Call a virtual function by referencing a base class Object
# Include "iostream. H "class cpoint {public: cpoint (double I, Double J) {x = I; y = J;} virtual double area () // virtual function {return 0.0 ;} PRIVATE: Double X, Y;}; Class crectangle: Public cpoint {public: crectangle (double I, Double J, Double K, double L); double area () {return w * h;} PRIVATE: double W, H;}; crectangle: crectangle (double I, Double J, Double K, double L): cpoint (I, j)
{W = K; H = L;} void fun (cpoint & S) {cout <S. area () <Endl;} // call the void main () {crectangle Rec (3.0, 5.2, 15.0, 25.0) virtual function by referencing a base class object ); fun (REC);} the program runs as follows: 375
In Example 4, the member function area uses the keyword virtual to define a virtual function in the base class cpoint. It is defined as a general function in the derived class crectangle, but is dynamically associated, the result is 15*25 or 375. This is because a virtual function retains its virtual feature no matter how many times it is inherited by the public. When you redefine a virtual function in a derived class,
Virtual keywords can be written or not written, but to maintain a good programming style, avoid confusionEnter this keyword.
4. Summary
From the above four examples, we can see that virtual functions are the basis for implementing polymorphism and one of the necessary conditions for implementing dynamic Association. Dynamic concatenation depends on virtual functions. They complement each other and are indispensable.