You are on page 1of 57

CS 412:

Object Oriented
Programming
Fall 2016
1. Starting OOP

Friday, April 27, 2018


Salman Ahmed Rajpoot 1

Please turn OFF your Mobile Phones!


Profile
• Name: Salman Ahmed Rajpoot

• 2010-2014 BS( Information Technology)


– University of Education Lahore

• 2015-2018 MS( Software Engineering)


– International Islamic University
Islamabad

Book:
Recommended: HTTP: The Definite Guide (O’REILLY, 2002)
Procedural vs. Object-Oriented

• Procedural • Object Oriented

April 27, 2018


Alhamd Islamic University
Islamabad
Withdraw, deposit, transfer Customer, money, account 3
(OO)
Object-Orientation

Alhamd Islamic University


April 27, 2018
4

Islamabad
What is Object-Orientation?

April 27, 2018


• A technique for system modeling

• OO model consists of several interacting objects

Alhamd Islamic University


Islamabad
5
What is a Model?

April 27, 2018


• A model is an abstraction of something

• Purpose is to understand the product before developing it

Alhamd Islamic University


Islamabad
6
Examples – Model

April 27, 2018


• Highway maps

• Architectural models

Alhamd Islamic University


Islamabad
• Mechanical models

7
Example – OO Model

Alhamd Islamic University


April 27, 2018
8

Islamabad
…Example – OO Model
• Objects lives-in
Ali House

April 27, 2018


• Ali
• House drives
• Car

Alhamd Islamic University


Islamabad
• Tree
• Interactions Car Tree
• Ali lives in the house
• Ali drives the car

9
Object-Orientation -
Advantages
• People think in terms of objects

April 27, 2018


• OO models map to reality

Alhamd Islamic University


Islamabad
• Therefore, OO models are
• easy to develop
• easy to understand

10
What is an Object?
An object is

April 27, 2018


• Something tangible (Ali, Car)

Alhamd Islamic University


Islamabad
• Something that can be apprehended intellectually (Time,
Date)

11
… What is an Object?
An object has

April 27, 2018


• State (attributes)
• Well-defined behaviour (operations)

Alhamd Islamic University


Islamabad
• Unique identity

12
Example – Ali is a Tangible Object
• State (attributes)

April 27, 2018


• Name
• Age
• behaviour (operations)

Alhamd Islamic University


Islamabad
• Walks
• Eats
• Identity
• His name

13
Example – Car is a Tangible Object
• State (attributes)

April 27, 2018


- Color
- Model
• behaviour (operations)

Alhamd Islamic University


Islamabad
- Start Car
- Accelerate
- Change Gear
• Identity
- Its registration number

14
Example – Time is an Object
Apprehended Intellectually
• State (attributes)

April 27, 2018


- Hours - Seconds
- Minutes
• behaviour (operations)

Alhamd Islamic University


Islamabad
- Set Hours - Set Seconds
- Set Minutes
• Identity
- Would have a unique ID in the model

15
Example – Date is an Object
Apprehended Intellectually
• State (attributes)

April 27, 2018


- Year - Day
- Month
• behaviour (operations)

Alhamd Islamic University


Islamabad
- Set Year - Set Day
- Set Month
• Identity
- Would have a unique ID in the model

16
Definition
• What Is an Object?

April 27, 2018


• An object is a software bundle of related
variables and methods. Software objects are
often used to model real-world objects you find

Alhamd Islamic University


Islamabad
in everyday life.
• Objects are key to understanding object-oriented
technology. You can look around you now and
see many examples of real-world objects: dog,
desk, television set, bicycle.
17
Real-world objects share two
characteristics
• They all have state and behavior
• dogs have state (name, color, breed) and behavior (barking,

April 27, 2018


fetching, and wagging tail).
• Bicycles have state (current gear, current pedal cadence, two
wheels, number of gears) and behavior (braking, accelerating,

Alhamd Islamic University


Islamabad
slowing down, changing gears).

18
Software objects are modeled after real-
world objects
• A software object maintains its state in one or more variables .

April 27, 2018


• A software object implements its behavior with methods . A
method is a function (subroutine) associated with an object.

Alhamd Islamic University


Islamabad
19
Can represent real-world objects by
using software objects.
• You might want to represent real-world dogs as

April 27, 2018


software objects in an animation program or a
real-world bicycle as a software object in the
program that controls an electronic exercise

Alhamd Islamic University


Islamabad
bike.
• You can also use software objects to model
abstract concepts. For example, an event is a
common object used in GUI window systems to
represent the action of a user pressing a mouse
button or a key on the keyboard. 20
Moving to new thinking …
• C is a procedural language. A procedure is a list of instructions.

April 27, 2018


• Very small programs (tens of lines) need no organization.
• As they get large (hundreds of lines), they are divided into functions.
• Functions are still lists of instructions.

Alhamd Islamic University


Islamabad
• Dividing a program into functions gives a structure to the program
(hence structured programming).

21
Moving to new thinking …
• C programming cannot do the following well:

April 27, 2018


• Functions have unrestricted access to global data.
• Data and Functions are unrelated; they do not form a logical group or
show any form of binding.

Alhamd Islamic University


Islamabad
• Cannot model ‘real world’.
• In real world, we deal with objects like cars, people etc.
• Are such objects like ‘data’? Which data type describes a car, for
example?
• Are such objects like ‘functions’? What will a function car() do?

22
Moving to new thinking …
• A real world object, e.g. car:
• Is its description purely the ability to have a ‘data type’ to represent its

April 27, 2018


specifications or ‘attributes’ only? For example what attributes describe
a car?
• So by having all these attributes ‘only’ do you know all about a car?
Would you buy a car by merely looking at these attributes and their

Alhamd Islamic University


Islamabad
values?
• No! What else do you wish to have?

23
Moving to new thinking …
• A real world object, e.g. car:
• Test drive! Right?

April 27, 2018


• What would you know through a test drive: how it ‘behaves’ in
response to various stimulus!
• How can it negotiate a speed braker, a speedy turn, full braking etc.
• Effectively, you wish to know how it ‘functions’.

Alhamd Islamic University


Islamabad
• Behaviour is like a function.
• So neither data nor functions, by themselves, model real-world
objects effectively.

24
Object Oriented Approach …
• Neither data nor functions, by themselves, model real-world
objects effectively.

April 27, 2018


• OO languages (like C++) combine both ‘data’ and ‘functions that
operate on that data’ into a single unit, called ‘object’. Hence
‘encapsulating’ an entity.

Alhamd Islamic University


Islamabad
• The objects functions are called member functions.
• Typically, data can be accessed through functions only. So data gets
‘hidden’.
• Data hiding ensures the data is not accidentally altered.
• Reference analogy of a growing company; 2 people vs 100
employee company.
• Objects communicate with each other by calling each other’s
member functions. 25
Object Oriented Approach …
• OOP is all about ‘organization’ and not about program operation.
Most individual statements are similar to C statements. Member

April 27, 2018


functions may be very similar to C procedural functions.
• In C you used to think about functions.
• In C++ you should think about objects.

Alhamd Islamic University


Islamabad
• Often objects in C++ are real-world analogies.
• In C++ objects are ‘instances’ of ‘classes’. E.g. Honda City is an
instance of class car!
• Class serves as a cookie cutter and an object a cookie.

26
Abstraction

April 27, 2018


• Abstraction is a way to cope with complexity.

• Principle of abstraction:

Alhamd Islamic University


Islamabad
“Capture only those details about an object that are relevant to
current perspective”

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

April 27, 2018


students

• Attributes

Alhamd Islamic University


Islamabad
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
28
Example – Abstraction
Ali is a PhD student and teaches BS

April 27, 2018


students
• behaviour

Alhamd Islamic University


Islamabad
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk

29
Example – Abstraction
Student’s Perspective

April 27, 2018


Attributes

Alhamd Islamic University


Islamabad
• - Name - Employee ID
• - Student Roll No - Designation
• - Year of Study - Salary
• - CGPA - Age
30
Example – Abstraction
Student’s Perspective

April 27, 2018


• behaviour

Alhamd Islamic University


Islamabad
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk

31
Example – Abstraction
Teacher’s Perspective

April 27, 2018


• Attributes

Alhamd Islamic University


Islamabad
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
32
Example – Abstraction
Teacher’s Perspective

April 27, 2018


• behaviour

Alhamd Islamic University


Islamabad
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk

33
Example – Abstraction
A cat can be viewed with different

April 27, 2018


perspectives

• Ordinary Perspective • Surgeon’s Perspective

Alhamd Islamic University


Islamabad
A pet animal with A being with
• Four Legs • A Skeleton
• A Tail • Heart
• Two Ears • Kidney
• Sharp Teeth • Stomach 34
Example – Abstraction

April 27, 2018


Alhamd Islamic University
Islamabad
Engineer’s View

Driver’s View

35
Abstraction – Advantages

April 27, 2018


• Simplifies the model by hiding irrelevant details

• Abstraction provides the freedom to defer implementation

Alhamd Islamic University


Islamabad
decisions by avoiding commitment to details

36
Classes

April 27, 2018


• In an OO model, some of the objects exhibit identical
characteristics (information structure and behaviour)

Alhamd Islamic University


Islamabad
• We say that they belong to the same class

37
Example – Class
• Ali studies mathematics

April 27, 2018


• Anam studies physics
• Sohail studies chemistry

Alhamd Islamic University


Islamabad
• Each one is a Student
• We say these objects are instances of the Student class

38
Example – Class
• Ahsan teaches mathematics

April 27, 2018


• Aamir teaches computer science
• Atif teaches physics

Alhamd Islamic University


Islamabad
• Each one is a teacher
• We say these objects are instances of the Teacher class

39
Graphical Representation of Classes

April 27, 2018


(Class Name)
(Class Name)

Alhamd Islamic University


Islamabad
(attributes)

Suppressed
(operations)
Form

Normal Form 40
Example – Graphical Representation
of Classes

April 27, 2018


Circle
center Circle

Alhamd Islamic University


Islamabad
radius
draw Suppressed
computeArea Form

Normal Form
41
Example – Graphical Representation
of Classes

April 27, 2018


Person
name Person

Alhamd Islamic University


Islamabad
age
gender Suppressed
eat Form
walk

Normal Form
42
Inheritance

April 27, 2018


• A child inherits characteristics of its parents

• Besides inherited characteristics, a child may have its own

Alhamd Islamic University


Islamabad
unique characteristics

43
Inheritance in Classes
• If a class B inherits from class A then it contains all the

April 27, 2018


characteristics (information structure and behaviour) of class
A
• The parent class is called base class and the child class is called
derived class

Alhamd Islamic University


Islamabad
• Besides inherited characteristics, derived class may have its
own unique characteristics

44
Example – Inheritance

April 27, 2018


Person

Alhamd Islamic University


Islamabad
Student Doctor
Teacher

45
Line
Shape

Circle
Example – Inheritance

Triangle

Alhamd Islamic University


April 27, 2018
46

Islamabad
Inheritance – “IS A” or
“IS A KIND OF” Relationship

April 27, 2018


• Each derived class is a special kind of its base class

Alhamd Islamic University


Islamabad
47
Example – “IS A” Relationship
Person
name

April 27, 2018


age
gender
eat

Alhamd Islamic University


Islamabad
walk

Student Teacher Doctor


program designation designation
studyYear salary salary
study teach checkUp 48

heldExam takeExam prescribe


Example – “IS A KIND of”
Relationship
Shape
color

April 27, 2018


coord
draw
rotate

Alhamd Islamic University


Islamabad
setColor

Circle Triangle
radius Line angle
49
draw length draw
computeArea draw computeArea
Inheritance – Advantages

April 27, 2018


• Reuse

• Less redundancy

Alhamd Islamic University


Islamabad
• Increased maintainability

50
Reuse with Inheritance
• Main purpose of inheritance is reuse

April 27, 2018


• We can easily add new classes by inheriting from existing
classes
• Select an existing class closer to the desired functionality

Alhamd Islamic University


Islamabad
• Create a new class and inherit it from the selected class
• Add to and/or modify the inherited functionality

51
Example Reuse
Shape
color

April 27, 2018


coord
draw
rotate

Alhamd Islamic University


Islamabad
setColor

Circle Triangle
radius Line angle
52
draw length draw
computeArea draw computeArea
Example Reuse
Person
name

April 27, 2018


age
gender
eat

Alhamd Islamic University


Islamabad
walk

Student Teacher Doctor


program designation designation
studyYear salary salary
study teach checkUp 53

heldExam takeExam prescribe


Example Reuse
Person
name

April 27, 2018


age
gender
eat

Alhamd Islamic University


Islamabad
walk

Student Teacher Doctor


program designation designation
studyYear salary salary
study teach checkUp 54

heldExam takeExam prescribe


April 27, 2018
References
• “Object Oriented Programming in C++”, by “Robert Lafore”,
published by Sams Publishing (The Waite Group). 4th ed. available

Alhamd Islamic University Islamabad


in soft form.
• “Object Oriented Programming Using C++” by “Joyce Farrell” ,
published by Course Technology, Cengage Learning. 4th ed. available
in soft form
• National University of Computer & Emerging Sciences
[www.nu.edu.pk]
• Virtual University of Pakistan [ocw.vu.edu.pk]
• Open Courseware Consortium
[http://www.ocwconsortium.org/en/courses]
55
Study Assignment
• I hope you did go through chapter 1 of both the books.

April 27, 2018


• How was the experience?
• What did you learn?

Alhamd Islamic University


Islamabad
56
Thanks

Alhamd Islamic University


April 27, 2018
57

Islamabad

You might also like