transferred from: http://www.cnblogs.com/dongsheng/p/3343939.htmldefinition of a 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 that any derived class define its own implementation method. The method of implementing a pure virtual function in a base class is to add "= 0" after the function prototype
Second, the reasons for the introduction
1, in order to facilitate the use of polymorphic features, we often need to define virtual functions in the base class.
2, in many cases, the base class itself is unreasonable to generate objects. For example, animals as a base class can be derived from tigers, peacocks and other sub-categories, but the animals themselves generated objects are obviously unreasonable.
In order to solve the above problems, the concept of pure virtual function is introduced, the function is defined as pure virtual function (method: Virtual ReturnType functions () = 0;), the compiler requirements must be overloaded in derived classes to achieve polymorphism. A class that contains a purely virtual function is called an abstract class, and it cannot produce an object. This is a good solution to both of these problems.
Iii. Concept of similarity
1. Polymorphism
A different implementation action occurs when the same object receives different messages or when different objects receive the same message. C + + supports two kinds of polymorphism: compile-time polymorphism, run-time polymorphism.
A. Compile-time polymorphism: implemented by function overloading and operator overloading.
B. Run-time polymorphism: implemented through inheritance and virtual functions.
2. Virtual function
A virtual function is a member function that is declared as virtual in a base class and redefined in a derived class to implement dynamic overloading of member functions.
the declaration of a pure virtual function has a special syntax format: virtual return value type member function name (parameter table) = 0;
Note that pure virtual functions should only be declared, without a specific definition, even if the definition of a pure virtual function is ignored by the compiler.
3. Abstract class
Classes that contain pure virtual functions are called abstract classes. Because an abstract class contains a pure virtual function that is not defined, an object of an abstract class cannot be defined.
In C + +, we can set a class that is only used to be inherited and cannot create objects directly to abstract class.
The reason why an abstract class exists is that it has an element of uncertainty. We do exist in those classes, but the member functions in the parent class that are not able to determine the specific implementation are called pure virtual functions. Pure virtual function is a special virtual function, it only declares, there is no specific definition. At least one pure virtual function exists in an abstract class, and a class with a pure virtual function must be an abstract class. Existence of pure virtual function is a necessary and sufficient condition for becoming abstract class.
Examples of programs:
1 //base class:2 classA3 {4 Public:5 A ();6 voidF1 ();7 Virtual voidF2 ();8 Virtual voidF3 () =0;9 Virtual~A ();Ten }; One A //sub-class: - classD | PublicA - { the Public: - B (); - voidF1 (); - voidF2 (); + voidf3 (); - Virtual~B (); + }; A at //Main function: - intMainintargcChar*argv[]) - { -A *m_j=NewB (); -M_j->F1 (); -M_j->F2 (); inM_j->f3 (); - DeleteM_j; to return 0; + } - /* the F1 () is a normal overload. * call M_J->F1 () and call the F1 () in Class A, which is set when we write the code well. Because F1 () is not a virtual function, it is not bound dynamically. $ That is, it is defined by the Class A, so that the function of the class is called. Panax Notoginseng F2 () is a virtual function. - call M_J->F2 (); the F2 () function in B is called. This is because new is a B object. the F3 () is just like F2 (), except that the write function implementation is not required in the base class. + */
[C + +] C + + abstract class