C + + Language Note Series 18--Virtual function (1)

Source: Internet
Author: User

Polymorphism in the 1.c++
(1) Polymorphism: The invocation of the same function can perform different operations, and function overloading is a means of realizing polymorphism.


(2) Link: Join during the compilation phase. That is, the point of Call of a function is joined with the defining point of the function during the compilation phase.
A. Static linking: function overloading, which is completed at compile-time.


B. Dynamic linking: At the execution stage of the program, the system chooses the detailed function-the virtual function at its own initiative.


Note: The polymorphism of C + + mainly refers to dynamic linking.
2. Virtual functions
(1) A virtual function can be declared as a virtual function when the function is defined.
(2) Description: Virtual data type function name (reference) {function Body}
A. Purpose: When a virtual function is called by a base-class pointer, the system is dynamically linked.
B. When a function is defined in a derived class that has the same name as the base class, the same number of parameters, the same number of parameters, and the same type of return value. A member function of the same name in the Destructors class is a virtual function, and a member function of the same name in the derived class is self-virtualized.


C. Static linking is used when visiting virtual functions through objects.

A dynamic union can be completed only by pointing to the object's pointer and the object's reference.
3. Examples
Example 1

#include <iostream.h>

Class Point (
{
Private
float x, y;
Public
void SetPoint (float i, float j)
{
x = i; y = j;
}
Float area () {return 0.0;} Equivalent to: float area () = 0;
};
const float pi=3.1416;
Class Circle:pulic Point
{
Private
float rad;
Public
void Setrad (float r) {rad = R;}
Float area () {return pi*rad*rad;}
};
int main ()
{
Point P;
Circle C;
float a = P.area ();
cout<<a<<endl;
C.setrad (2.5);
A = C.area ();
cout<<a<<endl;
}
Analysis:
Float A=p.area () calls the area function of the point class: P is the object of the base class point, and the object invokes the function with a static binder; The object invocation of the point class Area,area must be the Area;a=c.area () of the point class. Called is the area function of the Circle class, due to the proximity principle.


Change the main function:

int main ()
{
Point *p;
Circle C;
float A;
C.setrad (2.5);
p = &c;
A = P->area ();//The function is a static binder
cout<<a<<endl;
}
Program output:
0.0
Analysis: Although a base-class pointer points to a derived class object, the derived class object can only be referenced from the base class by a pointer to the base class to inherit past members. If you want a base-class pointer to point to a member of a derived class, use a virtual function to implement a dynamic union.


4. Four types of static linking
(1) The base class pointer points to the base class object, which invokes the base class member with static union.
(2) A pointer to a derived class refers to an object of a derived class that uses a static binder to invoke a derived class member. If a derived class does not have a member. Call this member of the base class.


(3) The base class pointer points to the derived class object, using a static binder to invoke the base class member.


(4) The object reference member must be a static binder. A base class object references a base class member, and a derived class object references a derived class member, and a derived class can reference a base class member if it is not.
Note: A derived class pointer cannot point to a base class, nor can a reference be sufficient.
5. Dynamic linking
(1) Condition: a function with the same name as the base class is declared as a virtual function. The base-class pointer points to the derived class object.


(2) Function: Use dynamic linking. Enables a base-class pointer to access member functions of multiple derived classes, which can only be implemented by pointers or references (provided the virtual function mechanism).
Example 2

#include <iostream.h>

Class Point
{
Public
Virtual double area () {return 0.0;}
};
Class Rectangle:public Point
{
Double length, width;
Public
Rectangle (Double L, double W): Length (L), Width (w) {}
Double area () {return length*width;}
};
Class Circle:public Point
{
Double radium;
Public
Circle (Double r) {radium = R;}
Double area () {return 3.1416*radium*radium;}
};
Class Triangle:public Point
{
Double x, y, Z;
Public
Triangle (double A, double b, double c)
{x = A; y = b; z = C;}
Double area () {return (x+y+z) *0.5;}
};
int main ()
{
Point *p;
Rectangle R (5.0, 2.0);
Circle C (6.0);
Triangle T (3.0, 4.0, 5.0);
p = &r;
Cout<<p->area () <<endl;
p = &c;
Cout<<p->area () <<endl;
p = &t;
Cout<<p->area () <<endl;
}
Program output:
10
113.098
6
Note: Virtual functions of the base class can also be called. Calling method: The scope of the additive class (forcing static linking).
Demo Sample:
p = &r;
P->area ();//Call the area function of the rectangle class
P->point::area ();//forcing a static union to invoke the area function under the action of the Point class
6. Access permissions for virtual functions
(1) Virtual functions in derived classes do not affect dynamic linking, and virtual functions of base classes are necessary to ensure dynamic linking.
(2) A virtual function in a class has only an effect on a function that is redefined by a derived class and has no effect on its base class members.


Example 3

#include <iostream.h>

Class A
{
Public
virtual void fun1 ()
{cout<< "fun1 () ... fun2 ()" <<endl; fun2 ();}
void Fun2 ()
{cout<< "fun2 () ... fun3 ()" <<endl; fun3 ();}
virtual void Fun3 ()
{cout<< "Fun3 () ... fun4 ()" <<endl; fun4 ();}
virtual void Fun4 ()
{cout<< "Fun4 () ... fun5 ()" <<endl; fun5 ();}
void Fun5 () {cout<< "the end." <<endl;}
};
Class B:public A
{
void Fun3 ()
{cout<< "Fun3...fun4" <<endl; fun4 ();}
void Fun4 ()
{cout<< "Fun4...fun5" <<endl; fun5 ();}
void Fun5 ()
{cout<< "All done." <<endl;}
};
int main ()
{
A *thing;
thing = new A;
Thing->fun1 ();
thing = new B;
Thing->fun1 ();
}
Program output:
Fun1 () ... fun2 ()
Fun2 () ... fun3 ()
Fun3 () ... fun4 ()
Fun4 () ... fun5 ()
The end.
Fun1 () ... fun2 ()
Fun2 () ... fun3 ()
Fun3...fun4
Fun4...fun5
All is done.
7. Destructors classes and derived classes have different number of functions with the same name. The member functions in the base class are virtual functions, unlike the parameter types. However, the virtual attribute is lost-static linking is used.
A member function of the same name in a base class and a derived class that has the same name as a member function in a derived class is a virtual function. Then a static binder is also used.
Example 4

#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 Device:public Base
{
Public
virtual void fun1 ()
{cout<< "Device fun1." <<endl;}
virtual void fun2 ()
{cout<< "Device fun2." <<endl;}
virtual void Fun3 ()
{cout<< "Device fun3." <<endl;}
virtual void Fun4 ()
{cout<< "Device fun4." <<endl;}
};
int main ()
{
Base *PB;
Device D;
PB = &d;
Pb->fun1 ();
Pb->fun2 ();
Pb->fun3 ();
Pb->fun4 ();
}
Program output:
Device fun1.
Device fun2.
Base Fun3.
Base Fun4.

C + + Language Note Series 18--Virtual function (1)

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.