Fourth Session

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

OOP Principles

The main principles of OOP in C++

Saad Kahlaouinate
@
Summary
In today’s session we will see:
● Encapsulation and Abstraction
● Inheritance
● polymorphism
● Exception handling
Encapsulation
What is Encapsulation in OOP?

Encapsulation is the bundling of data (attributes or properties) and the


methods (functions) that operate on that data within a single unit, which is a
class in the context of C++. It is about restricting access to the internal details
of a class and exposing only what is necessary.

● Data Hiding: Encapsulation hides the implementation details of a class from


the outside world, allowing access only through well-defined interfaces
(public methods).
Abstraction
What is Abstraction in OOP?

Abstraction is the process of simplifying complex systems by modeling classes


based on essential features while ignoring unnecessary details. It focuses on
what an object does rather than how it achieves its functionality.

● Hiding Implementation Details: Abstraction hides the complex internal


details of how a class achieves its functionality, providing a clear separation
between the interface and implementation.
A virtual function (also known as virtual methods) is a
member function that is declared within a base class
and is re-defined (overridden) by a derived class.
Inheritance
Not the one from your parents/grandparents

Inheritance is a mechanism that allows you


to create a new class that is a modified
version of an existing class. The new class,
called the derived class or subclass, inherits
attributes and behaviors from the existing
class, known as the base class or superclass.
Inheritance
Constructures in multiple inheritance

Multiple Inheritance is a feature of C++


where a class can derive from several(two
or more) base classes. The constructors
of inherited classes are called in the
same order in which they are inherited.
Inheritance
Friend classes in C++

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

Overloading doesn’t work for derived


class in the C++ programming
language. There is no overload
resolution between Base and Derived
classes.
Polymorphism
Still with me ?? Read this:

Polymorphism allows objects of different types to be treated as objects of a


common type. There are two types of polymorphism in C++: compile-time
(static) polymorphism and runtime (dynamic) polymorphism.

● 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++

Exception handling is an important feature in C++ that allows you to


deal with runtime errors in a more controlled manner. In the context
of object-oriented programming (OOP) in C++, exceptions can be used
to handle errors that may occur during the execution of methods or
operations on objects.
Throwing Exceptions

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy