In C ++ virtual functions and pure virtual function

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/leon_zeng0/article/details/89268584

Has been a bit empty for virtual functions, specially google a bit, the following article is almost 2, reproduced below, while adding some of my test results.

https://blog.csdn.net/yusiguyuan/article/details/12676177

https://blog.csdn.net/Hackbuteer1/article/details/7558868

First of all: to emphasize a concept
defined function as a virtual function, does not mean that the function is a function of not being realized.
He is defined as a virtual function in order to allow the base class pointer to call this function subclass.
Is defined as a pure virtual function is a function of only the representative function is not implemented.
Defines a pure virtual function in order to implement an interface, plays a role specification, the programmer of this specification inheriting class must implement this function.
1. Introduction
Suppose we have the following class hierarchy:

#include <iostream>

class A
{
public:
	virtual void foo()
	{
		std::cout << "A::foo() is called" << std::endl;
	}
};
class B :public A
{
public:
	void foo()
	{
		std::cout << "B::foo() is called" << std::endl;
	}
};
int main(void)
{
	A *a = new B();
	a->foo(); // 在这里,a虽然是指向A的指针,但是被调用的函数(foo)却是B的!

	B bt;
	A& aa = bt;
	A aaa;
	aa.foo();
	aaa.foo();

	return 0;
}


     This example is a typical application of virtual functions, through this example, maybe you have some concept of virtual functions. It is false to false on so-called "deferred binder" or "dynamic binding", call a class function is not to be determined at compile time, but is determined at run time. Since the preparation of the code can not be invoked to determine the function of which is a function of the derived class or base class, it is a "virtual" function.

This program is compiled and linked under vs2017 c ++, and the result is that the program is running:

B::foo() is called
B::foo() is called
A::foo() is called

3 A class object, a pointer to the first one (the original implementation is B), the second is a reference. B, 3 A is a class object. So although all A class object, the results are not the same.

    By means of a virtual function pointer or reference only to achieve the effect of polymorphism.
C ++ pure virtual function
, the definition
 pure virtual function is declared in the base class virtual function, it is not defined in the base class, but require any derived class must define your own implementation. A method to achieve a pure virtual function in the base class in the function prototype plus "= 0"
 Virtual void funtion1 () = 0
Second, the introduction of the reason
  1, in order to facilitate the use of multi-state properties, we often need to define a virtual function in the base class .
  2, in many cases, objects are generated base class itself unreasonable. For example, an animal as a base class can be derived tigers, peacock subclass of the animal itself generates a significant anomaly objects.
  To solve the above problems, the concept of a pure virtual function, the function will be defined as a pure virtual functions (methods: virtual ReturnType Function () = 0 ;), requires the compiler must be overridden in a derived class to achieve polymorphism . Class also contains pure virtual function is referred to as an abstract class, it is not generated object. This solves both of these problems.
It declares a pure virtual function of the class is an abstract class. Therefore, the user can not create an instance of the class, it can only create an instance of the derived class.
Pure virtual function most notable features are: they must re-declare the function in a derived class (not the back = 0, otherwise the derived class can not be instantiated), but they are often not defined in the abstract class.
The purpose of defining pure virtual function in that the derived classes inherit just the interface functions.
Meaning pure virtual function, so that all class objects (mainly derived class objects) can perform actions pure virtual function, but the class can not provide a reasonable default implementation for the pure virtual functions. So pure virtual function declaration class is a subclass of the designer told, "You must provide an implementation of pure virtual functions, but I do not know how you will achieve it."

Introduce an abstract class
abstract class is a special class, which is for the purpose of design abstraction and for the establishment of, it is in the upper layers inheritance hierarchy.
(1) defines an abstract class: class with a pure virtual function is an abstract class.
Effect (2) abstract classes:
primary role abstract class is related to the operation as a result of the interface tissue in an inheritance hierarchy, to provide a common root for the derived class by its derived class will be specifically implemented in the base class as an operation of the interface. Therefore, the derived class actually depicts a set of actions common semantics interface subclass, the subclass pass these semantics, specific subclasses can implement these semantics, the semantics can then pass their subclasses.
(3) Note that when using an abstract class:
• abstract class can be used as a base class to implement pure virtual function which is given by the derived classes. If the derived class does not redefine pure virtual function, but only inherited pure virtual function in the base class, the derived class it is still an abstract class. If the derived class is given in the base class pure virtual function, the derived class is not an abstract class, and it is a specific object can create a class.
• An abstract class can not define the object.

Summary:
1, pure virtual function declared as follows: virtual void funtion1 () = 0 ; pure virtual function is not necessarily defined, pure virtual function code of conduct for the derived class, i.e. the interface. It contains pure virtual class is an abstract class, an abstract class instance can not be defined, but can declare a pointer pointing to the concrete classes implement the abstract class or reference.
2, virtual function declared as follows: virtual ReturnType FunctionName (Parameter); virtual functions must be achieved, if not achieved, the compiler error, error is:
error LNK ****: --unresolved External Symbol "public: void __thiscall Virtual ClassName: : virtualFunctionName (void) "
. 3, to the virtual function, the parent class and subclass has its own version. When called by the multi-state mode dynamic binding.
4, subclass that implements pure virtual functions, the pure virtual function in the subclass to the programming virtual function, i.e., subclass grandchildren subclass can override this class virtual function, when called by the multi-state mode dynamic binding.
5, virtual function in C ++ is the mechanism used to achieve polymorphism (polymorphism) of. The core concept is the base class to access the derived class definition through.
6, when there is dynamically allocated on the heap memory of the virtual destructor function must be, but need not be pure imaginary.
7, the friend is not a member function, it can only be a virtual member functions, so a friend can not be virtual functions. But it can make a virtual friend function call virtual member functions to solve the problem of a friend.
8, the destructor should be virtual function calls the appropriate object type destructor, therefore, if the pointer points to a subclass object, calls the destructor subclasses, and then automatically calls the destructor for the base class.

Pure virtual function class is abstract, the object can not be generated, only derived. He derived class pure virtual function is not rewritten, then it's derived class is an abstract class.
Defines a pure virtual function is to allow instantiation of the base class can not
because the instance of the abstract data structure itself of such not meaningful.
Or give realize there is no sense
in fact I personally think that the introduction of a pure virtual function, is for two purposes
1, for safety, the unknown outcome because the need to avoid any explicit but because of carelessness caused remind subclass to do It should be done to achieve.
2, for efficiency, the efficiency of the program is not executed, but to the coding efficiency.
 

Guess you like

Origin blog.csdn.net/leon_zeng0/article/details/89268584