Introduction To OOP
Introduction To OOP
Oriented
Programming
Using
C ++
Introduction
Abdul Wahab
Lecturer
University of Science and Technology Bannu
[email protected]
1
Object Oriented Programming
It is a technique in which we visualize our programming problems in the form of objects
and their interactions as happens in real life. lives-in
Ali House
Objects
– Ali drives
– House
– Car
– Tree Car Tree
Interactions
– Ali lives in the house
– Ali drives the car
2
Example…
3
Object-Orientation - Advantages
People think in terms of objects
4
What is an Object?
An object is
5
… What is an Object?
An object has
State (attributes)
Well-defined behaviors (operations)
Unique identity
6
Example – Ali is a Tangible Object
State (attributes)
– Name
– Age
Behavior (operations)
– Walks
– Eats
Identity
– His name
7
Example – Car is a Tangible Object
State (attributes)
- Color
- Model
Behavior (operations)
- Accelerate
- Start Car
- Change Gear
Identity
- Its registration number
8
Example – Time is an Object (Conceptual)
State (attributes)
- Hours - Seconds
- Minutes
Behavior (operations)
- Set Hours
- Set Seconds
- Set Minutes
Identity
- Would have a unique ID in the model
9
Example – Date is an Object Apprehended Intellectually
State (attributes)
- Year
- Day
- Month
Behavior (operations)
- Set Year
- Set Day
- Set Month
Identity
- Would have a unique ID
10
OO Paradigm
Information Hiding
Encapsulation
Abstraction
Inheritance
Polymorphism
11
Information Hiding
12
Information Hiding Advantages
13
Encapsulation
Both the information structure and implementation details of its operations are hidden
from the outer world
14
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
15
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
16
Encapsulation – Advantages
Low complexity
Better understanding
17
Abstraction
Abstraction is a way to deal with complexity.
Principle of abstraction:
“Capture only those details about an object that are relevant to current perspective”
18
Example – Abstraction
Ali is a PhD student and teaches BS students.
Attributes
As a Student As a Teacher
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
19
Example – Abstraction
Behavior
As a Student As a Teacher
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
20
Example – Abstraction
21
Example – Abstraction
Engineer’s View
Driver’s View
22
Abstraction – Advantages
23
Classes
In an OO model, some of the objects exhibit identical characteristics (information
structure and behavior)
24
Example – Class
Ali studies mathematics
Anam studies physics
Sohail studies chemistry
25
Example – Class
Ahsan teaches mathematics
Aamir teaches computer science
Atif teaches physics
26
Inheritance
Besides inherited characteristics, a child may have its own unique characteristics
27
Inheritance in Classes
If a class B inherits from class A then it contains all the characteristics (information structure and
behavior) 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
28
Inheritance – “IS A” or
“IS A KIND OF” Relationship
29
Example – “IS A” Relationship
30
Example – “IS A” Relationship
31
Inheritance – Advantages
Reuse
Less redundancy
Increased maintainability
32
Polymorphism
Greek words:
– Poly Many
– Morpheme Shapes
33
Polymorphism
Polymorphism
Function Overridding
34
Have a Good Day!