SlideShare uma empresa Scribd logo
1 de 9
Lecture 20



Pol ymor phism
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
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’;
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….
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;
}
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);
}
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
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( );
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; }              }
};

Mais conteúdo relacionado

Mais procurados

From clever code to better code
From clever code to better codeFrom clever code to better code
From clever code to better codeDror Helper
 
Virtual function
Virtual functionVirtual function
Virtual functionharman kaur
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Deepak Singh
 
Lab 9 sem ii_12_13
Lab 9 sem ii_12_13Lab 9 sem ii_12_13
Lab 9 sem ii_12_13alish sha
 
Lab 10 sem ii_12_13
Lab 10 sem ii_12_13Lab 10 sem ii_12_13
Lab 10 sem ii_12_13alish sha
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cppgourav kottawar
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st StudyChris Ohk
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Liju Thomas
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementSreedhar Chowdam
 

Mais procurados (20)

Programs of C++
Programs of C++Programs of C++
Programs of C++
 
C++ Chapter IV
C++ Chapter IVC++ Chapter IV
C++ Chapter IV
 
From clever code to better code
From clever code to better codeFrom clever code to better code
From clever code to better code
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
C++ Chapter III
C++ Chapter IIIC++ Chapter III
C++ Chapter III
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Lecture03
Lecture03Lecture03
Lecture03
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009
 
Lab 9 sem ii_12_13
Lab 9 sem ii_12_13Lab 9 sem ii_12_13
Lab 9 sem ii_12_13
 
Lab 10 sem ii_12_13
Lab 10 sem ii_12_13Lab 10 sem ii_12_13
Lab 10 sem ii_12_13
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
 
Pre zen ta sion
Pre zen ta sionPre zen ta sion
Pre zen ta sion
 
Lab 6
Lab 6Lab 6
Lab 6
 
Computer Programming- Lecture 10
Computer Programming- Lecture 10Computer Programming- Lecture 10
Computer Programming- Lecture 10
 
Computer Programming- Lecture 7
Computer Programming- Lecture 7Computer Programming- Lecture 7
Computer Programming- Lecture 7
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
 
Codejunk Ignitesd
Codejunk IgnitesdCodejunk Ignitesd
Codejunk Ignitesd
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
 

Destaque (9)

Lecture10
Lecture10Lecture10
Lecture10
 
Facebook經營觀察 0820
Facebook經營觀察 0820Facebook經營觀察 0820
Facebook經營觀察 0820
 
Lecture16
Lecture16Lecture16
Lecture16
 
Springwood at music resonate
Springwood at music resonateSpringwood at music resonate
Springwood at music resonate
 
Lecture07
Lecture07Lecture07
Lecture07
 
Lecture21
Lecture21Lecture21
Lecture21
 
Lecture09
Lecture09Lecture09
Lecture09
 
Lecture17
Lecture17Lecture17
Lecture17
 
Lecture05
Lecture05Lecture05
Lecture05
 

Semelhante a Lecture20

Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1Little Tukta Lita
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperDeepak Singh
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++Jay Patel
 
computer science sample papers 2
computer science sample papers 2computer science sample papers 2
computer science sample papers 2Swarup Kumar Boro
 
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdfDoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdfaathiauto
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and ObjectsPayel Guria
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - ReferenceMohammed Sikander
 
Presentation on template and exception
Presentation  on template and exceptionPresentation  on template and exception
Presentation on template and exceptionSajid Alee Mosavi
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxssuser3cbb4c
 
maincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdfmaincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdfabiwarmaa
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsAlfonso Peletier
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_papervandna123
 

Semelhante a Lecture20 (20)

Opp compile
Opp compileOpp compile
Opp compile
 
Lecture19
Lecture19Lecture19
Lecture19
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
 
computer science sample papers 2
computer science sample papers 2computer science sample papers 2
computer science sample papers 2
 
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdfDoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
 
Lecture 12: Classes and Files
Lecture 12: Classes and FilesLecture 12: Classes and Files
Lecture 12: Classes and Files
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
 
New presentation oop
New presentation oopNew presentation oop
New presentation oop
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Presentation on template and exception
Presentation  on template and exceptionPresentation  on template and exception
Presentation on template and exception
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
maincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdfmaincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdf
 
C++ via C#
C++ via C#C++ via C#
C++ via C#
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
 
Function in C program
Function in C programFunction in C program
Function in C program
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
C++11
C++11C++11
C++11
 

Último

Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Mohamed Rizk Khodair
 
How to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 InventoryHow to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 InventoryCeline George
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....Ritu480198
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptxPoojaSen20
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17Celine George
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatmentsaipooja36
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxheathfieldcps1
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxLimon Prince
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesAmanpreetKaur157993
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 

Último (20)

Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).
 
How to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 InventoryHow to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 Inventory
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
IPL Online Quiz by Pragya; Question Set.
IPL Online Quiz by Pragya; Question Set.IPL Online Quiz by Pragya; Question Set.
IPL Online Quiz by Pragya; Question Set.
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 

Lecture20

  • 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; } } };