First: emphasize a concept
Defining a function as a virtual function does not mean the function is not implemented.
Defining it as a virtual function is to allow using a base class pointer to call this function of the derived class.
Defining a function as a pure virtual function means the function has not been implemented.
Defining a pure virtual function is to implement an interface, serving a normative role, requiring programmers who inherit this class to implement this function.
1. Introduction
Suppose we have the following class hierarchy:
class A
{
public:
virtual void foo()
{
cout<<"A::foo() is called"<<endl;
}
};
class B:public A
{
public:
void foo()
{
cout<<"B::foo() is called"<<endl;
}
};
int main(void)
{
A *a = new B();
a->foo(); // 在这里,a虽然是指向A的指针,但是被调用的函数(foo)却是B的!
return 0;
}
This example is a typical application of virtual functions. Through this example, you may have some concept of virtual functions. It is "virtual" in the sense of so-called "late binding" or "dynamic binding." The call to a class function is not determined at compile time, but at runtime. Since when writing code, it cannot be determined whether the function being called is the base class's function or which derived class's function, it is called a "virtual" function.
Virtual functions can only achieve polymorphism with the help of pointers or references.
C++ Pure Virtual Functions
I. Definition
A pure virtual function is a virtual function declared in the base class. It has no definition in the base class, but requires any derived class to define its own implementation method. The way to declare a pure virtual function in the base class is to add "=0" after the function prototype.
virtual void funtion1()=0
II. Reason for Introduction
- 1. To facilitate the use of polymorphism, we often need to define virtual functions in the base class.
- 2. In many cases, it is unreasonable for the base class itself to instantiate objects. For example, an animal as a base class can derive subclasses such as tiger and peacock, but an animal itself instantiating an object is clearly unreasonable.
To solve the above problems, the concept of pure virtual functions was introduced. By defining a function as a pure virtual function (method: virtual ReturnType Function() = 0;), the compiler requires that derived classes must override it to achieve polymorphism. Meanwhile, a class containing pure virtual functions is called an abstract class, which cannot instantiate objects. This nicely solves the above two problems.
A class that declares a pure virtual function is an abstract class. Therefore, users cannot create instances of the class, only instances of its derived classes.
The most notable feature of pure virtual functions is that they must be re-declared in the derived classes (without the "=0" at the end, otherwise the derived class cannot be instantiated either), and they often have no definition in the abstract class.
The purpose of defining pure virtual functions is to make derived classes inherit only the interface of the function.
The significance of pure virtual functions is to allow all class objects (mainly derived class objects) to perform the actions of the pure virtual function, but the class cannot provide a reasonable default implementation for the pure virtual function. Therefore, declaring a pure virtual function in a class is telling the designer of the derived class, "You must provide an implementation of the pure virtual function, but I don't know how you will implement it."
Introduction to Abstract Classes
An abstract class is a special type of class, established for the purpose of abstraction and design. It occupies the upper levels of the inheritance hierarchy.
(1)Definition of abstract class:A class with pure virtual functions is called an abstract class.
-
(2)Role of abstract classes:The main role of an abstract class is to organize related operations as result interfaces into an inheritance hierarchy, providing a common root for derived classes. Derived classes concretely implement the operations that serve as interfaces in their base classes. So derived classes actually describe the common semantics of a set of subclasses' operation interfaces. These semantics are also passed to subclasses, which can concretely implement these semantics or pass them on to their own subclasses.
-
(3)Notes when using abstract classes:a、An abstract class can only be used as a base class, and the implementation of its pure virtual functions is provided by derived classes. If a derived class does not redefine the pure virtual function but only inherits the base class's pure virtual function, then this derived class is still an abstract class. If a derived class provides an implementation of the base class's pure virtual function, then this derived class is no longer an abstract class; it is a concrete class that can create objects.b、Abstract classes cannot define objects.
Summary:
-
1. A pure virtual function is declared as follows:virtual void funtion1()=0;A pure virtual function definitely has no definition. Pure virtual functions are used to regulate the behavior of derived classes, i.e., the interface. A class containing pure virtual functions is an abstract class. An abstract class cannot define instances, but pointers or references to concrete classes that implement the abstract class can be declared.
-
2. A virtual function is declared as follows:virtual ReturnType FunctionName(Parameter);A virtual function must be implemented. If it is not implemented, the compiler will report an error, with an error message like:
error LNK****: unresolved external symbol "public: virtual void __thiscall ClassName::virtualFunctionName(void)"
-
3. For virtual functions, both the parent class and the derived class have their own versions. When called in a polymorphic way, dynamic binding is used.
-
4. In a derived class that implements a pure virtual function, that pure virtual function becomes a virtual function in the derived class. The derived class's subclass (i.e., the grandchild class) can override this virtual function. When called in a polymorphic way, dynamic binding is used.
-
5. Virtual functions are the mechanism in C++ for implementing polymorphism. The core concept is to access functions defined in derived classes through the base class.
-
6. When dynamically allocating memory on the heap, the destructor must be a virtual function, but it does not need to be pure virtual.
-
7. Friends are not member functions. Only member functions can be virtual, so friends cannot be virtual functions. However, the virtual problem with friends can be solved by having the friend function call a virtual member function.
-
8. The destructor should be a virtual function, which will call the destructor of the corresponding object type. Therefore, if the pointer points to a derived class object, the derived class's destructor will be called, and then the base class's destructor will be automatically called.
A class with pure virtual functions is an abstract class; it cannot create objects and can only be derived from. If the pure virtual function is not overridden in its derived class, then that derived class is still an abstract class.
Defining pure virtual functions is precisely to make the base class non-instantiable.
Because instantiating such an abstract data structure itself is meaningless.
Or providing an implementation is also meaningless.
Actually, personally I think the introduction of pure virtual functions serves two purposes:
-
1. For safety, to avoid any unknown results caused by carelessness when clarity is needed, reminding derived classes to do the implementations they should do.
-
2. For efficiency, not the efficiency of program execution, but the efficiency of coding.
Original address: http://blog.csdn.net/hackbuteer1/article/details/7558868