Upcoming SlideShare
×

# Lecture20

280 views

Published on

Published in: Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
280
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
18
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Lecture20

1. 1. Lecture 20Pol ymor phism
2. 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. 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. 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. 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. 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 18void 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. 7. Polymorphism Type 2 : Sample program 3Member 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. 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. 9. Polymorphism Type 3#include <iostream.h> class OutPatient : public Patientclass 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 Patientclass 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; } }};