Fourth Session
Fourth Session
Fourth Session
Saad Kahlaouinate
@
Summary
In today’s session we will see:
● Encapsulation and Abstraction
● Inheritance
● polymorphism
● Exception handling
Encapsulation
What is Encapsulation in OOP?
In C++, a friend class is a class that is granted access to the private and
protected members of another class. This is achieved by declaring the friend
class in the class whose members need to be accessed. The friend
relationship is not mutual; if class A is a friend of class B, it doesn't imply that
class B is a friend of class A.
Inheritance
Overloading in inheritance
● Compile-time Polymorphism
● Runtime Polymorphism
Polymorphism
Types of polymorphism
Compile-time Polymorphism:
● Compile-time polymorphism is achieved through function overloading and
operator overloading.
Runtime Polymorphism:
● Runtime polymorphism is achieved through virtual functions and pointers or
references to base class objects.
Compile-time Polymorphism
Runtime Polymorphism
Exception Handling
What we must use exceptions in C++
• To signal that an error has occurred, you can use the throw
keyword to throw an exception.
• You can throw objects of any type, including built-in types,
pointers, or instances of custom classes.
Custom Exception Classes
• You can define your own exception classes by
deriving from the “std::exception” class or any other
appropriate base class.