Object-Oriented
Programming (OOP)
Lecture No. 1
Course Objective
► Objectiveof this course is to make
students familiar with the concepts of
object-oriented programming
► Concepts
will be reinforced by their
implementation in C++
Introduction
► Programmers write instructions in
various programming languages to
perform their computation tasks such
as:
► (i) Machine level Language
► (ii) Assembly level Language
► (iii) High level Language
► The high-level programming languages
are broadly categorized in to two
categories:
► (iv) Procedure oriented
programming(POP) language.
► (v) Object oriented programming(OOP)
language.
Procedure Oriented
Programming Language
► Inthe procedure oriented approach,
the problem is viewed as sequence of
things to be done such as reading ,
calculation and printing.
disadvantage of the POP
languages
► 1. Global data access
► 2. It does not model real world
problem very well
► 3. No data hiding
Object-Orientation (OO)
What is Object-Orientation?
►A technique for system modeling
► OO model consists of several
interacting objects
Object-Oriented Paradigm
► Object-Oriented Paradigm is where we
focus real life objects while
programming any solution. By focusing
real life objects we mean that over
solutions revolves around different
objects, which represent respective
objects in real life situation.
Abstraction
► “Eliminate the Irrelevant, Amplify the
Essential”
Encapsulation
► “Hiding the Unnecessary"
Inheritance
► "Modeling the Similarity"
Polymorphism
► “Same Function different behavior"
What is a Model?
►A model is an abstraction of something
► Purposeis to understand the product
before developing it
Examples – Model
► Highway maps
► Architectural models
► Mechanical models
Example – OO Model
…Example – OO Model
► Objects lives-in
Ali House
Ali
drives
House
Car
Car Tree
Tree
► Interactions
Ali lives in the house
Ali drives the car
Object-Orientation -
Advantages
► People think in terms of objects
► OO models map to reality
► Therefore, OO models are
easy to develop
easy to understand
What is an Object?
An object is
► Something tangible/ physical (Ali, Car)
► Something that can be apprehended
intellectually (Time, Date)
… What is an Object?
An object has
► State (attributes)
► Well-defined behaviour (operations)
► Unique identity
Example – Ali is a Tangible
Object
► State (attributes)
Name
Age
► behaviour (operations)
Walks
Eats
► Identity
His name
Example – Car is a Tangible
Object
► State (attributes)
- Color
- Model
► behaviour (operations)
- Accelerate - Start Car
- Change Gear
► Identity
- Its registration number
Example – Time is an Object
Apprehended Intellectually
► State (attributes)
- Hours - Seconds
- Minutes
► behaviour (operations)
- Set Hours - Set Seconds
- Set Minutes
► Identity
- Would have a unique ID in the model
Example – Date is an Object
Apprehended Intellectually
► State (attributes)
- Year - Day
- Month
► behaviour (operations)
- Set Year - Set Day
- Set Month
► Identity
- Would have a unique ID in the model
Information Hiding
► Information is stored within the object
► It is hidden from the outside world
► Itcan only be manipulated by the
object itself
Example – Information Hiding
► Ali’s name is stored within his brain
► We can’t access his name directly
► Rather we can ask him to tell his name
Example – Information Hiding
►A phone stores several phone numbers
► We can’t read the numbers directly
from the SIM card
► Ratherphone-set reads this
information for us
Information Hiding
Advantages
► Simplifies
the model by hiding
implementation details
► Itis a barrier against change
propagation
Encapsulation
► Data and behaviour are tightly coupled
inside an object
► Boththe information structure and
implementation details of its
operations are hidden from the outer
world
Example – Encapsulation
► Ali
stores his personal information and
knows how to translate it to the
desired language
► We don’t know
How the data is stored
How Ali translates this information
Example – Encapsulation
►A Phone stores phone numbers in
digital format and knows how to
convert it into human-readable
characters
► We don’t know
How the data is stored
How it is converted to human-readable
characters
Encapsulation – Advantages
► Simplicity and clarity
► Low complexity
► Better understanding
Object has an Interface
► An object encapsulates data and
behaviour
► So how objects interact with each other?
► Each object provides an interface
(operations)
► Other objects communicate through this
interface
Example – Interface of a Car
► Steer Wheels
► Accelerate
► Change Gear
► Apply Brakes
► Turn Lights On/Off
Example – Interface of a
Phone
► Input Number
► Place Call
► Disconnect Call
► Add number to address book
► Remove number
► Update number
Implementation
► Provides services offered by the object
interface
► This includes
Data structures to hold object state
Functionality that provides required
services
Example – Implementation of
Gear Box
► Data Structure
Mechanical structure of gear box
► Functionality
Mechanism to change gear
Example – Implementation of
Address Book in a Phone
► Data Structure
SIM card
► Functionality
Read/write circuitry
Separation of Interface &
Implementation
► Means change in implementation does
not effect object interface
► Thisis achieved via principles of
information hiding and encapsulation
Example – Separation of
Interface & Implementation
►A driver can drive a car independent of
engine type (petrol, diesel)
► Because interface does not change
with the implementation
Example – Separation of
Interface & Implementation
►A driver can apply brakes independent
of brakes type (simple, disk)
► Again, reason is the same interface
Advantages of Separation
► Users
need not to worry about a
change until the interface is same
► Low Complexity
► Directaccess to information structure
of an object can produce errors
Messages
► Objects communicate through
messages
► They send messages (stimuli) by
invoking appropriate operations on the
target object
► The number and kind of messages that
can be sent to an object depends upon
its interface
Examples – Messages
►A Person sends message (stimulus)
“stop” to a Car by applying brakes
►A Person sends message “place call”
to a Phone by pressing appropriate
button