0% found this document useful (0 votes)
69 views

Encapsulation in C++

Uploaded by

bishirvv5
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)
69 views

Encapsulation in C++

Uploaded by

bishirvv5
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/ 13

Encapsulation in C++

PREPARED BY
Dr.C.MAHIBA
ASSISTANT PROFESSOR/CSBS
Data Hiding

Encapsulation is a fundamental concept in object-oriented


programming.
It essentially means binding variables and methods together into a
single unit and preventing them from being accessed by other classes.
 Encapsulation is one of the fundamental concepts in OOP that
bundles data and associated methods that operate on that data into a
single block called CLASS.
 It is also seen as a pathway for restricting direct access to some data
and methods associated with a class (which leads to DATA
HIDING).
 In other words: Encapsulation is about wrapping data and methods
into a single class and protecting it from outside intervention.
Why Encapsulation?

 In C++, encapsulation helps us keep related data and functions


together, which makes our code cleaner and easy to read.
 It helps to control the modification of our data members.
ADVANTAGES OF ENCAPSULATION
 The main advantage of using Encapsulation is to hide the data from
other methods. For example,By making the data private, these data
are only used within the class, but these data are not accessible
outside the class.
 Protects data from unauthorized users
This concept is applicable in the marketing and finance sector, where
there is a high demand for security and restricted data access to various
departments.
 Encapsulation helps us in binding the member functions and data of
a class.
 Encapsulation also helps us make code flexible, which is easy to
change and maintain.
Real-life example
 Suppose you go to an automatic teller machine(ATM) and request
money. The machine processes your request and gives you money.
 Here, ATM is a class. It takes data from the user(money amount and
PIN) and displays data as icons and options. It processes the
request(functions). So, it contains
both data and functions wrapped/integrated under a single ATM.
This is called Encapsulation.
Encapsulation in C++
 Encapsulation in C++ can be implemented using class and access
specifiers.
 In C++, the implementation of encapsulation has two steps:
1. Labeling data members as private using the private access specifier,
2. tagging the member function that manipulates data members as
public.
Access specifiers in C++
 We can make our data public, private, and protected in class. This can be done
with the help of access specifiers like public, private, and protected.
 We can change the access specifier according to our needs and data. Using
these access specifiers, the programmer gets control over data visibility.
 That means a programmer can decide what functions or data should be hidden
and what to show the user.
 NOTE: By default(if not declared), all the items in a class are private.
 Public: The data Members/member functions declared as public can
be accessed by the same class and other classes.
 Private: The data Members/member functions declared as private
can be accessed by the same class only. Data cannot be accessed
outside the class. If accessed, it will give an error.
 Protected: The data Members/member functions declared as private
can be accessed by the same class and the derived class.
#include<iostream>
using namespace std;

class demo {
private:
//private member hidden from outside world
int var;
public:
//to set the value of var
void set_var(int temp)
{
var =temp;
}

//to get the value of var


int get_var()
{
return var;
}
};
// main function
int main() {
//creating object of demo class
demo obj;

//using set_var function to set the value 100 to private member var
obj.set_var(100);

//using get_var function to get/access the value of private member var


cout<<obj.get_var();

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy