OOP Principles: Encapsulation & Abstraction
OOP Principles: Encapsulation & Abstraction
Programming
WEEK 2
17
Information Hiding 2
OOP)
Encapsulation is a process of
wrapping of data and methods in a
single unit.
Itinvolves the bundling of data members
and functions inside a single class.
Bundling similar data members and
functions inside a class together also helps
in data hiding.
Real Life Example of 4
Encapsulation
Real Life Example of 5
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
ENCAPSULATION – ADVANTAGES 6
Low complexity
Better understanding
Abstraction in OOP (2nd Principle) 7
Student’s Perspective
Attributes
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Behavior
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
Example – Abstraction 10
Teacher’s Perspective
Attributes
- Name - Employee
ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
11
Access Specifiers 12
Public
Private
Protected
Syntax 13
class
{
private:
// private members and function
public:
// public members and function
protected:
// protected members and function
};
A Simple Class 14
Brain-Storming 15
Consider a world in which people can save money into a bank. There may be
different banks (e.g. Bank Al-Habib, Allied Bank etc) and customers may have
different activities with their banks.
• Write down the different kinds of objects which are involved in this “Personal
banking” domain.
Customer
Consider a customer who can open bank accounts in a bank. Write down the list of
attributes required to model the data components of a customer, (e.g. the name of
the customer)
For each of these attributes:
• Would you model it as a class-level/object-level attribute and why?
• What will be the appropriate data type?
• Would you make this attribute visible to other objects and why?
• Would you hide this attribute from other objects and why?
• Write down the list of methods required to model the procedural component of a
customer, i.e. Operations/ behavior that a customer can perform.
Q1) Write a C++ program to create a class to get and print16
details of a student.
- The details of the student includes the following:
Name, rollNo, total, perc
- The class contains two functions input and display that
takes input from the student and displays the information
using the display function.
- Create a object of the class in the main function and call
both the functions using the object.
Variable Declarations
When you declare a variable you are telling the
compiler what kind of data you will be storing in
the variable.
For
example, the following two declarations
declare the three variables used in that program:
int number_of_bars;
double one_weight, total_weight;
Input and Output 20
Output Using cout
The values of variables as well as strings of text may be
output to the screen using cout.
Consider the following line from the program:
cout << number_of_bars << " candy bars\n";
This statement tells the computer to output two items: the
value of the variable number_of_bars and the quoted string
" candy bars\n".
You can simply list all the items to be output preceding
each item to be output with the arrow symbols <<.
The arrow notation << is often called the insertion
operator.
Input and Output 21
Include Directives and Namespaces
We have started all of our programs with the following two lines:
#include <iostream>
using namespace std;
These two lines make the library iostream available. This is the library
that includes, among other things, the definitions of cin and cout.
The following line is known as an include directive. It “includes” the
library iostream in your program so that you have cin and cout
available:
#include <iostream>
Input and Output 22
Include Directives and Namespaces
C++ divides names into namespaces.
A namespace is a collection of names, such as the names cin and
cout.
A statement that specifies a namespace in the way illustrated by the
following is called a using directive.
using namespace std;
This particular using directive says that your program is using the std
(“standard”) namespace.
This means that the names you use will have the meaning defined for
them in the std namespace
Input and Output 23
Escape Sequences
The backslash, \, preceding a
character tells the compiler that
the character following the \
does not have the same
meaning as the character
appearing by itself.
Such a sequence is called an
escape sequence.
Input and Output 24
Write a class named Vehicle that can represent both the Rickshaw and Bike. Each vehicle
has the following details:
year. An int that holds the vehicle’s model year.
manufacturer. A string that holds the manufacturer name of that vehicle.
speed. An int that holds the vehicle’s current speed.
Member functions.
accelerate. The accelerate function should add 5 to the speed member variable each
time it is called.
brake. The brake function should subtract 5 from the speed member variable each time it
is called.
Demonstrate the class in a program that creates a Vehicle object for a Rickshaw and for a
Bike, and then calls the accelerate function and call the brake function .