SlideShare uma empresa Scribd logo
1 de 16
Baixar para ler offline
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

Mais conteúdo relacionado

Semelhante a 6_VirtualFunctions_AbstractClasses.pdf

Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)Oopsecondgrouppresentation 180726073512-converted (1)
Oopsecondgrouppresentation 180726073512-converted (1)
Hassan Hashmi
 
Sample Lab Manual Word Edited aaaaaaaaaaaaaaa
Sample Lab Manual Word Edited aaaaaaaaaaaaaaaSample Lab Manual Word Edited aaaaaaaaaaaaaaa
Sample Lab Manual Word Edited aaaaaaaaaaaaaaa
XEON14
 

Semelhante a 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++
 
C++ Training
C++ TrainingC++ Training
C++ Training
 
09slide.ppt
09slide.ppt09slide.ppt
09slide.ppt
 
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
 
C++ interview questions
C++ interview questionsC++ interview questions
C++ interview questions
 
Sample Lab Manual Word Edited aaaaaaaaaaaaaaa
Sample Lab Manual Word Edited aaaaaaaaaaaaaaaSample Lab Manual Word Edited aaaaaaaaaaaaaaa
Sample Lab Manual Word Edited aaaaaaaaaaaaaaa
 

Último

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Último (20)

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

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