2. Introduction
• General meaning ; the ability to take on different forms.
• Programming language term:
– Allows an entity to take a variety of representations.
– Entities which exhibit Polymorphism are
• Type 1: Variables
• Type 2: Functions
• Type 3: Objects
3. Polymorphism Type 1
• Definition: The concept of dynamic binding allows a
variable to take different types of values
EG:
int input;
:
input = 100; // same variable used to store
integer and character values
:
:
input = ‘Hello’;
4. Polymorphism Type 2
• Definition: If a function is defined by the combination of
its name and its parameters then it is called
polymorphism.
• Examples:
Next slide….
5. Polymorphism Type 2 : Sample program 1
Different Number of Arguments
#include <iostream.h>
int add (int a, int b)
{ int c;
c = a + b;
return c;
}
int add (int d, int e, int f)
{ int g; Output:
g = d + e + f;
return g; Sum of two no. is 10
}
void main() Sum of three no. is 18
{ int i,j,k,l,m,n,p;
i = 4; j = 6;
k=add(i,j);
cout<<"Sum of two no. is ” << k << endl;
l=5; m=6; n=7;
p=add(l,m,n);
cout << "Sum of three no. is ” << p << endl;
}
6. Polymorphism Type 2 : Sample program 2
Same Number of arguments but different data types
#include <iostream.h>
int add (int a, int b)
{ int c;
c = a + b;
return c;
}
void add (float d, float e)
{ float g; Output:
g = d + e;
cout << "Sum of two float no. is ”<< g << endl; Sum of two int no. is 10
}
Sum of two float no. is 18
void main() {
int i, j, k;
float l, m;
i = 4; j = 6;
k=add(i, j);
cout << "Sum of two int no. is ”<< k <<endl;
l=5.2; m=6.4;
add(l, m);
}
7. Polymorphism Type 2 : Sample program 3
Member functions name are same in the class with different number of arguments
#include <iostream.h> void Patient::Displaydetails()
class Patient { cout <<“Patient No: “<<IdNumber <<endl;
{ private: cout<<“Patient Name:
int IdNumber; “<<Name<<endl<<endl; }
char Name; void main()
public: { Patient p1(267,’B');
Patient (); Patient p2;
Patient(int,char); p1.Displaydetails();
void Displaydetails(); p2.Displaydetails();
}; }
Output:
Patient :: Patient()
{ cout<<”Enter number and name: ";
cin >> IdNumber >> Name; Enter number and name: 8678 H
}
Patient No: 267
Patient::Patient(int id, char nam) Patient Name: B
{ IdNumber = id;
Name = nam; Patient No: 8678
} Patient Name: H
8. Polymorphism Type 3
• The method (function) to be invoked can depend on the
object.
• EG :
Class A Class B Class C
{ add( ) } { add( ) } { add( ) }
Main( )
A firstobject
B secondsobject
firstobject.add( );
Secondbject.add( );
9. Polymorphism Type 3
#include <iostream.h> class OutPatient : public Patient
class Patient { { private : int BP;
public: public :
int IdNumber; char Name; void Setdetails(int I, char N, int B)
void Setdetails (int I, char N) { IdNumber = I; Name = N; BP = B; }
{ IdNumber = I; Name = N; } void Displaydetails()
void Displaydetails() { cout<<endl<<"Outpatient:"<<IdNumber
{ cout<<endl<<"Patient:"<<IdNumber <<Name<<BP; } };
<<Name; } }; // end class Patient
class InPatient : public Patient void main()
{ private: int Wardnumber; { Patient p1;
int Daysinward; p1.Setdetails(111,’A');
public: p1.Displaydetails();
void Setdetails (int I, char N, int W, int D) InPatient p2;
{ IdNumber = I; Name = N; p2.Setdetails(333,’Z',12,14);
Wardnumber = W; p2.Displaydetails();
Daysinward = D;}; OutPatient p3;
void Displaydetails() p3.Setdetails(444,’Y',140);
{ cout<<endl<<"Inpatient:"<<IdNumber<< p3.Displaydetails();
Name<<Wardnumber<<Daysinward; } }
};