What do virtual functions do in c++ 🧐 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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🙂

20th Jul 2020, 6:53 PM
Pjc
Pjc - avatar
1 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.
22nd Jul 2020, 6:15 PM
A S Raghuvanshi
A S Raghuvanshi - avatar