SlideShare una empresa de 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; }              }
};

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

Destacado (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
 

Similar 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
 

Similar 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

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
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
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
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
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 

Último (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
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...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
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
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

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