Skip to main content
OOPS using C++
Dr. Satish Kumar
March 11, 2026
Dr. Satish Kumar March 11, 2026 1 / 15
Virtual Functions
Virtual function is the member function defined in the base class and
can further be defined in the child class as well
While calling the derived class, the overridden function will be called
Virtual functions must be members of some class
Virtual functions cannot be static members
Virtual functions are accessed through object pointers
A virtual function must be defined in the base class, even though it is
not used
The prototypes of a virtual function of the base class and all the
derived class must be identical
If the two functions with the same name but different prototypes,
C++ will consider them as the overloaded functions
We cannot have a virtual constructor
Dr. Satish Kumar March 11, 2026 2 / 15
Consider the situation when we don’t use the virtual keyword
1 #include <iostream >
2 using namespace std;
3
4 class A{
5 int x = 5;
6 public:
7 void display ()
8 {
9 cout << "Value of x is: " << x << endl;
10 }
11 };
12
13 class B: public A{
14 int y = 10;
15 public:
16 void display ()
17 {
18 cout << "Value of y is: " << y << endl;
Dr. Satish Kumar March 11, 2026 3 / 15
19 }
20 };
21
22 int main (){
23 A *a;
24 B b;
25 a = &b;  Concept: Base class pointer
pointing to derived class object
26 a -> display ();
27 b.display ();
28 return 0;
29 }
30
31 output:
32 Value of x is: 5
33 Value of y is: 10
Without virtual: Function call is resolved at compile time. Based on
pointer type, not object type
Dr. Satish Kumar March 11, 2026 4 / 15
Dr. Satish Kumar March 11, 2026 5 / 15
Use of virtual function I
1 #include <iostream >
2 using namespace std;
3
4 class A{
5 int x = 5;
6 public:
7 virtual void display ()
8 {
9 cout << "Value of x is: " << x << endl;
10 }
11 };
12
13 class B: public A{
14 int y = 10;
15 public:
16 void display ()
17 {
Dr. Satish Kumar March 11, 2026 6 / 15
Use of virtual function II
18 cout << "Value of y is: " << y << endl;
19 }
20 };
21
22 int main (){
23 A *a;
24 B b;
25 a = &b
26 a -> display ();
27 b.display ();
28 return 0;
29 }
30
31 output:
32 Value of y is: 10
33 Value of y is: 10
Dr. Satish Kumar March 11, 2026 7 / 15
Use of virtual function III
With virtual: Function binding will be done at runtime. Therefore we call
it Dynamic Binding based on object type.
Dr. Satish Kumar March 11, 2026 8 / 15
Pure Virtual Function
A virtual function does not perform any task on its own; it acts as a
placeholder
When a function has no definition, it is referred to as a “do-nothing”
function
Such a function is known as pure virtual function, which is declared in
the base class but has no definition for that class
A class containing a pure virtual function cannot be instantiated, and
such classes are called abstract base classes
The primary purpose of an abstract base class is to provide
characteristics for derived classes and to enable the creation of base
pointers, which facilitate runtime polymorphism
Dr. Satish Kumar March 11, 2026 9 / 15
Example of Pure Virtual Function I
1 #include <iostream >
2 using namespace std;
3
4 class Base{
5 public:
6 virtual void show () = 0;
7 };
8
9 class Derived: public Base{
10 public:
11 void show ()
12 {
13 cout << "Derived class is derived from the
base class" << endl;
14 }
15 };
16
Dr. Satish Kumar March 11, 2026 10 / 15
Example of Pure Virtual Function II
17 int main (){
18 Base *bptr;
19 Derived d;
20 bptr = &d;
21 bptr -> show ();
22 return 0;
23 }
Dr. Satish Kumar March 11, 2026 11 / 15
Problem: Virtual Functions
Problem Statement
Design a C++ program to calculate the area of different shapes using
virtual functions.
Create a base class Shape with a virtual function area().
Derive the following classes from Shape:
Circle
Rectangle
Triangle
Each derived class should override the area() function.
Use a base class pointer to call the area function.
Demonstrate runtime polymorphism.
Expected Output Example
Area of Circle: 78.5
Area of Rectangle: 50
Area of Triangle: 24
Dr. Satish Kumar March 11, 2026 12 / 15
Skeleton Code: Virtual Functions I
1 #include <iostream >
2 using namespace std;
3
4 class Shape {
5 public:
6 virtual void area () {
7 cout << "Area not defined" << endl;
8 }
9 };
10
11 class Circle : public Shape {
12 float r;
13 public:
14 Circle(float radius) {
15 r = radius;
16 }
17
Dr. Satish Kumar March 11, 2026 13 / 15
Skeleton Code: Virtual Functions II
18 void area () {
19 // Write code to compute area of circle
20 }
21 };
22
23 class Rectangle : public Shape {
24 float l, b;
25 public:
26 Rectangle(float length , float breadth) {
27 l = length;
28 b = breadth;
29 }
30
31 void area () {
32 // Write code to compute area of rectangle
33 }
34 };
Dr. Satish Kumar March 11, 2026 14 / 15
Skeleton Code: Virtual Functions III
35
36 int main () {
37 Shape *s;
38 Circle c(5);
39 Rectangle r(10 ,5);
40
41 s = &c;
42 s->area ();
43
44 s = &r;
45 s->area ();
46
47 return 0;
48 }
Dr. Satish Kumar March 11, 2026 15 / 15