background
@
J
v
$
I
0
s
<
f
K
Q
Q
J
#
H
#
-
f
A
9
Y
;
n
3
e
f
r
e
:
j
g
t

favicon
favicon
Reverier's Blog
 

C++ Virtual Functions

Reverier-Xu at 2020-06-18 09:11:33 C++ Learning CC-BY-NC-SA 4.0

A quick primer on the basic idea behind C++ virtual functions.

Introduction

Virtual functions

A virtual function is a member function declared with the virtual keyword in a base class. When you call that function through a base-class pointer or reference, C++ allows the derived-class implementation to be dispatched instead.

Pure virtual functions

Declaring a function as pure virtual means that the base class provides only the declaration, not an implementation. A derived class must override that function before it can be used. Since the base class itself does not provide an implementation, the base class cannot be instantiated and used as a normal object.

  1. Definition A pure virtual function is a virtual function declared in a base class without a definition there, while requiring every derived class to provide its own implementation. In the base class, this is done by appending = 0 to the function declaration: virtual void function1() = 0;

  2. Why it exists

  1. To make polymorphism convenient, we often need virtual functions in base classes.
  2. In many situations, instantiating the base class itself simply does not make sense. For example, Animal may serve as a base class for Tiger, Peacock, and other derived classes, but constructing Animal directly is not especially meaningful. To solve that problem, C++ introduces pure virtual functions. Once a function is declared pure virtual (virtual ReturnType Function() = 0;), the compiler requires derived classes to override it to participate in polymorphism. A class containing a pure virtual function is called an abstract class, and it cannot be instantiated. That means users can only instantiate derived classes, not the abstract base class itself. The most visible feature of a pure virtual function is that derived classes must redeclare and implement it (without the trailing = 0, otherwise the derived class also remains abstract). Pure virtual functions typically have no definition in the abstract base class. The point of defining a pure virtual function is to let derived classes inherit only the interface. In other words, a pure virtual function tells derived-class authors: “You must provide an implementation here, because I cannot give a reasonable default one.”

From Difference between virtual functions and pure virtual functions - hackbuteer1

Abstract classes

A class containing pure virtual functions is called an abstract class.

An abstract class can only be used as a base class for inheritance. If a derived class still does not provide definitions for the inherited pure virtual functions, then that derived class is abstract as well.

Abstract classes cannot be instantiated as objects.

Experiment

// virtualFunc.cpp - virtual function experiment
#include <stdio.h>
 
class Father {
  public:
    virtual void virtualFunc() {
        printf("Virtual Function of Father class\n");
    }
    void commonFunc() {
        printf("Common function of Father class\n");
    }
};
 
class Son : public Father { // Father must be public here, or the base class becomes inaccessible
  public:
    virtual void virtualFunc() {
        printf("Virtual Function of Son class\n");
    }
    void commonFunc() {
        printf("Common function of Son class\n");
    }
};
 
int main () {
    Father *p = new Son();
    p->virtualFunc();
    p->commonFunc();
    return 0;
}

Result:

result

Wrapping up

Apache553 was right: you never really finish learning C++.