Week 2 L 1
Week 2 L 1
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
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
Reuse
Less redundancy
Increased maintainability
Polymorphism:
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
Demo oop1.cpp
Why Member Function?
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.
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.