Easy Tutorial
❮ Verilog Deassign Android Tutorial Development Environment Build ❯

The Difference Between Virtual Functions and Pure Virtual Functions

Category Programming Techniques

>

Firstly: Emphasize a Concept

Defining a function as a virtual function does not mean that the function is not implemented.

Defining it as a virtual function is to allow the function to be called by a pointer of the base class.

Defining a function as a pure virtual function means that the function has not been implemented.

Defining a pure virtual function is to implement an interface, serving as a standard, which stipulates that programmers inheriting this class must 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(); // Here, although a is a pointer to A, the function (foo) called is B's!
   return 0;
}

This example is a typical application of a virtual function. Through this example, you may have some understanding of virtual functions. It is virtual in the sense of "deferred binding" or "dynamic binding". The call to a class function is not determined at compile time, but at runtime. Since it is not possible to determine at the time of writing the code whether the base class function or which derived class function is called, it is called a "virtual" function.

Virtual functions can only achieve polymorphism through pointers or references.


C++ Pure Virtual Functions

I. Definition

A pure virtual function is a virtual function declared in the base class. It is not defined in the base class but requires any derived class to define its own implementation method. The method to implement a pure virtual function in the base class is to add "=0" after the function prototype.

virtual void function1() = 0

II. Reasons for Introduction

To solve the above problems, the concept of pure virtual functions is introduced. If a function is defined as a pure virtual function (method: virtual ReturnType Function() = 0;), the compiler requires that it must be overridden in the derived class to implement polymorphism. At the same time, a class containing pure virtual functions is called an abstract class, and it cannot generate objects. This well 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, but can only create instances of its derived classes.

The most notable feature of pure virtual functions is that they must be re-declared in the inheriting class (without the =0 at the end, otherwise the derived class cannot be instantiated), and they often have no definition in the abstract class.

The purpose of defining a pure virtual function is to make the derived class only inherit the interface of the function.

The significance of a pure virtual function is to allow all class objects (mainly derived class objects) to perform the actions of a pure virtual function, but the class cannot provide a reasonable default implementation. Therefore, the declaration of a pure virtual function is to tell the designer of the subclass, "You must provide an implementation of a pure virtual function, but I don't know how you will implement it."


Introduction to Abstract Classes

An abstract class is a special class that is established for the purpose of abstraction and design. It is in a higher position in the inheritance hierarchy.

-

(1) Definition of Abstract Class: A class with a pure virtual function is called an abstract class.

-

(2) Role of Abstract Class: The main role of an abstract class is to organize related operations as a result interface in an inheritance hierarchy, providing a common root for derived classes, which will implement the operations as an interface in their base class. Therefore, the derived class actually portrays a common semantic interface of a group of subclasses, which is also passed on to the subclasses. The subclasses can implement these semantics specifically or pass them on to their own subclasses.

-

(3) Notes on Using Abstract Classes: a, Abstract classes can only be used as base classes, and the implementation of their pure virtual functions is provided by the derived classes. If the derived class does not redefine

❮ Verilog Deassign Android Tutorial Development Environment Build ❯