The document defines and provides examples of polymorphism in object-oriented programming. It discusses two types of polymorphism: static and dynamic. Static polymorphism is resolved at compile-time through function overloading and operator overloading, while dynamic polymorphism uses virtual functions and is resolved at run-time. Virtual functions, pure virtual functions, and abstract classes are also explained as key aspects of implementing dynamic polymorphism.
2. Definition…….
• Polymorphism is a Greek term that means ability to
exist in more than one form. For example, an English
word “bat” can have two meanings in two different
contexts., i.e. a cricket bat and an animal bat.
• Similarly, in Object oriented system, polymorphism can
be in functions as well as in existing operators( with
few exceptions). In broader sense, Polymorphism is of
two types:
• Static Polymorphism
• Dynamic Polymorphism
3. Static Polymorphism
• Also known as early binding or compile time
polymorphism it’s the ability to bind the
function codes with the function calls
(provided all the functions have same name)
at the compilation time. To implement static
polymorphism, we have the concept of
function overloading & operator overloading.
4. Dynamic Polymorphism-
• Also known as late binding or run time
polymorphism, it is the ability to bind the
function codes with the function calls
(provided all the functions have same name)
at the program run time. To implement
dynamic polymorphism, we have the concept
of virtual functions.
5.
6. Static Polymorphism
Function overloading: It’s a phenomenon for
a function with same name to exist with
different number and types of arguments. Eg.
a function called area() can be with two
arguments for finding area of a rectangle and
the same function area() can be with one
argument for finding area of a circle. The
syntax for both the functions will be:
7. Syntax:
void area( int l, int b)
{
cout<<”area of rectangle”<< l*b;
}
void area(int r)
{
cout <<” area of circle”<< 3.14*r*r;
}
8. Explanation….
• When the compiler will come across these
two function calls in main(), it will bind the
function call to area() with two arguments to
first function and function call to area() with
one argument to the second function. Hence,
this is called static binding of function call
with function code that is done at compile
time.
9. Operator overloading:
• In operator overloading, one operator is
capable of existing in more than one forms
without changing the actual meaning of the
operator. Eg. a “+” operator can be used to
add two numbers as well as can be used to
concatenate two strings or can add two
objects of any class. But in any case it will just
add. Any other operation is invalid.
10. Exceptions….
Except for some operators, all the operators can be
overloaded.
These exceptions are---
. (Member access or dot operator)
.*(Pointer to member operator)
: :(scope resolution operator)
sizeof( ) operator
?: (ternary operator)
11. Syntax for defining operator function
The operator can be overloaded through
operator function. Its syntax is:
return type class-name : : operator op (argument list)
{
Function body;
}
Here, the operator function is a member function
of any class so it has to be associated with a class
when defined outside the class. “operator” is the
keyword and “op” is the operator that has to be
overloaded.
12. An example…..
class xyz
{
int a, b;
public:
void getdata( )
{
cin>>a>>b;
}
void operator –( );
void putdata( )
{
cout <<a<<b;
}
};
void xyz : : operator –( )
{
a = -a;
b = -b;
}
int main( )
{
xyz ob; ob.getdata( );
-ob; // calling the operator
function
ob.putdata( );
return 0;
}
13. • In the previous slide, the example of
overloading unary minus operator was
shown.
• Above operator function can also be used
with friend function. It is shown as: friend
void operator (xyz &ob);
• Here “&” is the reference operator and it
refers to the object ob of the class xyz.
14. Pointers to function
• Function pointers are widely used in dynamic
binding.
• The concept of pointer to function acts as a
base for pointers to the members(.*).
• They are used to select a function dynamically
at run time.
• General syntax
• Datatype (*function_name)();
15. An example
• typedef void (*funptr)
(int,int);
• void add(int i, int j)
• { cout<< i<<"+"
<<j<<"="<<i+j;}
• void subtract( int i, int j)
• {
• cout<< i<<"-"
<<j<<"="<<i-j;
• }
• int main() {
• funptr ptr;
• ptr = &add;
• ptr(1,2);
• cout<<"n";
• ptr =&subtract;
• ptr(3,2);
•
• return 0;
• }
16. Pointer to object
• Void main()
• {
• Item x,*ptr;
• ptr= &x;
• x.getdata();//ptr-> getdata();
• x.show();// ptr->show();//(*ptr).show()
• }
• The parentheses are necessary because the dot
operators has higher precedence than the
indirection operator *.
17. This Pointer
A pointer to the calling object is referred to as
this pointer. It is passed as a default argument
to any member function that is called by an
object.
But, we can also refer to the calling object
explicitly in a member function by using this
pointer. Eg.
18. An example
• class sample
• {
• int i;
• public :
• void getdata( )
• {
• cin>> this -> i;
• }
• void putdata( )
• {
• cout << this ->i;
•
• }
• };
19. Use of pointer to object
• class item {
• int code;
• float price;
• public:
• void getdata()
• { cout<<"n enter code and price of the item:";
cin>>code>>price;}
• void show()
• { cout<<"n Code:"<<code; cout<<"nPrice:"<<
price;}};int main() { item X, *ptr; ptr= &X; ptr-
>getdata(); ptr->show(); return 0;}
20. Pointer to the object of base class.
• We always create the pointer to the object of
base class.
• Pointer to the object of base class are type
compatible with pointers to object of a
derived class. i.e a single pointer variable can
be made to point to object belonging to
different class.
21. Virtual function
• Virtual functions are those which are
preceded by a keyword virtual.
• They help in deciding which function has to be
called at run time depending upon the type of
function pointed by the base pointer.
22. Pure virtual functions
• Virtual functions with no definition and which are
equated to a zero are called pure virtual
functions. They are a basic feature of an Abstract
class.
• Virtual function defined in a base class is seldom
used for performing any task. They servers as a
place holders.
• Such functions are called do nothing functions or
pure virtual functions.
• Ex. Virtual show()=0;
23. Pure virtual functions
• Pure virtual functions are used
• if a function doesn't have any use in the base class
• but the function must be implemented by all its derived classes
• Let's take an example,
• Suppose, we have derived Triangle, Square and Circle classes
from the Shape class, and we want to calculate the area of all
these shapes.
• In this case, we can create a pure virtual function
named calculateArea() in the Shape. Since it's a pure virtual
function, all derived classes Triangle, Square and Circle must
include the calculateArea() function with implementation.
24. Continues…………….
• A pure virtual function doesn't have the
function body and it must end with = 0.
class Shape
{
public:
// creating a pure virtual function
virtual void calculateArea() = 0;
};