Quote:
how to create a virtual function and what is use of it
One usage is to create abstract classes for example:
class Employee
{
...
virtual void getSalary() = 0; ...
};
Another usage, suppose you have two classes Parent and Child. The Child class inherits from Parent class and Parent class has its own display function which will display for example "I'm parent", and the Child class has an overridden function display which will display for example "I'm child".
And you've this function:
void mFunction(Parent &p)
{
p.display();
}
If you
don't use virtual keyword for display function (virtual void display();) in parent's class then no matter what you pass Parent class object or Child class object it will display "I'm parent".
If you use virtual keyword
then it will display "I'm parent" if it is a Parent class object
or it will display "I'm child" if it is a child class object.
There might be other usages that I don't know about and I hope someone will help you with that.
Good luck.
Regards,
Mohammed Abdulgaffar.