Example for Virtual Function and
Pure Virtual Function
1
Prepared by
Dr.S.Raja Ratna
2
Virtual Function
● A virtual function is a member function which is declared within a
base class and is re-defined (overridden) by a derived class.
● They are mainly used to achieve Runtime polymorphism. The
resolving of function call is done at runtime.
● It is not mandatory for the derived class to override (or re-define the
virtual function), in that case, the base class version of the function
is used.
● Functions are declared with a virtual keyword in base class.
Rules for Virtual Functions
● Virtual functions cannot be static.
● A virtual function can be a friend function of another class.
● Virtual functions should be accessed using pointer or reference of base
class type to achieve runtime polymorphism.
● The prototype of virtual functions should be the same in the base as well
as derived class.
● They are always defined in the base class and overridden in a derived
class.
● A class may have virtual destructor but it cannot have a virtual
constructor.
3
Example 1 (Virtual Function)
#include<iostream>
using namespace std;
class base {
public:
virtual void print()
{
cout << "print base classn"; }
void show()
{
cout << "show base classn";
}
};
class derived : public base {
public:
void print()
{
cout << "print derived classn";
}
4
void show()
{
cout << "show derived classn";
}
};
int main()
{
base *bptr;
derived d;
bptr = &d;
// Virtual function, binded at runtime
bptr->print();
// Non-virtual function, binded at compile time
bptr->show();
return 0;
}
Output:
print derived class
show base class
5
Explanation for Example 1:
● Runtime polymorphism is achieved only through a pointer (or reference) of
base class type.
● Also, a base class pointer can point to the objects of base class as well as
to the objects of derived class. In above code, base class pointer ‘bptr’
contains the address of object ‘d’ of derived class.
● Late binding (Runtime) is done in accordance with the content of pointer )
and Early binding (Compile time) is done according to the type of pointer.
● Since print() function is declared with virtual keyword so it will be bound at
runtime (output is print derived class as pointer is pointing to object of
derived class) and show() is non-virtual so it will be bound during compile
time (output is show base class as pointer is of base type).
6
Example 2 (Virtual Function)
#include <iostream>
class A
{
int x=5;
public:
void display()
{
cout << "Value of x is : " << x;
}
};
class B: public A
{
int y = 10;
public:
void display()
{
cout << "Value of y is : " <<y;
}
};
7
int main()
{
A *a;
A a1;
B b;
a = & a1;
a1->display();
a = &b;
a->display();
return 0;
}
Output:
Value of x is : 5
Value of y is : 10
8
Explanation for Example 2
• In the above example, * a is the base class pointer.
• The pointer can only access the base class members but not the members
of the derived class.
• Although C++ permits the base pointer to point to any object derived from
the base class, it cannot directly access the members of the derived class.
• Therefore, there is a need for virtual function which allows the base pointer
to access the members of the derived class.
9
Pure Virtual Function
• A virtual function is not used for performing any task. It only serves as a
placeholder.
• When the function has no definition, such function is known as "do-
nothing" function.
• The "do-nothing" function is known as a pure virtual function. A pure
virtual function is a function declared in the base class that has no definition
relative to the base class.
• A class containing the pure virtual function cannot be used to declare the
objects of its own, such classes are known as abstract base classes.
10
Example (Pure Virtual Function)
#include <iostream>
class Base
{
public:
virtual void show() = 0;
};
class Derived : public Base
{
public:
void show()
{
cout << "Derived class is derived from the base class.“ ;
}
};
11
int main()
{
Base *bptr; //Base b;
Derived d;
bptr = &d;
bptr->show();
return 0;
}
Output:
Derived class is derived from the base class.
● In the above example, the base class contains the pure virtual function.
Therefore, the base class is an abstract base class. We cannot create
the object of the base class.
12

Example for Virtual and Pure Virtual function.pdf

  • 1.
    Example for VirtualFunction and Pure Virtual Function 1 Prepared by Dr.S.Raja Ratna
  • 2.
    2 Virtual Function ● Avirtual function is a member function which is declared within a base class and is re-defined (overridden) by a derived class. ● They are mainly used to achieve Runtime polymorphism. The resolving of function call is done at runtime. ● It is not mandatory for the derived class to override (or re-define the virtual function), in that case, the base class version of the function is used. ● Functions are declared with a virtual keyword in base class.
  • 3.
    Rules for VirtualFunctions ● Virtual functions cannot be static. ● A virtual function can be a friend function of another class. ● Virtual functions should be accessed using pointer or reference of base class type to achieve runtime polymorphism. ● The prototype of virtual functions should be the same in the base as well as derived class. ● They are always defined in the base class and overridden in a derived class. ● A class may have virtual destructor but it cannot have a virtual constructor. 3
  • 4.
    Example 1 (VirtualFunction) #include<iostream> using namespace std; class base { public: virtual void print() { cout << "print base classn"; } void show() { cout << "show base classn"; } }; class derived : public base { public: void print() { cout << "print derived classn"; } 4
  • 5.
    void show() { cout <<"show derived classn"; } }; int main() { base *bptr; derived d; bptr = &d; // Virtual function, binded at runtime bptr->print(); // Non-virtual function, binded at compile time bptr->show(); return 0; } Output: print derived class show base class 5
  • 6.
    Explanation for Example1: ● Runtime polymorphism is achieved only through a pointer (or reference) of base class type. ● Also, a base class pointer can point to the objects of base class as well as to the objects of derived class. In above code, base class pointer ‘bptr’ contains the address of object ‘d’ of derived class. ● Late binding (Runtime) is done in accordance with the content of pointer ) and Early binding (Compile time) is done according to the type of pointer. ● Since print() function is declared with virtual keyword so it will be bound at runtime (output is print derived class as pointer is pointing to object of derived class) and show() is non-virtual so it will be bound during compile time (output is show base class as pointer is of base type). 6
  • 7.
    Example 2 (VirtualFunction) #include <iostream> class A { int x=5; public: void display() { cout << "Value of x is : " << x; } }; class B: public A { int y = 10; public: void display() { cout << "Value of y is : " <<y; } }; 7
  • 8.
    int main() { A *a; Aa1; B b; a = & a1; a1->display(); a = &b; a->display(); return 0; } Output: Value of x is : 5 Value of y is : 10 8
  • 9.
    Explanation for Example2 • In the above example, * a is the base class pointer. • The pointer can only access the base class members but not the members of the derived class. • Although C++ permits the base pointer to point to any object derived from the base class, it cannot directly access the members of the derived class. • Therefore, there is a need for virtual function which allows the base pointer to access the members of the derived class. 9
  • 10.
    Pure Virtual Function •A virtual function is not used for performing any task. It only serves as a placeholder. • When the function has no definition, such function is known as "do- nothing" function. • The "do-nothing" function is known as a pure virtual function. A pure virtual function is a function declared in the base class that has no definition relative to the base class. • A class containing the pure virtual function cannot be used to declare the objects of its own, such classes are known as abstract base classes. 10
  • 11.
    Example (Pure VirtualFunction) #include <iostream> class Base { public: virtual void show() = 0; }; class Derived : public Base { public: void show() { cout << "Derived class is derived from the base class.“ ; } }; 11
  • 12.
    int main() { Base *bptr;//Base b; Derived d; bptr = &d; bptr->show(); return 0; } Output: Derived class is derived from the base class. ● In the above example, the base class contains the pure virtual function. Therefore, the base class is an abstract base class. We cannot create the object of the base class. 12