0% found this document useful (0 votes)
6 views29 pages

OOP Principles: Encapsulation & Abstraction

The document covers key concepts of Object-Oriented Programming (OOP), focusing on principles such as Information Hiding, Encapsulation, and Abstraction. It explains how encapsulation wraps data and methods into a single unit for simplicity and clarity, while abstraction selects relevant details to manage complexity. Additionally, it discusses access specifiers, variable declarations, input/output operations, and provides examples and exercises related to class creation in C++.

Uploaded by

sarah ahmed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views29 pages

OOP Principles: Encapsulation & Abstraction

The document covers key concepts of Object-Oriented Programming (OOP), focusing on principles such as Information Hiding, Encapsulation, and Abstraction. It explains how encapsulation wraps data and methods into a single unit for simplicity and clarity, while abstraction selects relevant details to manage complexity. Additionally, it discusses access specifiers, variable declarations, input/output operations, and provides examples and exercises related to class creation in C++.

Uploaded by

sarah ahmed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Object Oriented

Programming
WEEK 2
17
Information Hiding 2

 Itis hidden from


the outside world.
 Itcan only be
manipulated by the
object itself.
 Simplifies
the
model by hiding
implementation
details.
Encapsulation (1st Principle of 3

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

 Simplicity and clarity

 Low complexity

 Better understanding
Abstraction in OOP (2nd Principle) 7

 Abstraction is selecting data from a


larger pool to show only the
relevant details to the object.
 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 8

 Aliis a PhD student and teaches BS


students
 Attributes
- Name -
Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Example – Abstraction 9

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.

Q2) A man who manages a scoreboard wants a simple


application to manage the history of a batsman. For every
new batsman, the app must let us fill the details including
the Id, Name, Age, Runs, avg, etc. At anytime a batsman
can check his runs and his average.
Variables and Assignments 17
Variables and Assignments 18

 An identifier must start with either a letter or the


underscore symbol, and all the rest of the
characters must be letters, digits, or the underscore
symbol.
 C++ is a case-sensitive language; that is, it distinguishes
between uppercase and lowercase letters in the spelling of
identifiers.
 Hence the following are three distinct identifiers and could
be used to name three distinct variables:
 rate RATE Rate
Variables and Assignments 19

 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

 Formatting for Numbers with a Decimal Point


Input and Output 25

 Input Using cin


 You use cin for input more or less the same way you use cout
for output.
 The syntax is similar, except that cin is used in place of cout
and the arrows point in the opposite direction.
Data Types 26
Data Types 27
Data Types 28
Introduction to the Class
string
To use the string class we must
first include the string library:
#include <string>
You declare variables of type
string just as you declare
variables of types int
or double.
For example, the following
declares one variable of type
string
and stores the text "Monday" in
it:
string day;
Define a class to represent a bank account. Include the following members
Data Members 29
Name of the depositor, Account number , Balance amount in the account
Member Function
To deposit an amount
To withdraw an amount after checking the balance
To display name and balance.

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 .

You might also like