0% found this document useful (0 votes)
8 views35 pages

Introduction To OOP

The document provides an introduction to Object-Oriented Programming (OOP) using C++, explaining key concepts such as objects, interactions, and the advantages of OOP. It covers fundamental principles including information hiding, encapsulation, abstraction, inheritance, and polymorphism, with examples illustrating each concept. The document emphasizes how OOP models align with real-life scenarios, making programming easier to develop and understand.

Uploaded by

Abdul Wahab
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)
8 views35 pages

Introduction To OOP

The document provides an introduction to Object-Oriented Programming (OOP) using C++, explaining key concepts such as objects, interactions, and the advantages of OOP. It covers fundamental principles including information hiding, encapsulation, abstraction, inheritance, and polymorphism, with examples illustrating each concept. The document emphasizes how OOP models align with real-life scenarios, making programming easier to develop and understand.

Uploaded by

Abdul Wahab
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/ 35

Object

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

 OO models map to reality

 Therefore, OO models are


– Easy to develop
– Easy to understand

4
What is an Object?
An object is

 Something tangible (Ali, Car)

 Something that can be understood intellectually (Time, Date)

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

 Information is stored within the object

 It is hidden from the outside world

 It can only be manipulated by the object itself

12
Information Hiding Advantages

 Simplifies the model by hiding implementation details

 It is a barrier against change propagation

13
Encapsulation

 Data and behavior are tightly coupled inside an object

 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

 Simplicity and clarity

 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.

 Ali has two perspectives


– Student
– Teacher

 Attributes
As a Student As a Teacher
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age

19
Example – Abstraction

 Ali’s behavior can be summed up as follows:

 Behavior
As a Student As a Teacher
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk

20
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

21
Example – Abstraction

Engineer’s View

Driver’s View

22
Abstraction – Advantages

 Simplifies the model by hiding irrelevant details

23
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

24
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

25
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

26
Inheritance

 A child inherits characteristics of its parents

 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

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

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

 Any thing having many shapes


 In computer science, polymorphism means that a single function having more than one
forms.

33
Polymorphism

Polymorphism

Compile-Time Polymorphism Run-Time polymorphism

Function Overloading Operator Overloading

Function Overridding

34
Have a Good Day!

You might also like