0% found this document useful (0 votes)
13 views

Module 5

Uploaded by

unicornpeppy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Module 5

Uploaded by

unicornpeppy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Introduction to Object-

oriented Programming
using C++
Object-Oriented Programming
• OOP stands for Object-Oriented Programming.
• Procedural programming is about writing procedures
or functions that perform operations on the data, while
object-oriented programming is about creating objects
that contain both data and functions.
Object-oriented programming
Advantages
• OOP is faster and easier to execute
• OOP provides a clear structure for the programs
• OOP helps to keep the C++ code DRY "Don't Repeat
Yourself", and makes the code easier to maintain, modify and
debug
• OOP makes it possible to create full reusable applications
with less code and shorter development time
Classes and Objects
• Classes and objects are the two main aspects of object-
oriented programming.
• a class is a template for objects.
• an object is an instance of a class
Create a Class
Create an
Object
Class Methods

• Methods are functions that belongs to the class.


• There are two ways to define functions that belongs to
a class:
• Inside class definition
• Outside class definition
Inside Class
Definition
Outside Class
Definition
Access Specifiers

• Public
• members are accessible from outside the class

• Private
• members cannot be accessed (or viewed) from outside the
class

• Protected
• members cannot be accessed from outside the class,
however, they can be accessed in inherited classes.
Encapsulation

• The meaning of Encapsulation, is to make sure that


"sensitive" data is hidden from users.
• To achieve this, you must declare class
variables/attributes as private (cannot be accessed from
outside the class).
• If you want others to read or modify the value of a
private member, you can provide
public get and set methods.
Why Encapsulation????
• It is considered good practice to declare your class attributes
as private (as often as you can). Encapsulation ensures better
control of your data, because you (or others) can change one
part of the code without affecting other parts
• Increased security of data
Inheritance

• In C++, it is possible to inherit attributes and methods from


one class to another. We group the "inheritance concept" into
two categories:
• derived class (child) - the class that inherits from another
class
• base class (parent) - the class being inherited from
Why And When To Use
"Inheritance"?
• It is useful for code reusability: reuse attributes and methods
of an existing class when you create a new class.
C++ Multilevel
Inheritance

• A class can also be


derived from one class,
which is already
derived from another
class.
Multiple Inheritance A class can also be derived from
more than one base class, using
a comma-separated list:
Polymorphism

• Polymorphism means "many forms", and it occurs when we have


many classes that are related to each other by inheritance.
• Like we specified in the previous chapter; Inheritance lets us inherit
attributes and methods from another class. Polymorphism uses those
methods to perform different tasks.
• This allows us to perform a single action in different ways.
Polymorphism

You might also like