0% found this document useful (0 votes)
6 views63 pages

OOP Lecture 01 03

The document discusses object-oriented programming (OOP) concepts, emphasizing the importance of modeling systems with interacting objects that represent real-world entities. Key principles include encapsulation, abstraction, inheritance, and polymorphism, which facilitate easier development and understanding of complex systems. It highlights how objects have state, behavior, and identity, and how they communicate through defined interfaces.

Uploaded by

sudisahmad878
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views63 pages

OOP Lecture 01 03

The document discusses object-oriented programming (OOP) concepts, emphasizing the importance of modeling systems with interacting objects that represent real-world entities. Key principles include encapsulation, abstraction, inheritance, and polymorphism, which facilitate easier development and understanding of complex systems. It highlights how objects have state, behavior, and identity, and how they communicate through defined interfaces.

Uploaded by

sudisahmad878
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

 C++ How to Program

By Deitel & Deitel

 The C++ Programming Language


By Bjarne Stroustrup

 Object-Oriented Programming in C++,


By Robert Lafore
 A technique for system modeling

 OO model consists of several interacting


objects
 A model is an abstraction of something

 Purpose is to understand the product before


developing it
 Highway maps

 Architectural models

 Mechanical models
lives-in
 Objects Ali House
 Ali drives
 House
 Car Car Tree
 Tree
 Interactions
 Ali lives in the house
 Ali drives the car
 People think in terms of objects

 OO models map to reality

 Therefore, OO models are


 easy to develop
 easy to understand
An object is

 Something tangible (Ali, Car)

 Something that can be apprehended


intellectually (Time, Date)
An object has

 State (attributes)
 Well-defined behaviour (operations)
 Unique identity
 State (attributes)
 Name
 Age
 behaviour (operations)
 Walks
 Eats
 Identity
 His name
 State (attributes)
- Color
- Model
 behaviour (operations)
- Accelerate - Start Car
- Change Gear
 Identity
- Its registration number
 State (attributes)
- Hours - Seconds
- Minutes
 behaviour (operations)
- Set Hours - Set Seconds
- Set Minutes
 Identity
- Would have a unique ID in the model
 State (attributes)
- Year - Day
- Month
 behaviour (operations)
- Set Year - Set Day
- Set Month
 Identity
- Would have a unique ID in the model
 Encapsulation/Data Hiding

 Polymorphism

 Inheritance
 Data is stored within the object

 It is hidden from the outside world

 It can only be manipulated by the object itself


 Ali’s name is stored within his brain

 We can’t access his name directly

 Rather we can ask him to tell his name


 A phone stores several phone numbers

 We can’t read the numbers directly from the


SIM card

 Rather phone-set reads this data for us


 Simplifies the model by hiding
implementation details

 It is a barrier against change propagation


 Data and behaviour are tightly coupled inside
an object

 Both the data structure and implementation


details of its operations are hidden from the
outer world
 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
 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
 Simplicity and clarity

 Low complexity

 Better understanding
 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
 Steer Wheels
 Accelerate
 Change Gear
 Apply Brakes
 Turn Lights On/Off
 Input Number
 Place Call
 Disconnect Call
 Add number to address book
 Remove number
 Update number
 Provides services offered by the object
interface

 This includes
 Data structures to hold object state
 Functionality that provides required services
 Data Structure
 Mechanical structure of gear box

 Functionality
 Mechanism to change gear
 Data Structure
 SIM card

 Functionality
 Read/write circuitry
 Means change in implementation does not
effect object interface

 This is achieved via principles of data hiding


and encapsulation
 A driver can drive a car independent of engine
type (petrol, diesel)

 Because interface does not change with the


implementation
 A driver can apply brakes independent of
brakes type (simple, disk)

 Again, reason is the same interface


 Users need not to worry about a change until
the interface is same

 Low Complexity

 Direct access to information structure of an


object can produce errors
 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
 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
 Abstraction is a way to cope with complexity.

 Principle of abstraction:

“Capture only those details about an object


that are relevant to current perspective”
Ali is a PhD student and teaches BS students

 Attributes
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Ali is a PhD student and teaches BS students

 behaviour
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
Student’s Perspective

 Attributes
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Student’s Perspective

 behaviour
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
Teacher’s Perspective

 Attributes
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Teacher’s Perspective

 behaviour
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
A cat can be viewed with different
perspectives

 Ordinary Perspective  Surgeon’s Perspective


A pet animal with A being with
 Four Legs  A Skeleton
 A Tail
 Heart
 Two Ears
 Kidney
 Sharp Teeth
 Stomach
Engineer’s View

Driver’s View
 Simplifies the model by hiding irrelevant
details

 Abstraction provides the freedom to defer


implementation decisions by avoiding
commitment to details
 In an OO model, some of the objects exhibit
identical characteristics (information
structure and behaviour)

 We say that they belong to the same class


 Ali studies mathematics
 Anam studies physics
 Sohail studies chemistry

 Each one is a Student


 We say these objects are instances of the
Student class
 Ahsan teaches mathematics
 Aamir teaches computer science
 Atif teaches physics

 Each one is a teacher


 We say these objects are instances of the
Teacher class
(Class Name)
(Class Name)
(attributes)

Suppressed
(operations)
Form

Normal Form
Circle
center Circle
radius
draw Suppressed
computeArea Form

Normal Form
Person
name Person
age
gender Suppressed
eat Form
walk

Normal Form
 A child inherits characteristics of its parents

 Besides inherited characteristics, a child may


have its own unique characteristics
 If a class B inherits from class A then it
contains all the characteristics (information
structure and behaviour) of class A
 The parent class is called base class and the
child class is called derived class
 Besides inherited characteristics, derived
class may have its own unique characteristics
Person

Student Doctor
Teacher
Shape

Line Triangle
Circle
 Each derived class is a special kind of its base
class
Person
name
age
gender
eat
walk

Student Teacher Doctor


program designation designation
studyYear salary salary
study teach checkUp
heldExam takeExam prescribe
Shape
color
coord
draw
rotate
setColor

Circle Triangle
radius Line angle
draw length draw
computeArea draw computeArea
 Reuse

 Less redundancy

 Increased maintainability
 Main purpose of inheritance is reuse
 We can easily add new classes by inheriting
from existing classes
 Select an existing class closer to the desired
functionality
 Create a new class and inherit it from the selected
class
 Add to and/or modify the inherited functionality
Shape
color
coord
draw
rotate
setColor

Circle Triangle
radius Line angle
draw length draw
computeArea draw computeArea
Person
name
age
gender
eat
walk

Student Teacher Doctor


program designation designation
studyYear salary salary
study teach checkUp
heldExam takeExam prescribe

You might also like