Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 20 Polymorphism. Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a.

Similar presentations


Presentation on theme: "Lecture 20 Polymorphism. Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a."— Presentation transcript:

1 Lecture 20 Polymorphism

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 #include int add (int a, int b) { int c; c = a + b; return c; } int add (int d, int e, int f) {int g; g = d + e + f; return g; } void main() {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; } Output: Sum of two no. is 10 Sum of three no. is 18 Different Number of Arguments

6 Polymorphism Type 2 : Sample program 2 #include int add (int a, int b) { int c; c = a + b; return c; } void add (float d, float e) { float g; g = d + e; cout << "Sum of two float no. is ”<< g << endl; } 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); } Output: Sum of two int no. is 10 Sum of two float no. is 18 Same Number of arguments but different data types

7 Polymorphism Type 2 : Sample program 3 #include class Patient { private: int IdNumber; char Name; public: Patient (); Patient(int,char); void Displaydetails(); }; Patient :: Patient() {cout<<”Enter number and name: "; cin >> IdNumber >> Name; } Patient::Patient(int id, char nam) { IdNumber = id; Name = nam; } void Patient::Displaydetails() { cout <<“Patient No: “<<IdNumber <<endl; cout<<“Patient Name: “<<Name<<endl<<endl; } void main() { Patient p1(267,’B'); Patient p2; p1.Displaydetails(); p2.Displaydetails(); } Output: Enter number and name: 8678 H Patient No: 267 Patient Name: B Patient No: 8678 Patient Name: H Member functions name are same in the class with different number of arguments

8 Polymorphism Type 3  The method (function) to be invoked can depend on the object.  EG : Class A { add( ) } Class B { add( ) } Class C { add( ) } Main( ) A firstobject B secondsobject firstobject.add( ); Secondbject.add( );

9 Polymorphism Type 3 #include class Patient { public: int IdNumber; char Name; void Setdetails (int I, char N) { IdNumber = I; Name = N;} void Displaydetails() { cout<<endl<<"Patient:"<<IdNumber <<Name; } }; // end class Patient class InPatient : public Patient { private: int Wardnumber; int Daysinward; public: void Setdetails (int I, char N, int W, int D) { IdNumber = I;Name = N; Wardnumber = W; Daysinward = D;}; void Displaydetails() { cout<<endl<<"Inpatient:"<<IdNumber<< Name<<Wardnumber<<Daysinward; } }; class OutPatient : public Patient { private : int BP; public : void Setdetails(int I, char N, int B) {IdNumber = I; Name = N; BP = B; } void Displaydetails() {cout<<endl<<"Outpatient:"<<IdNumber <<Name<<BP; } }; void main() { Patient p1; p1.Setdetails(111,’A'); p1.Displaydetails(); InPatient p2; p2.Setdetails(333,’Z',12,14); p2.Displaydetails(); OutPatient p3; p3.Setdetails(444,’Y',140); p3.Displaydetails(); }


Download ppt "Lecture 20 Polymorphism. Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a."

Similar presentations


Ads by Google