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

Lecture 01

Uploaded by

garamm800
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)
4 views

Lecture 01

Uploaded by

garamm800
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/ 55

CS1201

Object-Oriented Programming
(OOP)
Lecture No. 1

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 1


Course Objective

► Objective of this course is to make students


familiar with the concepts of object-oriented
programming

► Concepts will be reinforced by their


implementation in C++

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 2


Course Contents
► Object-Orientation
► Objects and Classes
► Overloading
► Inheritance
► Polymorphism
► Generic Programming
► Exception Handling
► Introduction to Design Patterns

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 3


Books
► C++ How to Program
By Deitel & Deitel

► The C++ Programming Language


By Bjarne Stroustrup

► Object-Oriented Software Engineering


By Jacobson, Christerson, Jonsson, Overgaard
Engr. Nabeel Ali © CS1201 - Object Oriented Programming 4
Grading Policy
► Assignments 10 Marks Average
► Quizes 10 Marks Average
► Mid-Term 50 Marks
► Final 80 Marks
► Lab
 Lab Assignments – 20 Marks Average
 Project – 30 Marks

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 5


Object-Orientation (OO)

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 6


What is Object-Orientation?

►A technique for system modeling

► OO model consists of several interacting


objects

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 7


What is a Model?

►A model is an abstraction of something

► Purpose is to understand the product before


developing it

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 8


Examples – Model

► Highway maps

► Architectural models

► Mechanical models

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 9


Example – OO Model

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 10


…Example – OO Model
lives-in
► Objects Ali House
 Ali
drives
 House
 Car
Car Tree
 Tree
► Interactions
 Ali lives in the house
 Ali drives the car
Engr. Nabeel Ali © CS1201 - Object Oriented Programming 11
Object-Orientation - Advantages
► People think in terms of objects

► OO models map to reality

► Therefore, OO models are


 easy to develop
 easy to understand

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 12


What is an Object?
An object is

► Something tangible (Ali, Car)

► Something that can be apprehended


intellectually (Time, Date)

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 13


… What is an Object?
An object has

► State (attributes)
► Well-defined behaviour (operations)
► Unique identity

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 14


Example – Ali is a Tangible Object

► State (attributes)
 Name
 Age
► behaviour (operations)
 Walks
 Eats
► Identity
 His name
Engr. Nabeel Ali © CS1201 - Object Oriented Programming 15
Example – Car is a Tangible Object

► State (attributes)
- Color
- Model
► behaviour (operations)
- Accelerate - Start Car
- Change Gear
► Identity
- Its registration number
Engr. Nabeel Ali © CS1201 - Object Oriented Programming 16
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

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 17


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
Engr. Nabeel Ali © CS1201 - Object Oriented Programming 18
Information Hiding

► Information is stored within the object

► It is hidden from the outside world

► It can only be manipulated by the object


itself

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 19


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

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 20


Example – Information Hiding

►A phone stores several phone numbers

► We can’t read the numbers directly from the


SIM card

► Rather phone-set reads this information for


us
Engr. Nabeel Ali © CS1201 - Object Oriented Programming 21
Information Hiding
Advantages

► Simplifies the model by hiding


implementation details

► It is a barrier against change propagation

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 22


Encapsulation

► Data and behaviour are tightly coupled


inside an object

► Both the information structure and


implementation details of its operations are
hidden from the outer world

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 23


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
Engr. Nabeel Ali © CS1201 - Object Oriented Programming 24
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

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 25


Encapsulation – Advantages

► Simplicity and clarity

► Low complexity

► Better understanding

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 26


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

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 27


Example – Interface of a Car
► Steer Wheels
► Accelerate
► Change Gear
► Apply Brakes
► Turn Lights On/Off

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 28


Example – Interface of a Phone
► Input Number
► Place Call
► Disconnect Call
► Add number to address book
► Remove number
► Update number

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 29


Implementation
► Provides services offered by the object
interface

► This includes
 Data structures to hold object state
 Functionality that provides required services

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 30


Example – Implementation of
Gear Box

► Data Structure
 Mechanical structure of gear box

► Functionality
 Mechanism to change gear

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 31


Example – Implementation of
Address Book in a Phone

► Data Structure
 SIM card

► Functionality
 Read/write circuitry

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 32


Separation of Interface &
Implementation

► Means change in implementation does not


effect object interface

► This is achieved via principles of information


hiding and encapsulation

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 33


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

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 34


Example – Separation of
Interface & Implementation

►A driver can apply brakes independent of


brakes type (simple, disk)

► Again, reason is the same interface

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 35


Advantages of Separation
► 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

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 36


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

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 37


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

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 38


Abstraction

► Abstraction is a way to cope with


complexity.

► Principle of abstraction:

“Capture only those details about an object


that are relevant to current perspective”
Engr. Nabeel Ali © CS1201 - Object Oriented Programming 39
Example – Abstraction
Ali is a PhD student and teaches BS students

► Attributes
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 40


Example – Abstraction
Ali is a PhD student and teaches BS students

► behaviour
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 41


Example – Abstraction
Student’s Perspective

► Attributes
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 42


Example – Abstraction
Student’s Perspective

► behaviour
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 43


Example – Abstraction
Teacher’s Perspective

► Attributes
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 44


Example – Abstraction
Teacher’s Perspective

► behaviour
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 45


Example – Abstraction
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
Engr. Nabeel Ali © CS1201 - Object Oriented Programming 46
Example – Abstraction

Engineer’s View

Driver’s View

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 47


Abstraction – Advantages

► Simplifies the model by hiding irrelevant


details

► Abstraction provides the freedom to defer


implementation decisions by avoiding
commitment to details

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 48


Classes

► In an OO model, some of the objects exhibit


identical characteristics (information
structure and behaviour)

► We say that they belong to the same class

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 49


Example – 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

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 50


Example – 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

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 51


Graphical Representation of Classes

(Class Name)
(Class Name)
(attributes)

Suppressed
(operations)
Form

Normal Form
Engr. Nabeel Ali © CS1201 - Object Oriented Programming 52
Example – Graphical Representation
of Classes

Circle
center Circle
radius
draw Suppressed
computeArea Form

Normal Form
Engr. Nabeel Ali © CS1201 - Object Oriented Programming 53
Example – Graphical Representation
of Classes

Person
name Person
age
gender Suppressed
eat Form
walk

Normal Form
Engr. Nabeel Ali © CS1201 - Object Oriented Programming 54
The END

Engr. Nabeel Ali © CS1201 - Object Oriented Programming 55

You might also like