SlideShare a Scribd company logo
1 of 20
1
Polymorphism
2
Object-Oriented Concept
 Encapsulation
 ADT, Object
 Inheritance
 Derived object
 Polymorphism
 Each object knows what it is
3
Polymorphism – An Introduction
 noun, the quality or state of being able to assume different
forms - Webster
 An essential feature of an OO Language
 It builds upon Inheritance
4
Before we proceed….
 Inheritance – Basic Concepts
 Class Hierarchy
 Code Reuse, Easy to maintain
 Type of inheritance : public, private
 Function overriding
5
Class Interface Diagram
Protected data:
hrs
mins
secs
ExtTime class
Set
Increment
Write
Time
Time
Set
Increment
Write
ExtTime
ExtTime
Private data:
zone
Time class
6
Why Polymorphism?--Review:
Time and ExtTime Example by Inheritance
void Print (Time someTime ) //pass an object by value
{
cout << “Time is “ ;
someTime.Write ( ) ;
cout << endl ;
}
CLIENT CODE
Time startTime ( 8, 30, 0 ) ;
ExtTime endTime (10, 45, 0, CST) ;
Print ( startTime ) ;
Print ( endTime ) ;
OUTPUT
Time is 08:30:00
Time is 10:45:00
// Time :: write()
7
Static Binding
 When the type of a formal parameter is a parent class,
the argument used can be:
the same type as the formal parameter,
or,
any derived class type.
 Static binding is the compile-time determination
of which function to call for a particular object
based on the type of the formal parameter
 When pass-by-value is used, static binding occurs
8
Can we do better?
void Print (Time someTime ) //pass an object by value
{
cout << “Time is “ ;
someTime.Write ( ) ;
cout << endl ;
}
CLIENT CODE
Time startTime ( 8, 30, 0 ) ;
ExtTime endTime (10, 45, 0, CST) ;
Print ( startTime ) ;
Print ( endTime ) ;
OUTPUT
Time is 08:30:00
Time is 10:45:00
// Time :: write()
9
Polymorphism – An Introduction
 noun, the quality or state of being able to assume different
forms - Webster
 An essential feature of an OO Language
 It builds upon Inheritance
 Allows run-time interpretation of object type for
a given class hierarchy
 Also Known as “Late Binding”
 Implemented in C++ using virtual functions
10
Dynamic Binding
 Is the run-time determination of which function to
call for a particular object of a derived class based
on the type of the argument
 Declaring a member function to be virtual instructs
the compiler to generate code that guarantees
dynamic binding
 Dynamic binding requires pass-by-reference
11
Virtual Member Function
// SPECIFICATION FILE ( time.h )
class Time
{
public :
. . .
virtual void Write ( ) ; // for dynamic binding
virtual ~Time(); // destructor
private :
int hrs ;
int mins ;
int secs ;
} ;
12
This is the way we like to see…
void Print (Time * someTime )
{
cout << “Time is “ ;
someTime->Write ( ) ;
cout << endl ;
}
CLIENT CODE
Time startTime( 8, 30, 0 ) ;
ExtTime endTime(10, 45, 0, CST) ;
Time *timeptr;
timeptr = &startTime;
Print ( timeptr ) ;
timeptr = &endTime;
Print ( timeptr ) ;
OUTPUT
Time is 08:30:00
Time is 10:45:00 CST
Time::write()
ExtTime::write()
13
Virtual Functions
 Virtual Functions overcome the problem of run time object
determination
 Keyword virtual instructs the compiler to use late binding
and delay the object interpretation
 How ?
 Define a virtual function in the base class. The word virtual
appears only in the base class
 If a base class declares a virtual function, it must
implement that function, even if the body is empty
 Virtual function in base class stays virtual in all the derived
classes
 It can be overridden in the derived classes
 But, a derived class is not required to re-implement a
virtual function. If it does not, the base class version
is used
14
Polymorphism Summary:
 When you use virtual functions, compiler store
additional information about the types of object
available and created
 Polymorphism is supported at this additional
overhead
 Important :
 virtual functions work only with pointers/references
 Not with objects even if the function is virtual
 If a class declares any virtual methods, the
destructor of the class should be declared as virtual
as well.
15
Abstract Classes & Pure Virtual Functions
 Some classes exist logically but not physically.
 Example : Shape
 Shape s; // Legal but silly..!! : “Shapeless shape”
 Shape makes sense only as a base of some classes
derived from it. Serves as a “category”
 Hence instantiation of such a class must be prevented
class Shape //Abstract
{
public :
//Pure virtual Function
virtual void draw() = 0;
}
 A class with one or more pure virtual
functions is an Abstract Class
 Objects of abstract class can’t be
created
Shape s; // error : variable of an abstract class
16
Example
Shape
virtual void draw()
Circle
public void draw()
Triangle
public void draw()
17
 A pure virtual function not defined in the derived
class remains a pure virtual function.
 Hence derived class also becomes abstract
class Circle : public Shape { //No draw() - Abstract
public :
void print(){
cout << “I am a circle” << endl;
}
class Rectangle : public Shape {
public :
void draw(){ // Override Shape::draw()
cout << “Drawing Rectangle” << endl;
}
Rectangle r; // Valid
Circle c; // error : variable of an abstract class
18
Pure virtual functions : Summary
 Pure virtual functions are useful because they
make explicit the abstractness of a class
 Tell both the user and the compiler how it was
intended to be used
 Note : It is a good idea to keep the common code
as close as possible to the root of you hierarchy
19
Summary ..continued
 It is still possible to provide definition of a pure virtual
function in the base class
 The class still remains abstract and functions must be
redefined in the derived classes, but a common piece of
code can be kept there to facilitate reuse
 In this case, they can not be declared inline
class Shape { //Abstract
public :
virtual void draw() = 0;
};
// OK, not defined inline
void Shape::draw(){
cout << “Shape" << endl;
}
class Rectangle : public Shape
{
public :
void draw(){
Shape::draw(); //Reuse
cout <<“Rectangle”<< endl;
}
20
Take Home Message
 Polymorphism is built upon class inheritance
 It allows different versions of a function to be
called in the same manner, with some overhead
 Polymorphism is implemented with virtual
functions, and requires pass-by-reference

More Related Content

Similar to Lecture6.ppt

2nd puc computer science chapter 8 function overloading
 2nd puc computer science chapter 8   function overloading 2nd puc computer science chapter 8   function overloading
2nd puc computer science chapter 8 function overloadingAahwini Esware gowda
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++Amresh Raj
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Abid Kohistani
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classDeepak Singh
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Abu Saleh
 
Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Asfand Hassan
 
Polymorphism & Templates
Polymorphism & TemplatesPolymorphism & Templates
Polymorphism & TemplatesMeghaj Mallick
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesDurgesh Singh
 
Example for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdfExample for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdfrajaratna4
 
C# for C++ programmers
C# for C++ programmersC# for C++ programmers
C# for C++ programmersMark Whitaker
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptishan743441
 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_typesAbed Bukhari
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.pptEmanAsem4
 

Similar to Lecture6.ppt (20)

2nd puc computer science chapter 8 function overloading
 2nd puc computer science chapter 8   function overloading 2nd puc computer science chapter 8   function overloading
2nd puc computer science chapter 8 function overloading
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
 
Compose Camp - Intro.pdf
Compose Camp - Intro.pdfCompose Camp - Intro.pdf
Compose Camp - Intro.pdf
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
C++ language
C++ languageC++ language
C++ language
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
 
Polymorphismupload
PolymorphismuploadPolymorphismupload
Polymorphismupload
 
Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)
 
Polymorphism & Templates
Polymorphism & TemplatesPolymorphism & Templates
Polymorphism & Templates
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
 
Example for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdfExample for Virtual and Pure Virtual function.pdf
Example for Virtual and Pure Virtual function.pdf
 
C# for C++ programmers
C# for C++ programmersC# for C++ programmers
C# for C++ programmers
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_types
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Swift, swiftly
Swift, swiftlySwift, swiftly
Swift, swiftly
 

Recently uploaded

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 

Recently uploaded (20)

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 

Lecture6.ppt

  • 2. 2 Object-Oriented Concept  Encapsulation  ADT, Object  Inheritance  Derived object  Polymorphism  Each object knows what it is
  • 3. 3 Polymorphism – An Introduction  noun, the quality or state of being able to assume different forms - Webster  An essential feature of an OO Language  It builds upon Inheritance
  • 4. 4 Before we proceed….  Inheritance – Basic Concepts  Class Hierarchy  Code Reuse, Easy to maintain  Type of inheritance : public, private  Function overriding
  • 5. 5 Class Interface Diagram Protected data: hrs mins secs ExtTime class Set Increment Write Time Time Set Increment Write ExtTime ExtTime Private data: zone Time class
  • 6. 6 Why Polymorphism?--Review: Time and ExtTime Example by Inheritance void Print (Time someTime ) //pass an object by value { cout << “Time is “ ; someTime.Write ( ) ; cout << endl ; } CLIENT CODE Time startTime ( 8, 30, 0 ) ; ExtTime endTime (10, 45, 0, CST) ; Print ( startTime ) ; Print ( endTime ) ; OUTPUT Time is 08:30:00 Time is 10:45:00 // Time :: write()
  • 7. 7 Static Binding  When the type of a formal parameter is a parent class, the argument used can be: the same type as the formal parameter, or, any derived class type.  Static binding is the compile-time determination of which function to call for a particular object based on the type of the formal parameter  When pass-by-value is used, static binding occurs
  • 8. 8 Can we do better? void Print (Time someTime ) //pass an object by value { cout << “Time is “ ; someTime.Write ( ) ; cout << endl ; } CLIENT CODE Time startTime ( 8, 30, 0 ) ; ExtTime endTime (10, 45, 0, CST) ; Print ( startTime ) ; Print ( endTime ) ; OUTPUT Time is 08:30:00 Time is 10:45:00 // Time :: write()
  • 9. 9 Polymorphism – An Introduction  noun, the quality or state of being able to assume different forms - Webster  An essential feature of an OO Language  It builds upon Inheritance  Allows run-time interpretation of object type for a given class hierarchy  Also Known as “Late Binding”  Implemented in C++ using virtual functions
  • 10. 10 Dynamic Binding  Is the run-time determination of which function to call for a particular object of a derived class based on the type of the argument  Declaring a member function to be virtual instructs the compiler to generate code that guarantees dynamic binding  Dynamic binding requires pass-by-reference
  • 11. 11 Virtual Member Function // SPECIFICATION FILE ( time.h ) class Time { public : . . . virtual void Write ( ) ; // for dynamic binding virtual ~Time(); // destructor private : int hrs ; int mins ; int secs ; } ;
  • 12. 12 This is the way we like to see… void Print (Time * someTime ) { cout << “Time is “ ; someTime->Write ( ) ; cout << endl ; } CLIENT CODE Time startTime( 8, 30, 0 ) ; ExtTime endTime(10, 45, 0, CST) ; Time *timeptr; timeptr = &startTime; Print ( timeptr ) ; timeptr = &endTime; Print ( timeptr ) ; OUTPUT Time is 08:30:00 Time is 10:45:00 CST Time::write() ExtTime::write()
  • 13. 13 Virtual Functions  Virtual Functions overcome the problem of run time object determination  Keyword virtual instructs the compiler to use late binding and delay the object interpretation  How ?  Define a virtual function in the base class. The word virtual appears only in the base class  If a base class declares a virtual function, it must implement that function, even if the body is empty  Virtual function in base class stays virtual in all the derived classes  It can be overridden in the derived classes  But, a derived class is not required to re-implement a virtual function. If it does not, the base class version is used
  • 14. 14 Polymorphism Summary:  When you use virtual functions, compiler store additional information about the types of object available and created  Polymorphism is supported at this additional overhead  Important :  virtual functions work only with pointers/references  Not with objects even if the function is virtual  If a class declares any virtual methods, the destructor of the class should be declared as virtual as well.
  • 15. 15 Abstract Classes & Pure Virtual Functions  Some classes exist logically but not physically.  Example : Shape  Shape s; // Legal but silly..!! : “Shapeless shape”  Shape makes sense only as a base of some classes derived from it. Serves as a “category”  Hence instantiation of such a class must be prevented class Shape //Abstract { public : //Pure virtual Function virtual void draw() = 0; }  A class with one or more pure virtual functions is an Abstract Class  Objects of abstract class can’t be created Shape s; // error : variable of an abstract class
  • 16. 16 Example Shape virtual void draw() Circle public void draw() Triangle public void draw()
  • 17. 17  A pure virtual function not defined in the derived class remains a pure virtual function.  Hence derived class also becomes abstract class Circle : public Shape { //No draw() - Abstract public : void print(){ cout << “I am a circle” << endl; } class Rectangle : public Shape { public : void draw(){ // Override Shape::draw() cout << “Drawing Rectangle” << endl; } Rectangle r; // Valid Circle c; // error : variable of an abstract class
  • 18. 18 Pure virtual functions : Summary  Pure virtual functions are useful because they make explicit the abstractness of a class  Tell both the user and the compiler how it was intended to be used  Note : It is a good idea to keep the common code as close as possible to the root of you hierarchy
  • 19. 19 Summary ..continued  It is still possible to provide definition of a pure virtual function in the base class  The class still remains abstract and functions must be redefined in the derived classes, but a common piece of code can be kept there to facilitate reuse  In this case, they can not be declared inline class Shape { //Abstract public : virtual void draw() = 0; }; // OK, not defined inline void Shape::draw(){ cout << “Shape" << endl; } class Rectangle : public Shape { public : void draw(){ Shape::draw(); //Reuse cout <<“Rectangle”<< endl; }
  • 20. 20 Take Home Message  Polymorphism is built upon class inheritance  It allows different versions of a function to be called in the same manner, with some overhead  Polymorphism is implemented with virtual functions, and requires pass-by-reference