Object Oriented Programming
C++
PREPARED BY: ENGR. REEMA QAISER KHAN
SCHEME OF PRESENTATION
Polymorphism/Virtual Functions
Normal Member Functions Accessed with Pointers
Virtual Member Functions Accessed with Pointers
References
ENGR. REEMA QAISER KHAN
Polymorphism/Virtual Functions
The word polymorphism means having many forms. Typically, polymorphism occurs when there
is a hierarchy of classes and they are related by inheritance.
C++ polymorphism means that a call to a member function will cause a different function to be
executed depending on the type of object that invokes the function.
Virtual functions provide a way for a program to decide while it is running what function to call.
In object-oriented programming, a virtual function or virtual method is a function or method whose
behavior can be overridden within an inheriting class by a function with the same signature.
This concept is an important part of the polymorphism portion of object-oriented programming (OOP).
ENGR. REEMA QAISER KHAN
Normal Member Functions Accessed
with Pointers
#include<iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////////////////////////////////////////
class Base
//base class
{
public:
void show()
//normal function
{ cout<<Base\n;}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////
class Derv1:public Base
//derived class 1
{
public:
void show()
{cout<<Derv1\n;}
};
Cont.
ENGR. REEMA QAISER KHAN
Normal Member Functions Accessed
with Pointers
////////////////////////////////////////////////////////////////////////////////////////////////////////
class Derv2:public Base
//derived class 2
{
public:
void show()
{cout<<Derv2\n;}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
Derv1 dv1;
//object of derived class 1
Derv2 dv2;
//object of derived class 2
Base* ptr;
//pointer to base class
ptr=&dv1;
ptr->show();
//put address of dv1 in pointer
//execute show()
ENGR. REEMA QAISER KHAN
Cont.
5
Normal Member Functions Accessed
with Pointers
ptr=&dv2;
ptr->show();
return 0;
}
//put address of dv2 in pointer
//execute show()
The Derv1 and Derv2 classes are derived from class Base. Each of these classes has a member function
show().
Now the question is, when you execute the line
ptr->show();
What function is called? Is it show() of Base, or show() of Derv1, or show() of Derv2?
Which of the show() function is called here? The output from the program answers these questions:
Base
Base
ENGR. REEMA QAISER KHAN
Normal Member Functions Accessed
with Pointers
As you see, the function in the base class is always executed. The compiler ignores the contents of the
pointer ptr and chooses the member function that matches the type of the pointer.
ptr
Nonvirtual Pointer Access
Base
show()
&Derv1
Derv1
ptr->show()
show()
Derv2
ptr
show()
&Derv2
ptr->show()
ENGR. REEMA QAISER KHAN
Virtual Member Functions Accessed with
Pointers
#include<iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////////////////////////////////////////
class Base
//base class
{
public:
virtual void show()
//virtual function
{ cout<<Base\n;}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////
class Derv1:public Base
//derived class 1
{
public:
void show()
{cout<<Derv1\n;}
};
Cont.
ENGR. REEMA QAISER KHAN
Virtual Member Functions Accessed with
Pointers
////////////////////////////////////////////////////////////////////////////////////////////////////////
class Derv2:public Base
//derived class 2
{
public:
void show()
{cout<<Derv2\n;}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
Derv1 dv1;
//object of derived class 1
Derv2 dv2;
//object of derived class 2
Base* ptr;
//pointer to base class
ptr=&dv1;
ptr->show();
//put address of dv1 in pointer
//execute show()
Cont.
ENGR. REEMA QAISER KHAN
Virtual Member Functions Accessed with
Pointers
ptr=&dv2;
ptr->show();
return 0;
}
//put address of dv2 in pointer
//execute show()
The output of this program is:
Derv1
Derv2
Now, as you see, the member functions of the derived classes, not the base class, are executed. We
change the contents of ptr from the address of Derv1 to that of Derv2, and the particular instance of
show() that is executed also changes. So the same function call
ptr->show();
Executes different functions, depending on the contents of ptr. The rule is that the compiler selects the
function based on the contents of the pointer ptr, not on the type of the pointer, as in the previous
example where we did not use virtual keyword.
ENGR. REEMA QAISER KHAN
10
Virtual Member Functions Accessed with
Pointers
The rule is that the compiler selects the function based on the contents of the pointer ptr, not on the type
of the pointer, as in the previous example where we did not use virtual keyword.
ptr
Virtual Pointer Access
Base
show()
&Derv1
Derv1
ptr->show()
show()
Derv2
ptr
show()
&Derv2
ptr->show()
ENGR. REEMA QAISER KHAN
11
REFERENCES
1- Book: Object-Oriented Programming in C++, Robert Lafore, 4th Edition,
pages(504-509).
ENGR. REEMA QAISER KHAN
12