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

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 

Último (20)

Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 

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