SlideShare a Scribd company logo
Lecture 21



Virtual Functions
Introduction:
               Overloading vs. Overriding


• Overloading is a form of polymorphism which means
  that a program construct which has a certain
  appearance can mean different things (for example, calls
  to different functions) depending on the types of the
  parameters involved.    Example

• Overriding is also a form of polymorphism which means
  that a derived class defines a function that has similar
  name and no. & types of parameters as its base class
  but the implementation of both functions are different.
                           Example
Overloading

Patient:
 Data Member:
       IdNumber, PatName
 Function member:
       SetDetails(int , char)



Inpatient:
 Data Member:
       Wardnumber, Daysinward
 Function member:
       SetDetails(int, char, int, int)
       // overload function member
Overriding

Patient:
 Data Member:
       IdNumber, PatName
 Function member:
       DisplayDetails() { cout<<IdNumber<<PatNumber; }



Inpatient:
 Data Member:
       Wardnumber, Daysinward
 Function member:
       DisplayDetails() {cout<<Wardnumber<<Daysinward;}
       // override function member
Introduction
• Binding means deciding exactly which form or function
  is appropriate.
• Binding occurs during
   • during compilation is called static or early binding,
   • during program execution is called dynamic or late
     binding.
                                 Dynamic Binding
 Static Binding                  In C++, to implement dynamic
 Usually is known ad hoc         binding, use virtual functions.
 polymorphism.
                                 A member function is declared
                                 as virtual by writing the word
 Performed at compile-time       virtual first in the
 when a function is called via   declaration.
 a specific object or via a
 pointer to an object            Syntax:
                                     virtual return_type
                                     function_name (arg_list….)
Static Binding : Sample Program

#include <iostream.h>                              void Displaydetails()
                                                      { cout<<endl<<"Inpatient:"<<IdNumber<<
class Patient {                                         Wardnumber<<Daysinward; }
public:                                            };
    int IdNumber; char Name;
   void Setdetails (int I, char N)                 void main()
   { IdNumber = I; Name = N;             }         { Patient p1;
   void Displaydetails()                                 p1.Setdetails(111,'a');     // static binding
   { cout<<endl<<"Patient:"<<IdNumber                    p1.Displaydetails();       // static binding
       <<Name; } }; // end class Patient

class InPatient : public Patient {                     InPatient p2;
private: int Wardnumber;                                 p2.Setdetails(333,'z',12,14); // static binding
              int Daysinward;                            p2.Displaydetails(); // static binding
public:                                            }
   void Setdetails (int I, char N, int W, int D)
   { IdNumber = I; Name = N;
       Wardnumber = W;
       Daysinward = D;              }
Points on Dynamic Binding
•   A virtual function tells the compiler to create a pointer to a function
    but not to fill in the value of the pointer until the function is actually
    called
•   A class which contains at least one virtual function, or which
    inherits a virtual function,     is called a
                               polymorphic class.


class Shape                              class Shape
{ private: ……                            { protected: ……
   public: virtual void display()           public: virtual void display()
      { ……..}                                  { ……..}
 :                                          :
 :                                          :
};                                       };
                                         class Rectangle: public Shape
                                         { ………… };
Dynamic Binding : Sample Program 1

#include <iostream.h>                                   void Displaydetails()
                                                        { cout<<"Inpatient No: "<<IdNumber<<endl;
class Patient {                                           cout<<“ Name: “<<Name<<endl;
protected:                                                cout<<“Ward No: “<<Wardnumber<<endl;
           int IdNumber; char PatName;                    cout<<“Days: ”<< Daysinward<<endl;};
public:                                            };
   void Setdetails (int I, char N)
  {……..}                                           void main()
                                                   {         Patient *Pat1, Pat2;
   virtual void Displaydetails()                             Pat2.Setdetails(999, ’A');
  { cout<<"Patient No: "<<IdNumber<<endl;                    Pat1 = &Pat2;
   cout<< ”Name: “<<PatName<<endl; }                         Pat1 -> Displaydetails();
};                                                                     //dynamic binding

class InPatient : public Patient {                             InPatient InPat;
private:                                                       InPat.Setdetails(333, ’Z', 12, 14);
   int Wardnumber;       int Daysinward;                       Pat1 = &InPat;
public:                                                        Pat1 -> Displaydetails();
   void Setdetails (int I, char N, int W, int D)                         //dynamic binding
   { …….. }                                        }
Dynamic Binding : Sample Program 2

#include <iostream.h>                              class Professor : public Person {
#include <string.h>                                private:
class Person {                                                 int publs;
protected:                                         public:
            char *name;                                 Professor (char* s, int n) : Person(s), publs(n) {}
public:                                                 void print() { cout<<"My name is "<<name
   Person(char *s) {………..}                              << " and I have "<<
   virtual void print() {                          publs<<"publications."; }
      cout<<"My name is "<<name<< ".n"; }         }; // End class Professor
}; // End class Person                             void main()
                                                   { Person* P;
class Student : public Person {                          Person P2("Ali");
private:                                                 Student Stud1("Fitri", 3.56);
           double gpa;                                   Professor Prof1("Halizah", 5);
public:                                                        P = &P2;           P->print();
  Student (char* s, double g) : Person(s) {….. }                                        //dynamic binding
                                                               P = &Stud1; P->print();
  void print() { cout<<"My name is "<<name                                              //dynamic binding
  << " and my G.P.A. is "<< gpa<<".n"; }                      P = &Prof1;         P->print();
}; //End class Student                                                                  //dynamic binding
                                                   } // End main block
Virtual Functions

• Once a function is declared virtual, it remains virtual all
  the way down the inheritance hierarchy when a class
  overrides it.         All display() are virtual except display()
         Person                              Person
              - virtual display()                 - display()

        Lecturer                            Lecturer
             - display()                         - virtual display()

Full Time         Part Time         Full Time        Part Time
- display()       - display()       - display()      - display()

All display() are virtual
Virtual Functions

• When a derived class chooses not to define a virtual
  function, the derived class simply inherits its immediate
  base class's virtual function definition.

           Person                     Class Lecturer inherits the
                - virtual display()   virtual function display() from
                                      class Person.
          Lecturer
               - setvalue()

  Full Time        Part Time
  - getvalue()     - getvalue()
Virtual Functions: When it is useful?


                                 Shape
                          virtual void draw();




     Circle          Triangle       Rectangle      Square
     void draw();    void draw();   void draw();   void draw();



• Each sub-class has its different definition of draw() function.
• Declare the draw() function in class Shape as virtual function.
• Each sub-class overrides the virtual function.

More Related Content

What's hot

Brief Summary Of C++
Brief Summary Of C++Brief Summary Of C++
Brief Summary Of C++
Haris Lye
 
Have Your Cake and Eat It Too: Meta-Programming Techniques for Java
Have Your Cake and Eat It Too: Meta-Programming Techniques for JavaHave Your Cake and Eat It Too: Meta-Programming Techniques for Java
Have Your Cake and Eat It Too: Meta-Programming Techniques for JavaHoward Lewis Ship
 
7 rules of simple and maintainable code
7 rules of simple and maintainable code7 rules of simple and maintainable code
7 rules of simple and maintainable code
Geshan Manandhar
 
Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code Smells
Mario Sangiorgio
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
Julie Iskander
 
Decaf language specification
Decaf language specificationDecaf language specification
Decaf language specification
Sami Said
 
Introduction to classes the concept of a class/tutorialoutlet
Introduction to classes the concept of a class/tutorialoutletIntroduction to classes the concept of a class/tutorialoutlet
Introduction to classes the concept of a class/tutorialoutlet
Oldingz
 
Clean code - Agile Software Craftsmanship
Clean code - Agile Software CraftsmanshipClean code - Agile Software Craftsmanship
Clean code - Agile Software Craftsmanship
Yukti Kaura
 
Clean code
Clean codeClean code
Clean code
ifnu bima
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Railselliando dias
 
The Xtext Grammar Language
The Xtext Grammar LanguageThe Xtext Grammar Language
The Xtext Grammar Language
Dr. Jan Köhnlein
 
Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009
Heiko Behrens
 
Clean code
Clean codeClean code
Clean code
Henrique Smoco
 
03 - Qt UI Development
03 - Qt UI Development03 - Qt UI Development
03 - Qt UI Development
Andreas Jakl
 
Objective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchObjective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central Dispatch
Matteo Battaglio
 
Kotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRreadyKotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRready
MobileAcademy
 

What's hot (19)

Brief Summary Of C++
Brief Summary Of C++Brief Summary Of C++
Brief Summary Of C++
 
Have Your Cake and Eat It Too: Meta-Programming Techniques for Java
Have Your Cake and Eat It Too: Meta-Programming Techniques for JavaHave Your Cake and Eat It Too: Meta-Programming Techniques for Java
Have Your Cake and Eat It Too: Meta-Programming Techniques for Java
 
7 rules of simple and maintainable code
7 rules of simple and maintainable code7 rules of simple and maintainable code
7 rules of simple and maintainable code
 
Clean code and Code Smells
Clean code and Code SmellsClean code and Code Smells
Clean code and Code Smells
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
Devoxx 2012 (v2)
Devoxx 2012 (v2)Devoxx 2012 (v2)
Devoxx 2012 (v2)
 
Decaf language specification
Decaf language specificationDecaf language specification
Decaf language specification
 
Introduction to classes the concept of a class/tutorialoutlet
Introduction to classes the concept of a class/tutorialoutletIntroduction to classes the concept of a class/tutorialoutlet
Introduction to classes the concept of a class/tutorialoutlet
 
Clean code - Agile Software Craftsmanship
Clean code - Agile Software CraftsmanshipClean code - Agile Software Craftsmanship
Clean code - Agile Software Craftsmanship
 
Eclipse Banking Day
Eclipse Banking DayEclipse Banking Day
Eclipse Banking Day
 
Clean code
Clean codeClean code
Clean code
 
Rapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and RailsRapid Development with Ruby/JRuby and Rails
Rapid Development with Ruby/JRuby and Rails
 
The Xtext Grammar Language
The Xtext Grammar LanguageThe Xtext Grammar Language
The Xtext Grammar Language
 
Test Engine
Test EngineTest Engine
Test Engine
 
Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009
 
Clean code
Clean codeClean code
Clean code
 
03 - Qt UI Development
03 - Qt UI Development03 - Qt UI Development
03 - Qt UI Development
 
Objective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchObjective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central Dispatch
 
Kotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRreadyKotlin for Android - Vali Iorgu - mRready
Kotlin for Android - Vali Iorgu - mRready
 

Viewers also liked (8)

Lecture20
Lecture20Lecture20
Lecture20
 
Springwood at music resonate
Springwood at music resonateSpringwood at music resonate
Springwood at music resonate
 
Lecture10
Lecture10Lecture10
Lecture10
 
Lecture16
Lecture16Lecture16
Lecture16
 
Facebook經營觀察 0820
Facebook經營觀察 0820Facebook經營觀察 0820
Facebook經營觀察 0820
 
Lecture09
Lecture09Lecture09
Lecture09
 
Lecture17
Lecture17Lecture17
Lecture17
 
Lecture05
Lecture05Lecture05
Lecture05
 

Similar to Lecture21

Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsPrincess Sam
 
Virtual function
Virtual functionVirtual function
Virtual function
harman kaur
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
RashidFaridChishti
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and ObjectsPayel Guria
 
OOPS 22-23 (1).pptx
OOPS 22-23 (1).pptxOOPS 22-23 (1).pptx
OOPS 22-23 (1).pptx
SushmaGavaraskar
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functionsPrincess Sam
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
Abu Saleh
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
instaface
 
polymorpisum-140106223024-phpapp01.pdf
polymorpisum-140106223024-phpapp01.pdfpolymorpisum-140106223024-phpapp01.pdf
polymorpisum-140106223024-phpapp01.pdf
BapanKar2
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
Teksify
 
3. Объекты, классы и пакеты в Java
3. Объекты, классы и пакеты в Java3. Объекты, классы и пакеты в Java
3. Объекты, классы и пакеты в Java
DEVTYPE
 
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
.NET Conf UY
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2
Aadil Ansari
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
RutujaTandalwade
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
Using the Windows 8 Runtime from C++
Using the Windows 8 Runtime from C++Using the Windows 8 Runtime from C++

Similar to Lecture21 (20)

Lecture18
Lecture18Lecture18
Lecture18
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptxObject Oriented Programming using C++: Ch11 Virtual Functions.pptx
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
 
OOPS 22-23 (1).pptx
OOPS 22-23 (1).pptxOOPS 22-23 (1).pptx
OOPS 22-23 (1).pptx
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functions
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
Test Engine
Test EngineTest Engine
Test Engine
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 
polymorpisum-140106223024-phpapp01.pdf
polymorpisum-140106223024-phpapp01.pdfpolymorpisum-140106223024-phpapp01.pdf
polymorpisum-140106223024-phpapp01.pdf
 
Mattbrenner
MattbrennerMattbrenner
Mattbrenner
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
 
3. Объекты, классы и пакеты в Java
3. Объекты, классы и пакеты в Java3. Объекты, классы и пакеты в Java
3. Объекты, классы и пакеты в Java
 
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Using the Windows 8 Runtime from C++
Using the Windows 8 Runtime from C++Using the Windows 8 Runtime from C++
Using the Windows 8 Runtime from C++
 

Recently uploaded

PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptxFresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
SriSurya50
 
Reflective and Evaluative Practice PowerPoint
Reflective and Evaluative Practice PowerPointReflective and Evaluative Practice PowerPoint
Reflective and Evaluative Practice PowerPoint
amberjdewit93
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
AG2 Design
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
What is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptxWhat is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptx
christianmathematics
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 

Recently uploaded (20)

PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptxFresher’s Quiz 2023 at GMC Nizamabad.pptx
Fresher’s Quiz 2023 at GMC Nizamabad.pptx
 
Reflective and Evaluative Practice PowerPoint
Reflective and Evaluative Practice PowerPointReflective and Evaluative Practice PowerPoint
Reflective and Evaluative Practice PowerPoint
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Delivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and TrainingDelivering Micro-Credentials in Technical and Vocational Education and Training
Delivering Micro-Credentials in Technical and Vocational Education and Training
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
What is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptxWhat is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptx
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 

Lecture21

  • 2. Introduction: Overloading vs. Overriding • Overloading is a form of polymorphism which means that a program construct which has a certain appearance can mean different things (for example, calls to different functions) depending on the types of the parameters involved. Example • Overriding is also a form of polymorphism which means that a derived class defines a function that has similar name and no. & types of parameters as its base class but the implementation of both functions are different. Example
  • 3. Overloading Patient: Data Member: IdNumber, PatName Function member: SetDetails(int , char) Inpatient: Data Member: Wardnumber, Daysinward Function member: SetDetails(int, char, int, int) // overload function member
  • 4. Overriding Patient: Data Member: IdNumber, PatName Function member: DisplayDetails() { cout<<IdNumber<<PatNumber; } Inpatient: Data Member: Wardnumber, Daysinward Function member: DisplayDetails() {cout<<Wardnumber<<Daysinward;} // override function member
  • 5. Introduction • Binding means deciding exactly which form or function is appropriate. • Binding occurs during • during compilation is called static or early binding, • during program execution is called dynamic or late binding. Dynamic Binding Static Binding In C++, to implement dynamic Usually is known ad hoc binding, use virtual functions. polymorphism. A member function is declared as virtual by writing the word Performed at compile-time virtual first in the when a function is called via declaration. a specific object or via a pointer to an object Syntax: virtual return_type function_name (arg_list….)
  • 6. Static Binding : Sample Program #include <iostream.h> void Displaydetails() { cout<<endl<<"Inpatient:"<<IdNumber<< class Patient { Wardnumber<<Daysinward; } public: }; int IdNumber; char Name; void Setdetails (int I, char N) void main() { IdNumber = I; Name = N; } { Patient p1; void Displaydetails() p1.Setdetails(111,'a'); // static binding { cout<<endl<<"Patient:"<<IdNumber p1.Displaydetails(); // static binding <<Name; } }; // end class Patient class InPatient : public Patient { InPatient p2; private: int Wardnumber; p2.Setdetails(333,'z',12,14); // static binding int Daysinward; p2.Displaydetails(); // static binding public: } void Setdetails (int I, char N, int W, int D) { IdNumber = I; Name = N; Wardnumber = W; Daysinward = D; }
  • 7. Points on Dynamic Binding • A virtual function tells the compiler to create a pointer to a function but not to fill in the value of the pointer until the function is actually called • A class which contains at least one virtual function, or which inherits a virtual function, is called a polymorphic class. class Shape class Shape { private: …… { protected: …… public: virtual void display() public: virtual void display() { ……..} { ……..} : : : : }; }; class Rectangle: public Shape { ………… };
  • 8. Dynamic Binding : Sample Program 1 #include <iostream.h> void Displaydetails() { cout<<"Inpatient No: "<<IdNumber<<endl; class Patient { cout<<“ Name: “<<Name<<endl; protected: cout<<“Ward No: “<<Wardnumber<<endl; int IdNumber; char PatName; cout<<“Days: ”<< Daysinward<<endl;}; public: }; void Setdetails (int I, char N) {……..} void main() { Patient *Pat1, Pat2; virtual void Displaydetails() Pat2.Setdetails(999, ’A'); { cout<<"Patient No: "<<IdNumber<<endl; Pat1 = &Pat2; cout<< ”Name: “<<PatName<<endl; } Pat1 -> Displaydetails(); }; //dynamic binding class InPatient : public Patient { InPatient InPat; private: InPat.Setdetails(333, ’Z', 12, 14); int Wardnumber; int Daysinward; Pat1 = &InPat; public: Pat1 -> Displaydetails(); void Setdetails (int I, char N, int W, int D) //dynamic binding { …….. } }
  • 9. Dynamic Binding : Sample Program 2 #include <iostream.h> class Professor : public Person { #include <string.h> private: class Person { int publs; protected: public: char *name; Professor (char* s, int n) : Person(s), publs(n) {} public: void print() { cout<<"My name is "<<name Person(char *s) {………..} << " and I have "<< virtual void print() { publs<<"publications."; } cout<<"My name is "<<name<< ".n"; } }; // End class Professor }; // End class Person void main() { Person* P; class Student : public Person { Person P2("Ali"); private: Student Stud1("Fitri", 3.56); double gpa; Professor Prof1("Halizah", 5); public: P = &P2; P->print(); Student (char* s, double g) : Person(s) {….. } //dynamic binding P = &Stud1; P->print(); void print() { cout<<"My name is "<<name //dynamic binding << " and my G.P.A. is "<< gpa<<".n"; } P = &Prof1; P->print(); }; //End class Student //dynamic binding } // End main block
  • 10. Virtual Functions • Once a function is declared virtual, it remains virtual all the way down the inheritance hierarchy when a class overrides it. All display() are virtual except display() Person Person - virtual display() - display() Lecturer Lecturer - display() - virtual display() Full Time Part Time Full Time Part Time - display() - display() - display() - display() All display() are virtual
  • 11. Virtual Functions • When a derived class chooses not to define a virtual function, the derived class simply inherits its immediate base class's virtual function definition. Person Class Lecturer inherits the - virtual display() virtual function display() from class Person. Lecturer - setvalue() Full Time Part Time - getvalue() - getvalue()
  • 12. Virtual Functions: When it is useful? Shape virtual void draw(); Circle Triangle Rectangle Square void draw(); void draw(); void draw(); void draw(); • Each sub-class has its different definition of draw() function. • Declare the draw() function in class Shape as virtual function. • Each sub-class overrides the virtual function.