2. Contents…
• What is Polymorphism?
• How many ways of implementation?
• Linking or Binding.
• Compile time Polymorphism.
• Runtime Polymorphism.
3. What is Polymorphism?
• In real world there are so many cases where
we see a single name is shared for
multiple jobs. This phenomena is termed as
POLYMORPHISM in OOPs.
4. How many ways of
implementation?
• OOP languages like C++, Java etc.
provide facility to implement
polymorphism.
• Let us start with C++_
5. Linking or Binding.
• To call a
function is
termed as
Linking or
Binding.
#include”iostream.h”
class demo
{
public:
void print()
{cout<<“ntHello ! PRINCE ”;}
};
void main()
{demo obj;
obj.print(); // linking fn print
}
6. Compile time
Polymorphism.
• Also known as Static Linking or Early binding.
• The compiler calls a function at the time of
compile.
• In C++ it is implemented as below_
• Overloading.
• Function
• Operator
• Templates.
7. Function Overloading
• More than one
functions have
same name with
different
signatures in the
same scope shows
overloading of
functions.
#include<iostream.h>
class demo
{
public:
void sum()
{
int x,y;
cout<<“Enter 2 no.: ”;cin>>x>>y;
cout<<“Sum: ”<<x+y;
}
void sum( int x, int y)
{
cout<<“Sum: ”<<x+y;
}
};
8. Function Overloading
• When two functions have same prototypes the
second is termed as redeclaration of the first.
• Two functions have same name and signature
with different return types, the second is
errorneous declaration.
• The signatures may be different in no. of
arguments or return types of arguments.
• Overloading of constructor is an example of
function overloading.
9. Function Overloading
• Constructor overloading:
#include<iostream.h>
class demo
{
int x;
float y;
public:
demo()
{ x=10; y=23.5; cout<<“constructor 1”;}
demo( int a, float b)
{ x=a; y=b; cout<<“constructor 2”; }
};
10. Operator Overloading
• The concept of defining functions of all the
existing operators on user defined types
(classes) is known as overloading of operators.
• Implemented_
• Through member functions.
• Through friend functions
11. Operator Overloading
• New operators are not defined .
• Precedence and associativity are never
changed.
• Followings are never overloaded_
• :: scope resolution operator
• sizeof() operator
• ?: conditional operator
• .* pointers
Operator Member fn Friend fn
Unary No argument 1 argument
Binary 1argument 2 arguments
13. Operator Overloading
• Implementation through Friend function:
• First declare the prototype inside the class.
SYNTAX:
friend return type operator <op> (type of arguments);
• Definition outside the class
SYNTAX:
return type operator <op>(arguments)
{
//definition
}
15. Operator Overloading
• Example: by friend function
#include<iostream.h>
class complex
{
int x,y;
public:
void get()
{
cout<<“Enter x & y:”;
cin>>x>>y;
}
void show
{
cout<<x<<“+”<<y<<“i”;
}
friend complex operator +(complex, complex);
};
complex operator + (complex c1, complex c2)
{
complex s;
s.x=c1.x+c2.x;
s.y=c1.y+c2.y;
return s;
};
Void main()
{
complex ca, cb, cd;
ca.get();cb.get();
cd=ca+cb;
cout<<“S=”<<x<<“+”<<y<<“is”;}
16. Run time
Polymorphism.
• Also known as Dynamic Linking or Late
binding.
• The compiler calls a function at the run time.
• In C++ it is implemented as below_
• Virtual function
17. Virtual function
• In Inheritance the base class pointer is compatible with all the
objects of the derived class, but its reverse is not true.
• At the time of pointing a derived class object, the base class
pointer points only those member functions which are
declared as same as in the base class(prototype).
• It is determined at run time through VIRTUAL FUNCTION.
18. Virtual function
• A Virtual function is_
• Declared within the base class.
• May be redefined in the derived class.
• Neither static nor friend.
• Syntax:
virtual return type identifier ( args…);
19. Virtual function
• A Pure virtual function is_
• Only declared within the base class.
• The derived class must add its own definition.
• If a derived class failed to override the pure virtual function
the compile will generate error.
• Syntax:
virtual return type identifier(args…) = 0;
20. Virtual function
• A class containing at least one PVF is Abstract class.
• There is no object is declared of an Abstract class.
class demo
{
public:
virtual void print() = 0; //PVF
};
class derived : public demo
{
public:
void print()
{cout<<“HELLO! PRINCE ”;}
};
void main()
{
demo *p;
derived obj;
p = &obj;
p->print();
}
/* output
HELLO! PRINCE
*/