Virtual function in c++
Virtual Function:
Virtual means existing in effect but not in reality. A type of function that appears to exist in some part of the program but does not exist really is called a virtual function.
Virtual functions are used to implement polymorphism. They enable the user to execute completely different functions by the same function call. A virtual function is defined in the parent class and is overridden in child classes. It is defined by the keyword virtual.
If you want to call a virtual function in C++ and execute the redefined version of the member function, you can achieve this task using a pointer or a reference ( -> ) by referring a derived class object to the base class.
A base class pointer has the capability to point to the objects of the base class and the derived class. But it's converse is not true.
An example of using virtual function with reference:
#include<iostream>
using namespace std;
class Animal
{
public:
virtual void eat() { cout << "I'm eating generic food."; }
};
class Cat : public Animal
{
public:
void eat() { cout << "I'm eating a rat."; }
};
class Rat : public Animal
{
public:
void eat() { cout << "I'm eating cheese.";}
};
int main(){
Animal animal1;
Cat cat1;
Rat rat1;
Animal *p;
p=&animal1;
p->eat();
p=&cat1;
p->eat();
p=&rat1;
p->eat();
return 0;}
This will output as:
I'm eating generic food.I'm eating a rat.I'm eating cheese.
The concept of virtual function seems a little bit similar to function overriding. Here it is explained why we use virtual functions instead of function overriding:
The reference is from https://stackoverflow.com/a/2392656/13123596
Here is how I understood not just what virtual
functions are, but why they're required:
Let's say you have these two classes:
class Animal
{
public:
void eat() { std::cout << "I'm eating generic food."; }
};
class Cat : public Animal
{
public:
void eat() { std::cout << "I'm eating a rat."; }
};
In your main function:
Animal *animal=new Animal;
Cat *cat=new Cat;
animal->eat(); // Outputs: "I'm eating generic food."
cat->eat(); // Outputs: "I'm eating a rat."
So far so good, right? Animals eat generic food, cats eat rats, all without virtual
.
Let's change it a little now so that eat()
is called via an intermediate function (a trivial function just for this example):
// This can go at the top of the main.cpp file
void func(Animal *xyz) { xyz->eat(); }
Now our main function is:
Animal *animal = new Animal;
Cat *cat = new Cat;
func(animal); // Outputs: "I'm eating generic food."
func(cat); // Outputs: "I'm eating generic food."
Uh oh... we passed a Cat into func()
, but it won't eat rats. Should you overload func()
so it takes a Cat*
? If you have to derive more animals from Animal they would all need their own func()
.
The solution is to make eat()
from the Animal
class a virtual function:
class Animal
{
public:
virtual void eat() { std::cout << "I'm eating generic food."; }
};
class Cat : public Animal
{
public:
void eat() { std::cout << "I'm eating a rat."; }
};
Main:
func(animal); // Outputs: "I'm eating generic food."
func(cat); // Outputs: "I'm eating a rat."
Done.
Why we need a virtual function?
Rules for virtual function in c++:
1.Since we already specify the member function to be a virtual function in the base class with the help of the “virtual” keyword. Now we need not again specify it while defining it in the derived class.
2.A member function of the base class cannot be a virtual function and a friend function simultaneously.
3.A member function of the base class cannot be a virtual function with the keyword static.
4.We can access virtual functions only with the help of a pointer or a reference.
5.The basic model of the virtual function in the base class should be similar while redefining it in the derived class.
6.It is not necessary to redefine the virtual function in the derived class. If the virtual function is not overridden, then we can use the original function declared or defined in the base class.
7.It is not possible to introduce a virtual constructor inside the class, although it is possible to introduce a virtual destructor.
What is the output of this program?
#include <iostream>
using namespace std;
class Weapon {
public:
void loadFeatures() { cout << "Loading weapon features.\n"; }
};
class Bomb : public Weapon {
public:
void loadFeatures() { cout << "Loading bomb features.\n"; }
};
class Gun : public Weapon {
public:
void loadFeatures() { cout << "Loading gun features.\n"; }
};
int main() {
Weapon *w = new Weapon;
Bomb *b = new Bomb;
Gun *g = new Gun;
w->loadFeatures();
b->loadFeatures();
g->loadFeatures();
return 0;
}
Try it yourself before seeing the output.
Great! if you want to learn freelancing, visit at Https://InternetKiDunya.Com
ReplyDelete