SlideShare a Scribd company logo
Programmation Orienté Object (POO)
Dr. Rida El Chall
3ème année – Semestre 5
2021 - 2022
Université Libanaise – Faculté de Génie
7. Virtual Functions,
and Abstract Classes
Inheritance, Pointers, And Virtual Functions
2
3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
▪ Recall that as a parameter, a class object can be passed either
by value or by reference.
▪ The types of the actual and formal parameters must match.
▪ However, in the case of classes, C++ allows the user to pass an
object of a derived class to a formal parameter of the base
class type.
▪ in compile-time binding, the necessary code to call a specific
function is generated by the compiler. (Compile-time binding
is also known as static binding or early binding.)
Inheritance, Pointers : Example
3
3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
Inheritance, Pointers : Example
4
3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
The function callPrint has a formal reference parameter p of type petType. You can
call the function callPrint by using an object of either type petType or type dogType
as a parameter.
Inheritance, Pointers : Example
5
3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
Virtual Functions
▪ C++ corrects this problem by providing the mechanism of
virtual functions.
▪ The binding of virtual functions occurs at program
execution time, not at compile time.
▪ This kind of binding is called run-time binding or late
binding or dynamic binding.
▪ in run-time binding, the compiler does not generate the
code to call a specific function. Instead, it generates enough
information to enable the run-time system to generate the
specific code for the appropriate function call.
▪ In C++, virtual functions are declared using the reserved
word virtual.
▪ We need to declare a virtual function only in the base class.
6
3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
Inheritance, Pointers, And Virtual Functions
7
3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
Inheritance, Pointers, And Virtual Functions
▪ The previous discussion also applies when a formal parameter is a
pointer to a class, and a pointer of the derived class is passed as an
actual parameter.
8
3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
Inheritance, Pointers, And Virtual Functions
▪ However, if p is a value parameter, then this mechanism of passing
a derived class object as an actual parameter to p does not work,
even if p uses a virtual function.
▪ Recall that, if a formal parameter is a value parameter, the value of
the actual parameter is copied into the formal parameter.
▪ The member variables of dog are copied into the member variables of p.
▪ p is an object of type petType, it has only one member variable. Only the
member variable name of dog will be copied into the member variable
name of p.
▪ Also the statement p.print(); in the body of the function will result in
executing the member function print of the class petType.
9
3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
void callPrint(petType p) //p is a value parameter
{
p.print();
}
dogType dog;
callPrint(dog);
Classes and Virtual Destructors
▪ One thing recommended for classes with pointer member
variables is that these classes should have the destructor.
▪ The destructor executes automatically when the class object
goes out of scope.
▪ if the object creates dynamic memory space, the destructor
can be designed to deallocate that memory space.
▪ If a derived class object is passed to a formal parameter of
the base class type, when the object goes out of scope, the
destructor of the base class.
▪ however, the destructor of the derived class should be
executed when the derived class object goes out of scope. →
▪ The virtual destructor of a base class automatically makes the
destructor of a derived class virtual.
10
3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
Classes and Virtual Destructors
▪ When a derived class object is passed to a formal parameter
of the base class type, then when the object goes out of
scope, the destructor of the derived class executes.
▪ After executing the destructor of the derived class, the
destructor of the base class executes.
▪ If a base class contains virtual functions, make the
destructor of the base class virtual.
11
3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
Abstract Classes and Pure Virtual Functions
▪ Through inheritance we can derive new classes without
designing them from scratch.
▪ There are many scenarios for which a class is desired to be
served as a base class for a number of derived classes;
however, the base class may contain certain functions that
may not have meaningful definitions in the base class.
▪ consider the class shape; from the class shape, you can
derive other classes, such as rectangle, circle, ellipse,
12
3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
Abstract Classes and Pure Virtual Functions
▪ The definitions of the functions draw and move are specific to a
particular shape, each derived class can provide an appropriate
definition of these functions.
▪ the functions draw and move virtual to enforce run-time binding of
these functions.
▪ This definition of the class shape requires you to write the definitions of
the functions draw and move.
13
3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
body of these functions empty!
Drawback: user can create objects of class shape,
however there is no shape to work with
Abstract Classes and Pure Virtual Functions
▪ convert these functions to pure virtual functions
▪ Once you make these functions pure virtual functions in the
class shape, no longer need to provide the definitions of
these functions for the class shape.
▪ Once a class contains one or more pure virtual, then that
class is called an abstract class.
14
3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
Abstract Classes and Pure Virtual Functions
▪ Because an abstract class is not a complete class, as it (or
implementation file) does not contain the definitions of certain
functions, you cannot create objects of that class.
▪ Note that in addition to the pure virtual functions, an abstract
class can contain instance variables, constructors, and functions
that are not pure virtual. However, the abstract class must
provide the definitions of the constructor and functions that are
not pure virtual.
15
3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
Abstract Classes and Pure Virtual Functions
▪ suppose that we derive the class rectangle from the class
shape.
▪ To make rectangle a non abstract class so that we can
create objects of this class, the class (or its implementation
file) must provide the definitions of the pure virtual
functions of its base class, which is the class shape.
16
3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes

More Related Content

Similar to 6_VirtualFunctions_AbstractClasses.pdf

Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
ishan743441
 
7. Pointers and Virtual functions final -3.pptx
7. Pointers and Virtual functions final -3.pptx7. Pointers and Virtual functions final -3.pptx
7. Pointers and Virtual functions final -3.pptx
Abhishekkumarsingh630054
 
Chapter11.ppt
Chapter11.pptChapter11.ppt
Chapter11.ppt
KalyaniLokhande5
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
ReKruiTIn.com
 
Polymorphismupload
PolymorphismuploadPolymorphismupload
Polymorphismupload
Mukhtar_Hunzai
 
UNIT IV (1).ppt
UNIT IV (1).pptUNIT IV (1).ppt
UNIT IV (1).ppt
VGaneshKarthikeyan
 
Lecture6.ppt
Lecture6.pptLecture6.ppt
Lecture6.ppt
ammu241754
 
Virtual function
Virtual functionVirtual function
Virtual function
sdrhr
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
Rabin BK
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_i
Nico Ludwig
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
anshunjain
 
Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)
Hassan Hashmi
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
Gamindu Udayanga
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Maaz Hasan
 
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
 
C++ Training
C++ TrainingC++ Training
C++ Training
SubhendraBasu5
 
09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects concept09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects concept
kavitamittal18
 
09slide.ppt
09slide.ppt09slide.ppt
09slide.ppt
kavitamittal18
 
C++ interview questions
C++ interview questionsC++ interview questions
C++ interview questions
arjavi
 

Similar to 6_VirtualFunctions_AbstractClasses.pdf (20)

Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
 
7. Pointers and Virtual functions final -3.pptx
7. Pointers and Virtual functions final -3.pptx7. Pointers and Virtual functions final -3.pptx
7. Pointers and Virtual functions final -3.pptx
 
Chapter11.ppt
Chapter11.pptChapter11.ppt
Chapter11.ppt
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
 
Polymorphismupload
PolymorphismuploadPolymorphismupload
Polymorphismupload
 
UNIT IV (1).ppt
UNIT IV (1).pptUNIT IV (1).ppt
UNIT IV (1).ppt
 
Lecture6.ppt
Lecture6.pptLecture6.ppt
Lecture6.ppt
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_i
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
 
Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
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
 
C++ Training
C++ TrainingC++ Training
C++ Training
 
09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects concept09slide.ppt oops classes and objects concept
09slide.ppt oops classes and objects concept
 
09slide.ppt
09slide.ppt09slide.ppt
09slide.ppt
 
C++ interview questions
C++ interview questionsC++ interview questions
C++ interview questions
 

Recently uploaded

Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"
National Information Standards Organization (NISO)
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
khuleseema60
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
TechSoup
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
nitinpv4ai
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
nitinpv4ai
 
How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
Celine George
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
nitinpv4ai
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
indexPub
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
EduSkills OECD
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
ImMuslim
 
Skimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S EliotSkimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S Eliot
nitinpv4ai
 

Recently uploaded (20)

Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"Benner "Expanding Pathways to Publishing Careers"
Benner "Expanding Pathways to Publishing Careers"
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
 
How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
 
Skimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S EliotSkimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S Eliot
 

6_VirtualFunctions_AbstractClasses.pdf

  • 1. Programmation Orienté Object (POO) Dr. Rida El Chall 3ème année – Semestre 5 2021 - 2022 Université Libanaise – Faculté de Génie 7. Virtual Functions, and Abstract Classes
  • 2. Inheritance, Pointers, And Virtual Functions 2 3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes ▪ Recall that as a parameter, a class object can be passed either by value or by reference. ▪ The types of the actual and formal parameters must match. ▪ However, in the case of classes, C++ allows the user to pass an object of a derived class to a formal parameter of the base class type. ▪ in compile-time binding, the necessary code to call a specific function is generated by the compiler. (Compile-time binding is also known as static binding or early binding.)
  • 3. Inheritance, Pointers : Example 3 3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
  • 4. Inheritance, Pointers : Example 4 3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes The function callPrint has a formal reference parameter p of type petType. You can call the function callPrint by using an object of either type petType or type dogType as a parameter.
  • 5. Inheritance, Pointers : Example 5 3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
  • 6. Virtual Functions ▪ C++ corrects this problem by providing the mechanism of virtual functions. ▪ The binding of virtual functions occurs at program execution time, not at compile time. ▪ This kind of binding is called run-time binding or late binding or dynamic binding. ▪ in run-time binding, the compiler does not generate the code to call a specific function. Instead, it generates enough information to enable the run-time system to generate the specific code for the appropriate function call. ▪ In C++, virtual functions are declared using the reserved word virtual. ▪ We need to declare a virtual function only in the base class. 6 3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
  • 7. Inheritance, Pointers, And Virtual Functions 7 3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
  • 8. Inheritance, Pointers, And Virtual Functions ▪ The previous discussion also applies when a formal parameter is a pointer to a class, and a pointer of the derived class is passed as an actual parameter. 8 3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
  • 9. Inheritance, Pointers, And Virtual Functions ▪ However, if p is a value parameter, then this mechanism of passing a derived class object as an actual parameter to p does not work, even if p uses a virtual function. ▪ Recall that, if a formal parameter is a value parameter, the value of the actual parameter is copied into the formal parameter. ▪ The member variables of dog are copied into the member variables of p. ▪ p is an object of type petType, it has only one member variable. Only the member variable name of dog will be copied into the member variable name of p. ▪ Also the statement p.print(); in the body of the function will result in executing the member function print of the class petType. 9 3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes void callPrint(petType p) //p is a value parameter { p.print(); } dogType dog; callPrint(dog);
  • 10. Classes and Virtual Destructors ▪ One thing recommended for classes with pointer member variables is that these classes should have the destructor. ▪ The destructor executes automatically when the class object goes out of scope. ▪ if the object creates dynamic memory space, the destructor can be designed to deallocate that memory space. ▪ If a derived class object is passed to a formal parameter of the base class type, when the object goes out of scope, the destructor of the base class. ▪ however, the destructor of the derived class should be executed when the derived class object goes out of scope. → ▪ The virtual destructor of a base class automatically makes the destructor of a derived class virtual. 10 3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
  • 11. Classes and Virtual Destructors ▪ When a derived class object is passed to a formal parameter of the base class type, then when the object goes out of scope, the destructor of the derived class executes. ▪ After executing the destructor of the derived class, the destructor of the base class executes. ▪ If a base class contains virtual functions, make the destructor of the base class virtual. 11 3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
  • 12. Abstract Classes and Pure Virtual Functions ▪ Through inheritance we can derive new classes without designing them from scratch. ▪ There are many scenarios for which a class is desired to be served as a base class for a number of derived classes; however, the base class may contain certain functions that may not have meaningful definitions in the base class. ▪ consider the class shape; from the class shape, you can derive other classes, such as rectangle, circle, ellipse, 12 3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
  • 13. Abstract Classes and Pure Virtual Functions ▪ The definitions of the functions draw and move are specific to a particular shape, each derived class can provide an appropriate definition of these functions. ▪ the functions draw and move virtual to enforce run-time binding of these functions. ▪ This definition of the class shape requires you to write the definitions of the functions draw and move. 13 3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes body of these functions empty! Drawback: user can create objects of class shape, however there is no shape to work with
  • 14. Abstract Classes and Pure Virtual Functions ▪ convert these functions to pure virtual functions ▪ Once you make these functions pure virtual functions in the class shape, no longer need to provide the definitions of these functions for the class shape. ▪ Once a class contains one or more pure virtual, then that class is called an abstract class. 14 3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
  • 15. Abstract Classes and Pure Virtual Functions ▪ Because an abstract class is not a complete class, as it (or implementation file) does not contain the definitions of certain functions, you cannot create objects of that class. ▪ Note that in addition to the pure virtual functions, an abstract class can contain instance variables, constructors, and functions that are not pure virtual. However, the abstract class must provide the definitions of the constructor and functions that are not pure virtual. 15 3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes
  • 16. Abstract Classes and Pure Virtual Functions ▪ suppose that we derive the class rectangle from the class shape. ▪ To make rectangle a non abstract class so that we can create objects of this class, the class (or its implementation file) must provide the definitions of the pure virtual functions of its base class, which is the class shape. 16 3ème année - Génie Electrique POO: Virtual Functions & Abstract Classes