Virtual Function
A virtual function is a member function which is declared
within a base class and is re-defined (Override) by a derived
class.
When we want to use same function name in both the base and
derived class, then the function in base class is declared as
virtual by using the virtual keyword and again re-defined this
function in derived class without using virtual keyword.
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.
Virtual Function
A virtual function is a member function that is defined within a base class and redefined by a
derived class.
To create virtual function, precede the function’s in the base class with the keyword virtual.
When a class containing virtual function is inherited, the derived class redefines the virtual
function to suit its own needs.
using base class pointer if we call some function which is in both classes, then base class
function is invoked. But if we want to invoke derived class function using base class pointer, it
can be achieved by defining the function as virtual in base class, this is how virtual functions
support runtime polymorphism.
Syntax of Virtual Functions
class class_name
{
public:
-------
--------
virtual returntype fun_name(args)
{
……
……
}
….
};
class Base
{
public: Derived function without virtual
void show()
{
cout << "Base class";
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class";
}
};
void main()
{
Base* b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show();
}
class A
{
protected:
int a;
public:
virtual void show()
main()
{
{
A *p;
}
B OBJ1;
};
p=&OBJ1;
class B : public A
p->show();
{
}
int b;
public:
B()
{
a=5;
b=10;
}
void show()
{
cout<<"value of a:"<<a;
cout<<"value of b:"<<b;
} };
Class A
{
int a;
public:
A()
{
a = 1;
} main()
virtual void show() {
{ A *p;
cout <<a; A z;
} p=&z;
}; p->show();
Class B: public A B b;
{ p = &b;
int b; p -> show();
public: }
B()
{
b = 2;
}
void show()
{
cout <<b;
}};
Pure Virtual Functions
A pure virtual function is a function which has no definition in the base class.
Its definition lies only in the derived class i.e. it is compulsory for the derived class
to provide definition of a pure virtual function.
Since there is no definition in the base class, these functions can be equated to
zero.
The general form of pure virtual function is:
virtual type func-name(parameter-list) = 0;
A class containing pure virtual functions cannot be used to define any objects of its
own and hence such classes are called pure abstract classes or abstract classes .
All other classes without pure virtual functions are called concrete classes.
#include <iostream>
using namespace std;
class Base //Abstract base class
{
public:
virtual void show() = 0; //Pure Virtual Function
};
class Derived:public Base
{
public:
void show()
{ cout << "Implementation of Virtual Function in Derived class"; }
};
main()
{
//Base obj; //Compile Time Error
Base *b;
Derived d;
b = &d;
b->show();
}
Abstract Class
A pure virtual function is a function which does not have definition of its own. The
classes which derive from this class need to provide definition for such function.
A class containing at least one such pure virtual function is called as abstract base class.
We can not create objects of abstract class because it contains functions which have no
definition. We can create pointers and references to an abstract class.