2.3. Virtual functions

Lecture



The mechanism of virtual functions is addressed in cases where each derived class requires its own version of some component function. Classes that include such functions are called polymorphic and play a special role in OOP.
Consider how non-virtual component functions with the same names, types, and parameter signatures behave when inheriting.

Example 2.3.1

Example 2.3.1

class base

{

public:

void print () {cout << “\ nbase”;}

};

class dir : public base

{

public:

void print () {cout << “\ ndir”;}

};

void main ()

{

base B, * bp = & B;

dir D, * dp = & D;

base * p = & D;

bp -> print (); // base

dp -> print (); // dir

p -> print (); // base

}

In the latter case, the print function of the base class is called, although the pointer p is set to an object of the derived class. The fact is that the choice of the necessary function is performed when the program is compiled and is determined by the type of the pointer, and not by its value. This mode is called early or static binding . Greater flexibility provides later ( delayed ) or dynamic binding , which is provided by the mechanism of virtual functions . Any non-static base class function can be made virtual by using the virtual keyword .
Example

class base
{
public:
virtual void print () {cout << “\ nbase”;}
. . .
};
// and so on - see the previous example.
In this case, will be printed
base
dir
dir

So the interpretation of each virtual function call through the pointer to the base class depends on the value of this pointer, i.e. on the type of object for which the call is made. Virtual functions are functions that are declared in the base class and are overridden in the derived classes. The class hierarchy, which is defined by open inheritance, creates a related set of user types, all objects of which can be referenced by the base class pointer. The choice of which virtual function to call will depend on the type of object to which the pointer is actually (at the time of program execution), and not on the type of pointer. Only non-static member functions can be virtual. Virtuality is inherited. After a function is defined as virtual, its redefinition in a derived class (with the same prototype) creates a new virtual function in this class, and the virtual qualifier may not be used. Constructors cannot be virtual, unlike destructors. Virtually every class that has a virtual function must have a virtual destructor. If you enter a function in the derived class with the same name and type but with a different parameter signature, then this function of the derived class will not be virtual. The virtual function may be friendly in another class. The virtual call mechanism can be suppressed by explicitly using the full qualified name.

Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

C ++ (C plus plus)

Terms: C ++ (C plus plus)