Polymorphism
Polymorphism
Polymorphism is a major strength of an object centered paradigm
Same general type of action…..
Accomplished in different ways
By different types of objects
The underlying software system
Decides how to achieve the action
Copyright 2006 Oxford Consulting, Ltd 1 February2006
-1-
Polymorphism
Polymorphism
Key issue…..
When to implement the action
Compile time
Early Binding
Allows greater execution speed
Achieved through optimized code
Run time
Late Binding
Allows for greater flexibility
Opportunity for abstraction
Copyright 2006 Oxford Consulting, Ltd 1 February2006
-2-
Polymorphism
Polymorphism and C++
Early
Binding occurs at compile time
Early binding polymorphism
Process of overloading members
Late
Binding occurs at runtime
Late binding polymorphism
The code to implement the method is chosen at runtime
Appropriate code chosen sending a message to
the object …. Not to the pointer to the object
Implemented through virtual functions
Copyright 2006 Oxford Consulting, Ltd 1 February2006
-3-
Polymorphism
Copyright 2006 Oxford Consulting, Ltd 1 February2006
-4-
What is Run Time Polymorphism
Run time polymorphism (implemented in C++ with virtual functions) is
the third essential feature of an object oriented programming language,
after data abstraction and inheritance.
C++ virtual function is a member function of a class, whose functionality
can be over-ridden in its derived classes.
C++ virtual function is,
* A member function of a class
* Declared with virtual keyword
* Usually has a different functionality in the derived class
* A function call is resolved at run-time
C++ virtual functions are used to achieve run time polymorphism. To
declare a virtual function virtual keyword is put at the start of normal
function declaration.
Requirements for implementing virtual function-
* Base Class Pointer
* Inheritance
* Method Overriding.
It is a functions whose behavior can be overidden with an inherited class
by a function of same signature. It is an important part of OOPS and
Polymorphism.
What is a virtual function ?
A virtual function is a member function that is declared within the
base class and redefined by a derived class. When a class
containing a virtual function is inherited ,the derived class redefines
the virtual function to fit its needs.
A virtual function is a member function you may redefine for other
derived classes, and can ensure that the compiler will call the
redefined virtual function for an object of the corresponding derived
class, even if you call that function with a pointer or reference to a
base class of the object.
You declare a function with the keyword virtual if you want the
compiler to use dynamic binding for that specific function.
A class that declares or inherits a virtual function is called a
polymorphic class
A virtual function must be one of the following:
1) Defined
2) Declared pure
Polymorphism
Virtual Functions
A virtual function must be declared in a parent class
syntax
virtual function
virtual returnType functionName ( argsi ) { function body ;}
pure virtual function
virtual returnType functionName ( argsi ) = 0;
Copyright 2006 Oxford Consulting, Ltd 1 February2006
-7-
Polymorphism
Virtual Functions
Declaration
A function name is preceded by the keyword virtual
Function name can only be used once in the parent class
Cannot overload virtual functions
Only class member functions can be declared virtual
A function is virtual…..
If it is declared virtual
There is a base class function with the same signature
declared virtual
Any or all class member functions (except constructors)
can be declared virtual
Copyright 2006 Oxford Consulting, Ltd 1 February2006
-8-
Polymorphism
Virtual Functions
Implementation
The body of the virtual function must be supplied in the parent
class unless declared to be a pure virtual function
A derived class can override the definition by providing its own
implementation
If the re-declaration does not match exactly…...
The function not considered virtual for that class
A virtual function still permitted in a subsequently derived class
Copyright 2006 Oxford Consulting, Ltd 1 February2006
-9-
Polymorphism
Virtual Functions
Such a capability permits multiple functions to be called through a
common interface.
Can be overridden by explicit qualification with the scope operator.
Public Interface
Base
Derived1
Derived2
Derived3 Gives Uniform Function
…….. Call Interface
Derivedn
Copyright 2006 Oxford Consulting, Ltd 1 February2006
- 10 -
Polymorphism
Virtual Functions
When function in a class is declared virtual
Keyword virtual tells compiler
Don’t perform early binding
Install mechanisms to perform late binding
Compiler responds by creating
Table of function pointers
Installing a data member to the class to point to the table
Copyright 2006 Oxford Consulting, Ltd 1 February2006
- 11 -
Polymorphism
Virtual Functions - Invocation
A virtual function is invoked through a public base class pointer or
reference.
Runtime Binding
Typically polymorphic binding is done dynamically at
runtime
Virtual functions are not inlined
Compile Time Binding
Occasionally have compile time polymorphic binding
Invoked through an object of the class type
Invoked using the scope operator
Invoked through a constructor or destructor
Copyright 2006 Oxford Consulting, Ltd 1 February2006
- 12 -
Polymorphism
Virtual Functions - Access Protection
The access level of a virtual function is determined by
Access level in class of the pointer through which it’s
invoked
Not by the class in which it’s defined.
Copyright 2006 Oxford Consulting, Ltd 1 February2006
- 13 -
Polymorphism
Virtual Functions
Rules for Virtual Functions
• A Virtual function must be members of some class.
• They cannot be static members.
• Accessed by using object pointers.
• Can be friend of another class.
• A Virtual function in a base class must be defined, eventhough
it is not used.
Copyright 2006 Oxford Consulting, Ltd 1 February2006
- 14 -
Polymorphism
Virtual Functions
Rules for Virtual Functions
• The prototypes of the base class version of virtual and all
derived class version must be identical.
• We cannot have virtual constructor but destructor.
• A base pointer points to any type of derived object, the reverse
is not true ie we cannot use a pointer to derived class to
access an object of the base class.
• Virtual functions are defined in base class, the need not be
redefined in derived class.
Copyright 2006 Oxford Consulting, Ltd 1 February2006
- 15 -
Copyright 2006 Oxford Consulting, Ltd 1 February2006
- 16 -
Copyright 2006 Oxford Consulting, Ltd 1 February2006
- 17 -
Board Practical
Write a program in C++ using virtual function. The program must
declare p to be a pointer to objects of the base class Person.
First, the program must assign p to point an instance x (name of
the person eg. “BOB”) of class Person. The program must then
assign p to point at an instance y (name of the student,
eg.”TOM”) of the derived class Student. Define a print() function
in the base class such that it invokes the same base class
function to print the name of the person by default.
Copyright 2006 Oxford Consulting, Ltd 1 February2006
- 18 -
Board Practical
#include<iostream.h>
#include<conio.h>
class person
{
public:
virtual void display()
{
cout<<"\n The name of the person is Bob";
}
};
Copyright 2006 Oxford Consulting, Ltd 1 February2006
- 19 -
Board Practical
class student:public person
{
public:
void display()
{
cout<<"\n The name of the student is Tom";
}
};
Copyright 2006 Oxford Consulting, Ltd 1 February2006
- 20 -
Board Practical
void main()
{
clrscr();
person *p;
person x;
student y;
p=&x;
p->display();
p=&y;
p->display();
getch();
}
Copyright 2006 Oxford Consulting, Ltd 1 February2006
- 21 -