2. Study Assignment
• I hope you did go through chapter 1 of both the books.
• How was the experience?
• What did you learn?
July
2,
2023
2
COMSATS
Institute
of
Information
Technology
3. Procedural vs. Object-Oriented
• Procedural
Withdraw, deposit, transfer
• Object Oriented
Customer, money, account
July
2,
2023
3
COMSATS
Institute
of
Information
Technology
5. What is Object-Orientation?
• A technique for system modeling
• OO model consists of several interacting objects
July
2,
2023
COMSATS
Institute
of
Information
Technology
5
6. What is a Model?
• A model is an abstraction of something
• Purpose is to understand the product before developing it
July
2,
2023
COMSATS
Institute
of
Information
Technology
6
7. Examples – Model
• Highway maps
• Architectural models
• Mechanical models
July
2,
2023
COMSATS
Institute
of
Information
Technology
7
8. Example – OO Model
July
2,
2023
COMSATS
Institute
of
Information
Technology
8
9. …Example – OO Model
• Objects
• Ali
• House
• Car
• Tree
• Interactions
• Ali lives in the house
• Ali drives the car
Ali
Car
House
Tree
lives-in
drives
July
2,
2023
COMSATS
Institute
of
Information
Technology
9
10. Object-Orientation -
Advantages
• People think in terms of objects
• OO models map to reality
• Therefore, OO models are
• easy to develop
• easy to understand
July
2,
2023
COMSATS
Institute
of
Information
Technology
10
11. What is an Object?
An object is
• Something tangible (Ali, Car)
• Something that can be apprehended intellectually (Time,
Date)
***Apprehended intellectually means taken mentally or
logically***
July
2,
2023
COMSATS
Institute
of
Information
Technology
11
12. … What is an Object?
An object has
• State (attributes)
• Well-defined behaviour (operations)
• Unique identity
July
2,
2023
COMSATS
Institute
of
Information
Technology
12
13. Example – Ali is a Tangible Object
• State (attributes)
• Name
• Age
• behavior (operations)
• Walks
• Eats
• Identity
• His name
July
2,
2023
COMSATS
Institute
of
Information
Technology
13
14. Example – Car is a Tangible Object
• State (attributes)
- Color
- Model
• behavior (operations)
- Start Car
- Accelerate
- Change Gear
• Identity
- Its registration number
July
2,
2023
COMSATS
Institute
of
Information
Technology
14
15. 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
July
2,
2023
COMSATS
Institute
of
Information
Technology
15
16. 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
July
2,
2023
COMSATS
Institute
of
Information
Technology
16
17. Definition
• What Is an Object?
• An object is a software bundle of related
variables and methods (functions). Software
objects are often used to model real-world
objects you find 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.
July
2,
2023
17
COMSATS
Institute
of
Information
Technology
18. Real-world objects share two
characteristics
• They all have state and behavior
• dogs have state (name, color, breed, hungry) and behavior
(barking, fetching, and wagging tail).
• Bicycles have state (current gear, current pedal cadence, two
wheels, number of gears) and behavior (braking, accelerating,
slowing down, changing gears).
July
2,
2023
18
COMSATS
Institute
of
Information
Technology
19. Software objects are modeled after real-
world objects
• A software object maintains its state in one or more variables .
• A software object implements its behavior with methods . A
method is a function (subroutine) associated with an object.
July
2,
2023
19
COMSATS
Institute
of
Information
Technology
20. Can represent real-world objects by
using software objects.
• You might want to represent real-world dogs as
software objects in an animation program or a
real-world bicycle as a software object in the
program that controls an electronic exercise
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.
July
2,
2023
20
COMSATS
Institute
of
Information
Technology
21. Moving to new thinking …
• C is a procedural language. A procedure is a list of instructions.
• 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.
• Dividing a program into functions gives a structure to the program
(hence structured programming).
July
2,
2023
21
COMSATS
Institute
of
Information
Technology
22. Moving to new thinking …
• C programming cannot do the following well:
• 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.
• 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?
July
2,
2023
22
COMSATS
Institute
of
Information
Technology
23. 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
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
values?
• No! What else do you wish to have?
July
2,
2023
23
COMSATS
Institute
of
Information
Technology
24. Moving to new thinking …
• A real world object, e.g. car:
• Test drive! Right?
• What would you know through a test drive: how it ‘behaves’ in
response to various stimulus!
• How can it negotiate a speed brake, a speedy turn, full braking etc.
• Effectively, you wish to know how it ‘functions’.
• Behavior is like a function.
• So neither data nor functions, by themselves, model real-world
objects effectively.
July
2,
2023
24
COMSATS
Institute
of
Information
Technology
25. Object Oriented Approach …
• Neither data nor functions, by themselves, model real-world
objects effectively.
• OO languages (like C++) combine both ‘data’ and ‘functions that
operate on that data’ into a single unit, called ‘object’. Hence
‘encapsulating’ an entity.
• 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.
July
2,
2023
25
COMSATS
Institute
of
Information
Technology
26. Object Oriented Approach …
• OOP is all about ‘organization’ and not about program operation.
Most individual statements are similar to C statements. Member
functions may be very similar to C procedural functions.
• In C you used to think about functions.
• In C++ you should think about objects.
• Often objects in C++ are real-world analogies.(parallel)
• 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.
July
2,
2023
26
COMSATS
Institute
of
Information
Technology
27. 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”
July
2,
2023
COMSATS
Institute
of
Information
Technology
27
28. Example – Abstraction
• Attributes
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Ali is a PhD student and teaches BS
students
July
2,
2023
COMSATS
Institute
of
Information
Technology
28
29. Example – Abstraction
Ali is a PhD student and teaches BS
students
• Behavior
- Study - Develop Exam
- Give Exam - Take Exam
- Play Sports - Eat
- Deliver Lecture - Walk
July
2,
2023
COMSATS
Institute
of
Information
Technology
29
30. Example – Abstraction
Attributes
• - Name - Employee ID
• - Student Roll No - Designation
• - Year of Study - Salary
• - CGPA - Age
Student’s Perspective
July
2,
2023
COMSATS
Institute
of
Information
Technology
30
31. Example – Abstraction
Student’s Perspective
• behavior
- Study - Develop Exam
- Give Exam - Take Exam
- Play Sports - Eat
- Deliver Lecture - Walk
July
2,
2023
COMSATS
Institute
of
Information
Technology
31
32. Example – Abstraction
• Attributes
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Teacher’s Perspective
July
2,
2023
COMSATS
Institute
of
Information
Technology
32
33. Example – Abstraction
Teacher’s Perspective
• behaviour
- Study - Develop Exam
- Give Exam - Take Exam
- Play Sports - Eat
- Deliver Lecture - Walk
July
2,
2023
COMSATS
Institute
of
Information
Technology
33
34. Example – Abstraction
• Ordinary Perspective
A pet animal with
• Four Legs
• A Tail
• Two Ears
• Sharp Teeth
• Surgeon’s Perspective
A being with
• A Skeleton
• Heart
• Kidney
• Stomach
A cat can be viewed with different
perspectives
July
2,
2023
COMSATS
Institute
of
Information
Technology
34
36. Abstraction – Advantages
• Simplifies the model by hiding irrelevant details
• Abstraction provides the freedom to defer implementation
decisions by avoiding commitment to details
July
2,
2023
COMSATS
Institute
of
Information
Technology
36
37. Classes
• In an OO model, some of the objects exhibit identical
characteristics (information structure and behavior)
• We say that they belong to the same class
July
2,
2023
COMSATS
Institute
of
Information
Technology
37
38. 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
July
2,
2023
COMSATS
Institute
of
Information
Technology
38
39. 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
July
2,
2023
COMSATS
Institute
of
Information
Technology
39
40. Graphical Representation of Classes
(Class Name)
(attributes)
(operations)
(Class Name)
Normal Form
Suppressed
Form
July
2,
2023
COMSATS
Institute
of
Information
Technology
40
41. Example – Graphical Representation
of Classes
Circle
center
radius
draw
computeArea
Normal Form
Suppressed
Form
Circle
July
2,
2023
COMSATS
Institute
of
Information
Technology
41
42. Example – Graphical Representation
of Classes
Person
name
age
gender
eat
walk
Normal Form
Suppressed
Form
Person
July
2,
2023
COMSATS
Institute
of
Information
Technology
42
43. Inheritance
• A child inherits characteristics of its parents
• Besides inherited characteristics, a child may have its own
unique characteristics
July
2,
2023
COMSATS
Institute
of
Information
Technology
43
44. Inheritance in Classes
• 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
July
2,
2023
COMSATS
Institute
of
Information
Technology
44
47. Inheritance – “IS A” or
“IS A KIND OF” Relationship
• Each derived class is a special kind of its base class
July
2,
2023
COMSATS
Institute
of
Information
Technology
47
48. Example – “IS A” Relationship
Person
name
age
gender
eat
walk
Teacher
designation
salary
teach
takeExam
Student
program
studyYear
study
heldExam
Doctor
designation
salary
checkUp
prescribe
July
2,
2023
COMSATS
Institute
of
Information
Technology
48
49. Example – “IS A KIND”
Relationship
Shape
color
cord
draw
rotate
setColor
Circle
radius
draw
computeArea
Line
length
draw
Triangle
angle
draw
computeArea
July
2,
2023
COMSATS
Institute
of
Information
Technology
49
50. Inheritance – Advantages
• Reuse
• Less redundancy
• Increased maintainability
July
2,
2023
COMSATS
Institute
of
Information
Technology
50
51. Reuse with Inheritance
• 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
July
2,
2023
COMSATS
Institute
of
Information
Technology
51
55. References
• “Object Oriented Programming in C++”, by “Robert Lafore”,
published by Sams Publishing (The Waite Group). 4th ed. available
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
July
2,
2023
COMSATS
Institute
of
Information
Technology