What do virtual functions do in c++ 🧐 | Sololearn: Learn to code for FREE!

+2

What do virtual functions do in c++ 🧐

Guys wat do virtual functions do , were can I use them , why should I use them , and wat is the perpose of using them🙂

7/20/2020 6:53:07 PM

Pjc

2 Answers

New Answer

+6

A C++ virtual function is a member function in the base class that you redefine in a derived class. It is declared using the virtual keyword. It is used to tell the compiler to perform dynamic linkage or late binding on the function. There is a necessity to use the single pointer to refer to all the objects of the different classes. So, we create the pointer to the base class that refers to all the derived objects. But, when base class pointer contains the address of the derived class object, always executes the base class function. This issue can only be resolved by using the virtual function.