Polymorphism
Deepali Singla
Assistant Professor
Chandigarh university
Polymorphism
Polymorphism is the ability to use an operator or function in
different ways.
Polymorphism gives different meanings or functions to the operators
or function.
Poly, referring too many, signifies the many uses of those operators
and functions
A single function usage or an operator functioning in many ways can
be called polymorphism.
Polymorphism refers to codes, operations or objects that behave
differently in different contexts.
2
Types of Polymorphism
Two types of polymorphism:
Compile time
Function and operator overloading
Run time
Virtual Function
Compile time
This is also the early binding
In that compiler decide the scope of member function at the
compile time.
Function Overloading
Overloading a function Simply means, that a function is not
only defined its name but by its name and parameter types.
Function should be declare or define in same class
The following function are different :
int area(int I, int k);
void area(float I, float k);
float area();
Function Overloading
Overloading a function Simply means, that a function is not
only defined its name but by its name and parameter types.
Function should be declare or define in same class
The following function are different :
int area(int I, int k);
These three
void area(float I, float k);
methods are
float area();
different Bases on
there arguments
Function Overloading
class A
{
public:
void sum(int a, int b)
{
cout<<"Integers sum is :"<<(a+b)<<endl;
}
void sum(float a, float b)
{
cout<<"float sum = "<<(a+b)<<endl;
}
};
void main()
{
A a1;
float i=1.01,j=9.23;
[Link](2,5);
[Link](i,j);
}
Function overloading
Function Overloading
class A
{
public:
void sum(int a, int b)
{
cout<<"Integers sum is :"<<(a+b)<<endl;
}
void sum(float a, float b)
{
cout<<"float sum = "<<(a+b)<<endl;
}
};
void main()
{
A a1;
float i=1.01,j=9.23;
[Link](2,5);
[Link](i,j);
}
Output:
Integers sum is : 7
float sum = 10.24
Operator Overloading
When operator is overloaded with multiple jobs, it is known
as a operator overloading
Means one operator have different meaning.
It is a way to implement compile time polymorphism.
Operator Overloading
Rules :
Any symbol can be used as function name
If it is valid operator in c language
If it is preceded by operator keywords
You cannot overloaded sizeof and ?: (Conditional)
operator
10
Operator Overloading
class complex
{
private:
int a,b;
public:
void setdata(int x, int y)
{
a=x; b=y;
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl;
}
};
11
void main()
{
complex c1,c2,c3;
[Link](4,5);
[Link](6,8);
c3=c1 + c2;
[Link]();
}
Operator Overloading
class complex
{
private:
int a,b;
public:
void setdata(int x, int y)
{
a=x; b=y;
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl;
}
};
12
void main()
{
complex c1,c2,c3;
[Link](4,5);
[Link](6,8);
c3=c1 + c2;
[Link]();
}
Error
Operator Overloading
class complex
{
private:
int a,b;
public:
void setdata(int x, int y)
{
a=x; b=y;
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl;
}
};
13
void main()
{
complex c1,c2,c3;
[Link](4,5);
[Link](6,8);
c3= [Link](c2);
[Link]();
}
Operator Overloading
14
class complex
{
private:
int a,b;
public:
void setdata(int x, int y)
{
a=x; b=y;
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl;
}
complex add(complex o1)
{
complex temp;
temp.a= a + o1.a;
temp.b= b + o1.b;
return (temp);
} };
void main()
{
complex c1,c2,c3;
[Link](4,5);
[Link](6,8);
c3= [Link](c2);
[Link]();
}
Operator Overloading
15
class complex
void main()
{
{
private:
complex c1,c2,c3;
int a,b;
[Link](4,5);
public:
[Link](6,8);
void setdata(int x, int y)
c3= [Link](c2);
{
[Link]();
a=x; b=y;
}
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl; a
a
}
b
b
complex add(complex o1)
functions
functions
{
c1
c2
complex temp;
a
temp.a= a + o1.a;
b
temp.b= b + o1.b;
functions
return (temp);
} };
temp
a
b
functions
o1
a
b
functions
c3
Operator Overloading
16
class complex
void main()
{
{
private:
complex c1,c2,c3;
int a,b;
[Link](4,5);
public:
[Link](6,8);
void setdata(int x, int y)
c3= [Link](c2);
{
[Link]();
a=x; b=y;
}
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl; a= 4
a
}
b= 5
b
complex add(complex o1)
functions
functions
{
c1
c2
complex temp;
a
temp.a= a + o1.a;
b
temp.b= b + o1.b;
functions
return (temp);
} };
temp
a
b
functions
o1
a
b
functions
c3
Operator Overloading
17
class complex
void main()
{
{
private:
complex c1,c2,c3;
int a,b;
[Link](4,5);
public:
[Link](6,8);
void setdata(int x, int y)
c3= [Link](c2);
{
[Link]();
a=x; b=y;
}
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl; a= 4
a= 6
}
b= 5
b= 8
complex add(complex o1)
functions
functions
{
c1
c2
complex temp;
a
temp.a= a + o1.a;
b
temp.b= b + o1.b;
functions
return (temp);
} };
temp
a
b
functions
o1
a
b
functions
c3
Operator Overloading
18
class complex
void main()
{
{
private:
complex c1,c2,c3;
int a,b;
[Link](4,5);
public:
[Link](6,8);
void setdata(int x, int y)
c3= [Link](c2);
{
[Link]();
a=x; b=y;
}
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl; a= 4
a= 6
}
b= 5
b= 8
complex add(complex o1)
functions
functions
{
c1
c2
complex temp;
a
temp.a= a + o1.a;
b
temp.b= b + o1.b;
functions
return (temp);
} };
temp
a
b
functions
o1
a
b
functions
c3
Operator Overloading
19
class complex
void main()
{
{
private:
complex c1,c2,c3;
int a,b;
[Link](4,5);
public:
[Link](6,8);
void setdata(int x, int y)
c3= [Link](c2);
{
[Link]();
a=x; b=y;
}
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl; a= 4
a= 6
}
b= 5
b= 8
complex add(complex o1)
functions
functions
{
c1
c2
complex temp;
a
temp.a= a + o1.a;
b
temp.b= b + o1.b;
functions
return (temp);
} };
temp
a
b
functions
o1
a
b
functions
c3
Operator Overloading
20
class complex
void main()
{
{
private:
complex c1,c2,c3;
int a,b;
[Link](4,5);
public:
[Link](6,8);
void setdata(int x, int y)
c3= [Link](c2);
{
[Link]();
a=x; b=y;
}
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl; a= 4
a= 6
}
b= 5
b= 8
complex add(complex o1)
functions
functions
{
c1
c2
complex temp;
a
temp.a= a + o1.a;
b
temp.b= b + o1.b;
functions
return (temp);
} };
temp
a= 6
b= 8
functions
o1
a
b
functions
c3
Operator Overloading
21
class complex
void main()
{
{
private:
complex c1,c2,c3;
int a,b;
[Link](4,5);
public:
[Link](6,8);
void setdata(int x, int y)
c3= [Link](c2);
{
[Link]();
a=x; b=y;
}
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl; a= 4
a= 6
}
b= 5
b= 8
complex add(complex o1)
functions
functions
{
c1
c2
complex temp;
a= 10
temp.a= a + o1.a;
b
temp.b= b + o1.b;
functions
return (temp);
} };
temp
a= 6
b= 8
functions
o1
a
b
functions
c3
Operator Overloading
22
class complex
void main()
{
{
private:
complex c1,c2,c3;
int a,b;
[Link](4,5);
public:
[Link](6,8);
void setdata(int x, int y)
c3= [Link](c2);
{
[Link]();
a=x; b=y;
}
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl; a= 4
a= 6
}
b= 5
b= 8
complex add(complex o1)
functions
functions
{
c1
c2
complex temp;
a= 10
temp.a= a + o1.a;
b= 13
temp.b= b + o1.b;
functions
return (temp);
} };
temp
a= 6
b= 8
functions
o1
a
b
functions
c3
Operator Overloading
23
class complex
void main()
{
{
private:
complex c1,c2,c3;
int a,b;
[Link](4,5);
public:
[Link](6,8);
void setdata(int x, int y)
c3= [Link](c2);
{
[Link]();
a=x; b=y;
}
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl; a= 4
a= 6
}
b= 5
b= 8
complex add(complex o1)
functions
functions
{
c1
c2
complex temp;
a= 10
temp.a= a + o1.a;
b= 13
temp.b= b + o1.b;
functions
return (temp);
} };
temp
a
b
functions
o1
a
b
functions
c3
Operator Overloading
24
class complex
void main()
{
{
private:
complex c1,c2,c3;
int a,b;
[Link](4,5);
public:
[Link](6,8);
void setdata(int x, int y)
c3= [Link](c2);
{
[Link]();
a=x; b=y;
}
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl; a= 4
a= 6
}
b= 5
b= 8
complex add(complex o1)
functions
functions
{
c1
c2
complex temp;
a= 10
temp.a= a + o1.a;
b= 13
temp.b= b + o1.b;
functions
return (temp);
} };
temp
a
b
functions
o1
a = 10
b= 13
functions
c3
Operator Overloading
25
class complex
{
private:
int a,b;
public:
void setdata(int x, int y)
{
a=x; b=y;
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl;
}
complex add(complex o1)
{
complex temp;
temp.a= a + o1.a;
temp.b= b + o1.b;
return (temp);
} };
void main()
{
complex c1,c2,c3;
[Link](4,5);
[Link](6,8);
c3= [Link](c2);
[Link]();
}
Output:
a= 10, b= 13
Operator Overloading
26
class complex
{
private:
int a,b;
public:
void setdata(int x, int y)
{
a=x; b=y;
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl;
}
complex add(complex o1)
{
complex temp;
temp.a= a + o1.a;
temp.b= b + o1.b;
return (temp);
} };
void main()
{
complex c1,c2,c3;
[Link](4,5);
[Link](6,8);
c3= [Link](c2);
[Link]();
}
In place of add can we write
Sum ?
Yes
But can we write +
Operator Overloading
27
class complex
{
private:
int a,b;
public:
void setdata(int x, int y)
{
a=x; b=y;
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl;
}
complex add(complex o1)
{
complex temp;
temp.a= a + o1.a;
temp.b= b + o1.b;
return (temp);
} };
void main()
{
complex c1,c2,c3;
[Link](4,5);
[Link](6,8);
c3= [Link](c2);
[Link]();
}
In place of add can we write
Sum ?
Yes
But can we write +
Yes we can write + in place of
Function name using the
operator keyword
Operator Overloading
28
class complex
{
private:
int a,b;
public:
void setdata(int x, int y)
{
a=x; b=y;
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl;
}
complex operator +(complex o1)
{
complex temp;
temp.a= a + o1.a;
temp.b= b + o1.b;
return (temp);
} };
void main()
{
complex c1,c2,c3;
[Link](4,5);
[Link](6,8);
c3= c1 + c2
[Link]();
}
Here we perform operator
overloading.
In that program + operator
used to add two complex no
But in normal case this is
used to add two primitive no.
Operator Overloading
29
class complex
void main()
{
{
private:
complex c1,c2,c3;
int a,b;
[Link](4,5);
public:
[Link](6,8);
void setdata(int x, int y)
c3= c1 + c2
{
[Link]();
a=x; b=y;
}
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl;
}
complex operator +(complex o1)
Output:
{
a= 10, b= 13
complex temp;
temp.a= a + o1.a;
temp.b= b + o1.b;
return (temp);
} };
Unary operator overloading
Unary operator is a operator where we have only one
operant.
For example:
b=-a
Here is a unary operator which is used to perform
negation operation
30
Unary operator overloading
31
class complex
void main()
{
{
private:
complex c1,c2;
int a,b;
[Link](4,5);
public:
c2= -c1;
void setdata(int x, int y)
[Link]();
{
}
a=x; b=y;
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl;
}
complex operator -()
{
complex temp;
temp.a= -a;
temp.b= -b;
return (temp);
} };
Unary operator overloading
32
class complex
void main()
{
{
private:
complex c1,c2;
int a,b;
[Link](4,5);
public:
c2= -c1;
void setdata(int x, int y)
[Link]();
{
}
a=x; b=y;
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl;
}
complex operator -()
Output:
{
a= -4, b= -5
complex temp;
temp.a= -a;
temp.b= -b;
return (temp);
} };
Run time
Run time polymorphism also called the dynamic binding or
late binding
Dynamic means object are created at run time
Dynamic binding offer a greater flexibility and higher level
of abstraction than static binding because it is done on the
fly when a program executes
33
Run time
Virtual function is the best example of run time
polymorphism
Virtual function :
if we define any function as a virtual that means that function cannot
bind at compile time.
Compiler perform late binding of that function
So using the virtual function we try to remove the ambiguity
problem in a program during the inheritance
34
Run time
class A
{
public:
void f1()
{
cout<< "hello"<<endl;
}
};
class B : public A
{
public:
void f1()
{
cout<<"hello there"<<endl;
}
};
35
void main()
{
A *p;
B ob;
p=&ob;
p->f1();
}
Run time
class A
{
public:
void f1()
{
cout<< "hello"<<endl;
}
};
class B : public A
{
public:
void f1()
{
cout<<"hello there"<<endl;
}
};
36
void main()
{
A *p;
B ob;
p=&ob;
p->f1();
}
Output:
hello
Run time
class A
{
public:
void f1()
{
cout<< "hello"<<endl;
}
};
class B : public A
{
public:
void f1()
{
cout<<"hello there"<<endl;
}
};
37
void main()
{
A *p;
B ob;
p=&ob;
p->f1();
}
Output:
hello
This is wrong because we want to execute drive
classs function f1() but here execute base classs function.
So thats why we perform late binding . But how can be do
that?
Using the virtual keywords.
Syntax:
virtual <return type> <name of function> <no. of
arg.>
Run time
class A
{
public:
virtual void f1()
{
cout<< "hello"<<endl;
}
};
class B : public A
{
public:
void f1()
{
cout<<"hello there"<<endl;
}
};
38
void main()
{
A *p;
B ob;
p=&ob;
p->f1();
}
Output:
hello
Function define as a virtual
Run time
class A
{
public:
virtual void f1()
{
cout<< "hello"<<endl;
}
};
class B : public A
{
public:
void f1()
{
cout<<"hello there"<<endl;
}
};
39
void main()
{
A *p;
B ob;
p=&ob;
p->f1();
}
Output:
Hello there
Function define as a virtual
Run time
Static Binding
Static binding means that the
legality of a member function
invocation is checked at the
earliest possible moment : by
the compiler at compile time.
The compiler uses the static
type of the pointer to
determine weather the
member function invocation
is legal
40
Dynamic Binding
Dynamic binding means that
the address of the code in a
member function invocation
is determined at the last
possible moment: based on
the dynamic type of the
object at run time. It is called
dynamic binding because
the binding to the code that
actually gets called as
accomplished dynamically (at
run time).
Run time
Static Binding
41
Dynamic Binding
With static binding, you get
Dynamic binding offers
better run time efficiency
because the compiler can
actually optimize the code
before running it.
The CLR knows how much
memory to take up for the
static method object
greater flexibility and a higher
level of abstraction than static
binding because it is done on
the fly when a program
executes.
The CLR doesn't know how
much memory to take up for
the dynamic method object
ThanQ
42