You are on page 1of 38

CS1004- Object-Oriented

Programming (OOP)
Week 02
Recap
What are the main principles of
Object-Oriented Programming?
The four principles of object-oriented programming are
1. encapsulation,
2. abstraction,
3. inheritance, and
4. polymorphism.
Information Hiding

“Showing only those details to the outside world which are necessary for the outside world
and hiding all other details from the outside world.”
Or
Information is stored within the object
It is hidden from the outside world
It can only be manipulated by the object itself
Real Life Examples of Information Hiding

Example 1:
Ali’s name is stored within his brain
We can’t access his name directly
Rather we can ask him to tell his name
Example 2:
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
Example 3:
Email Server
Information Hiding Advantages

Simplifies the model by hiding implementation details


Prevents accidental access
Prevents illegal access or manipulation

We can achieve information hiding using Encapsulation and Abstraction, so we see these two
concepts in detail now,
Encapsulation

“we have enclosed all the characteristics of an object in the object itself”.
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
Examples of Encapsulation
Consider the same example of object Ali of previous lecture we described it as follows,
Example:

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
Encapsulation-Example
You can feed the cat. But you can’t directly change how hungry the cat is.
Advantages of Encapsulation

The following are the main advantages of Encapsulation,


Simplicity and clarity
Low complexity
Better understanding
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”
Example – Abstraction

“Ali is a PhD student and teaches BS students”


Here object Ali has two perspectives one is his student perspective and second is his teacher
perspective.
We can sum up Ali’s attributes as follows,
Name
Age
Student Roll No
Year of Study
CGPA
Employee ID
Designation
Salary
Example – Abstraction

“Ali is a PhD student and teaches BS students”


Here object Ali has two perspectives one is his student perspective and second is his teacher
perspective.
Similarly we can sum up Ali’s behavior as follows,
Study
DevelopExam
GiveExam
TakeExam
PlaySports
Eat
DeliverLecture
Walk
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
Example – Abstraction

Driver’s View Engineer’s View


Example – Abstraction
Cell phones are complex. But using them is simple.
Example – Abstraction

Implementation details are hidden from the outside world


Information is stored within the object
It can only be changed by the object itself

All logic is hidden inside the handset


You use HOME button to get to the home screen without knowing “how” it was done
Abstraction – Advantages

Simplifies the model by hiding irrelevant details


Abstraction provides the freedom to defer implementation decisions by avoiding
commitment to details
Inheritance

A child inherits characteristics of its parents


Besides inherited characteristics, a child may have its own unique characteristics
Example
Inheritance – Example
A private teacher is a type of teacher. And any teacher is a type of Person.
Inheritance – Advantages

Reuse
Less redundancy
Increased maintainability
Polymorphism:

In general, polymorphism refers to existence of different forms of a single entity


For example, both Diamond and Coal are different forms of Carbon
Polymorphism – Example
Triangle, Circle, and Rectangle now can be used in the same collection
Polymorphism - Advantage

Messages can be interpreted in different ways depending upon the receiver class.
New classes can be added without changing the existing model
Question: So what is an object oriented program
exactly?

Answer: It is a program that contains objects, of course, which have certain properties and
have methods linked to them. These methods are used to obtain a programming result.
Week 2 | Lecture 1

Introduction to Object in real world(Already discussed in Week 2| Lecture 2).


Introduction to classes and objects
Classes

In an OO model, some of the objects exhibit identical characteristics (attribute and


behaviour)
We say that they belong to the same class
Class is a tool to realize objects
Class is a tool for defining a new type
Example:
Lion is an object
Student is an object
Both has some attributes and some behaviors
Example - Class

Ali studies OOP


Anam studies PF
Fahad studies DS

Each one is a Student


We say these objects are instances of the Student class
Example - Class

Prof Atif teaches Algorithm


Prof Zulfiqar teaches database system
Prof Suliman teaches Block chain

Each one is a teacher


We say these objects are instances of the Teacher class
Uses

The problem becomes easy to understand


Interactions can be easily modeled
Type in C++

Mechanism for user defined types are


Structures
Classes
Built-in types are like int, float and double
User defined type can be
Student in student management system
Circle in a drawing software
Graphical Representation of Classes
Graphical Representation of Classes
Class Example: Person
Syntax of Class
Syntax of Class and Object

Demo oop1.cpp
Why Member Function?

They model the behaviors of an object


Objects can make their data invisible
Object remains in consistent state
Example:
Student aStudent;
aStudent.rollNo = 514;
aStudent.rollNo = -514; //Error
Objects

In order to use the class functionality, we need to instantiate the class to create an object.
An object is an instance of a class. In simple words, we can say that an object is a variable
of type class.
Classname objectname;
Once the object is created, it can be used to access the data members and functions of that
class.

int var; // declaring built in int data type variable


Student aStudent; // declaring user defined class Student object
Summarize

A class:
It’s a blue print.
It’s a design or template.
An Object:
It’s an instance of a class.
Implementation of a class.
NOTE:
Classes are invisible.
Object are visible.

You might also like