Virtual function in C++ | Example of virtual function | Pure virtual function in c++ | Rules of virtual function in c++ | My CS Tutorial
A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don't want static linkage for this function.
What we do want is the selection of the function to be called at any given point in the program to be based on the kind of object for which it is called. This sort of operation is referred to as dynamic linkage, or late binding.
【●】 Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call.
【●】 They are mainly used to achieve Runtime polymorphism
Functions are declared with a virtual keyword in base class.
【●】 The resolving of function call is done at Run-time.
【●】Virtual functions cannot be static and also cannot be a friend function of another class.
【●】Virtual functions should be accessed using pointer or reference of base class type to achieve run time polymorphism.
【●】The prototype of virtual functions should be same in base as well as derived class.
【●】They are always defined in base class and overridden in derived class. It is not mandatory for derived class to override (or re-define the virtual function), in that case base class version of function is used.
【●】A class may have virtual destructor but it cannot have a virtual constructor.
It is possible that you want to include a virtual function in a base class so that it may be redefined in a derived class to suit the objects of that class, but that there is no meaningful definition you could give for the function in the base class.
We can change the virtual function area() in the base class to the following −
class Shape {
protected:
int width, height;
public:
Shape(int a = 0, int b = 0) {
width = a;
height = b;
}
// pure virtual function
virtual int area() = 0;
};
The = 0 tells the compiler that the function has no body and above virtual function will be called pure virtual function.
We can make base class's methods virtual by using virtual keyword while declaring them. Virtual keyword will lead to Late Binding of that method.
#include<iostream.h>
class Base
{
public:
virtual void show()
{
cout << "Base class\n";
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class";
}
}
int main()
{
Base* b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show(); //Late Binding Ocuurs
}
On using Virtual keyword with Base class's function, Late Binding takes place and the derived version of function will be called, because base class pointer pointes to Derived class object.
Virtual function in C++ | Example of virtual function | Pure virtual function in c++ | Rules of virtual function in c++ | My CS Tutorial
_______________________________________
Virtual function in C++ | My CS Tutorial |
Virtual Function
A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don't want static linkage for this function.
What we do want is the selection of the function to be called at any given point in the program to be based on the kind of object for which it is called. This sort of operation is referred to as dynamic linkage, or late binding.
【●】 Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call.
【●】 They are mainly used to achieve Runtime polymorphism
Functions are declared with a virtual keyword in base class.
【●】 The resolving of function call is done at Run-time.
Rules for Virtual Functions
【●】Virtual functions cannot be static and also cannot be a friend function of another class.
【●】Virtual functions should be accessed using pointer or reference of base class type to achieve run time polymorphism.
【●】The prototype of virtual functions should be same in base as well as derived class.
【●】They are always defined in base class and overridden in derived class. It is not mandatory for derived class to override (or re-define the virtual function), in that case base class version of function is used.
【●】A class may have virtual destructor but it cannot have a virtual constructor.
Pure Virtual Functions
It is possible that you want to include a virtual function in a base class so that it may be redefined in a derived class to suit the objects of that class, but that there is no meaningful definition you could give for the function in the base class.
We can change the virtual function area() in the base class to the following −
class Shape {
protected:
int width, height;
public:
Shape(int a = 0, int b = 0) {
width = a;
height = b;
}
// pure virtual function
virtual int area() = 0;
};
The = 0 tells the compiler that the function has no body and above virtual function will be called pure virtual function.
Simple c++ program using Virtual Keyword in C++
We can make base class's methods virtual by using virtual keyword while declaring them. Virtual keyword will lead to Late Binding of that method.
#include<iostream.h>
class Base
{
public:
virtual void show()
{
cout << "Base class\n";
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class";
}
}
int main()
{
Base* b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show(); //Late Binding Ocuurs
}
Output:
Derived classOn using Virtual keyword with Base class's function, Late Binding takes place and the derived version of function will be called, because base class pointer pointes to Derived class object.
Virtual function in C++ | Example of virtual function | Pure virtual function in c++ | Rules of virtual function in c++ | My CS Tutorial
_______________________________________
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment