Polly Morphis M
Polly Morphis M
Polly Morphis M
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
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
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;
a1.sum(2,5);
a1.sum(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;
a1.sum(2,5);
a1.sum(i,j);
}
Output:
Integers sum is : 7
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 :
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;
c1.setdata(4,5);
c2.setdata(6,8);
c3=c1 + c2;
c3.showdata();
}
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;
c1.setdata(4,5);
c2.setdata(6,8);
c3=c1 + c2;
c3.showdata();
}
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;
c1.setdata(4,5);
c2.setdata(6,8);
c3= c1.add(c2);
c3.showdata();
}
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;
c1.setdata(4,5);
c2.setdata(6,8);
c3= c1.add(c2);
c3.showdata();
}
Operator Overloading
15
class complex
void main()
{
{
private:
complex c1,c2,c3;
int a,b;
c1.setdata(4,5);
public:
c2.setdata(6,8);
void setdata(int x, int y)
c3= c1.add(c2);
{
c3.showdata();
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;
c1.setdata(4,5);
public:
c2.setdata(6,8);
void setdata(int x, int y)
c3= c1.add(c2);
{
c3.showdata();
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;
c1.setdata(4,5);
public:
c2.setdata(6,8);
void setdata(int x, int y)
c3= c1.add(c2);
{
c3.showdata();
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;
c1.setdata(4,5);
public:
c2.setdata(6,8);
void setdata(int x, int y)
c3= c1.add(c2);
{
c3.showdata();
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;
c1.setdata(4,5);
public:
c2.setdata(6,8);
void setdata(int x, int y)
c3= c1.add(c2);
{
c3.showdata();
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;
c1.setdata(4,5);
public:
c2.setdata(6,8);
void setdata(int x, int y)
c3= c1.add(c2);
{
c3.showdata();
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;
c1.setdata(4,5);
public:
c2.setdata(6,8);
void setdata(int x, int y)
c3= c1.add(c2);
{
c3.showdata();
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;
c1.setdata(4,5);
public:
c2.setdata(6,8);
void setdata(int x, int y)
c3= c1.add(c2);
{
c3.showdata();
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;
c1.setdata(4,5);
public:
c2.setdata(6,8);
void setdata(int x, int y)
c3= c1.add(c2);
{
c3.showdata();
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;
c1.setdata(4,5);
public:
c2.setdata(6,8);
void setdata(int x, int y)
c3= c1.add(c2);
{
c3.showdata();
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;
c1.setdata(4,5);
c2.setdata(6,8);
c3= c1.add(c2);
c3.showdata();
}
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;
c1.setdata(4,5);
c2.setdata(6,8);
c3= c1.add(c2);
c3.showdata();
}
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;
c1.setdata(4,5);
c2.setdata(6,8);
c3= c1.add(c2);
c3.showdata();
}
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;
c1.setdata(4,5);
c2.setdata(6,8);
c3= c1 + c2
c3.showdata();
}
Operator Overloading
29
class complex
void main()
{
{
private:
complex c1,c2,c3;
int a,b;
c1.setdata(4,5);
public:
c2.setdata(6,8);
void setdata(int x, int y)
c3= c1 + c2
{
c3.showdata();
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);
} };
operant.
For example:
b=-a
Here is a unary operator which is used to perform
negation operation
30
31
class complex
void main()
{
{
private:
complex c1,c2;
int a,b;
c1.setdata(4,5);
public:
c2= -c1;
void setdata(int x, int y)
c2.showdata();
{
}
a=x; b=y;
}
void showdata()
{
cout<<"a= "<<a<<", b= "<<b<<endl;
}
complex operator -()
{
complex temp;
temp.a= -a;
temp.b= -b;
return (temp);
} };
32
class complex
void main()
{
{
private:
complex c1,c2;
int a,b;
c1.setdata(4,5);
public:
c2= -c1;
void setdata(int x, int y)
c2.showdata();
{
}
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
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
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
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
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
Run time
Static Binding
Static binding means that the
Dynamic Binding
Dynamic binding means that
Run time
Static Binding
41
Dynamic Binding
ThanQ
42