virtual function, pure virtual function, virtual base class, abstract class, virtual function inheritance, virtual inheritance
Virtual functions: Virtual functions are the mechanisms used in C + + to implement polymorphism (polymorphism). The core idea is to access the function defined by the derived class through the base class. is an important embodiment of polymorphism in C + +, using a base class pointer to access a member function in a derived class, in which case a virtual function is used, in which case the dynamic binding technique is used.
A virtual function must be a non-static member function of a base class whose access rights can be protected or public, and define the general form of a virtual function in the class definition of a base class: The virtual function returns a value type virtual function name (formal parameter list)
{function Body}
Dynamic binding: Whether the base class pointer calls a member function in a derived class or calls a member function in a base class to be determined when the program runs. The main thing to look at is the object that the pointer points to.
Pure virtual function: A pure virtual function is a virtual function declared in a base class that is not defined in the base class, but requires any derived class to define its own implementation method. The way to implement pure virtual functions in a base class is to add "= 0" after the function prototype
virtual void Funtion1 () =0
Virtual base class, abstract class: A class that contains pure virtual functions is called an abstract class. Because an abstract class contains a pure virtual function that is not defined, you cannot define an object for an abstract class.
virtual function Inheritance:
Virtual function inheritance is overwrite. A virtual function in a base class is overridden by a function of the same name in a derived class. is the way to achieve polymorphism.
Class Parent
{
Public
vitual void foo () {cout < < "Foo from parent";
void Foo1 () {cout < < "foo1 from parent";
};
Class Son:public Parent
{
void foo () {cout < < "Foo from son";
void Foo1 () {cout < < "foo1 from Son";};
};
int main ()
{
Parent *p=new son ();
P->foo ();
P->foo1 ();
return 0;
}
Its output results are:
Foo from son,foo1 from parent
Virtual Inheritance:
Resolves a fuzzy problem with a derived class member function call in multiple inheritance. For example, Class A has a function of print (), Class B inherits A, Class C inherits A, Class D inherits Class B and Class C, and this time, there are two print functions in Class D, one is inherited from B, and one is inherited from C, A print-blurred compilation error occurs when the object of Class D calls the print function. Solution: Class B Virtual inheritance A. Class C virtual inheritance A, when Class D inherits B,c, only copies the data members and function members of a once, and then ignores it when it encounters a copy.
Virtual inheritance is to save memory, he is a unique concept of multiple inheritance. Applies with the Diamond inheritance form.
such as: Class B, C Inherit class A,d inheritance Class B and C. To conserve memory space, you can define the inheritance of B and C to a as virtual inheritance, at which point A is a virtual base class.
Class A;
Class B:public vitual A;
Class C:public vitual A;
Class D:public B,public C;