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.)
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.
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
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