A virtual function is a member function in a base class that can be overridden in a derived class. When a virtual function is called using a pointer or reference to a base class object, the appropriate derived class function is called based on the type of the object pointed to or referenced.Here is an example program showing the use of virtual function:#include <iostream>using namespace std;class Shape { protected: ...
Showing posts with label virtual function. Show all posts
Showing posts with label virtual function. Show all posts
Wednesday, March 1, 2023
Friday, February 17, 2023
When do we use virtual function? Differentiate early and late binding.
Niraj shrestha
February 17, 2023
0
Virtual functions are used in object-oriented programming to achieve polymorphism. In particular, virtual functions are used in the following scenarios:When a base class pointer points to an object of the derived class, and we want to call a member function of the derived class through the base class pointer. This is known as runtime polymorphism, and it requires the use of virtual functions.When a base class has one or more functions that are intended to...