4 Virtual-Functions12
4 Virtual-Functions12
A virtual function (also known as virtual methods) is a member function that is declared
within a base class and is re-defined (overridden) by a derived class. When you refer to a
derived class object using a pointer or a reference to the base class, you can call a virtual
function for that object and execute the derived class’s version of the method.
Virtual functions ensure that the correct function is called for an object, regardless of
the type of reference (or pointer) used for the function call.
They are mainly used to achieve Runtime polymorphism.
Functions are declared with a virtual keyword in a base class.
The resolving of a function call is done at runtime.
Virtual Functions
#include <iostream>
using namespace std;
class base {
public: virtual void print() { cout << "print base class\n";
}
void show()
{ cout << "show base class\n";
}
};
void show()
{ cout << "show derived class\n"; }
};
int main()
{
base*s;
derived d;
s = &d;