Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Lecture 21Virtual Functions
  2. 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. 3. OverloadingPatient: 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. 4. OverridingPatient: 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. 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. 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 Patientclass InPatient : public Patient { InPatient p2;private: int Wardnumber; p2.Setdetails(333,z,12,14); // static binding int Daysinward; p2.Displaydetails(); // static bindingpublic: } void Setdetails (int I, char N, int W, int D) { IdNumber = I; Name = N; Wardnumber = W; Daysinward = D; }
  7. 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. 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 bindingclass 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. 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. 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. 11. Virtual Functions• When a derived class chooses not to define a virtual function, the derived class simply inherits its immediate base classs 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. 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.