You are on page 1of 6

Data Hiding and Encapsulation using

Access Speci ers in C++


Google Bookmark Facebook Twitter Print Email More

Encapsulation De nition:
Encapsulation is a process of capsulation of data and methods into a combined single unit. In C++, encapsulation is
used along with the classes concept.

Bundling of data and methods (functions)  as a single unit is known as encapsulation.

Encapsulation represents the information of variables (attributes) in terms of data and, methods (functions) and its
operations in terms of purpose.

Encapsulation is the process of combining data and function into a single unit called class.

Encapsulation is a powerful feature that leads to information hiding and abstract data type.

They encapsulate all the essential properties of the object that are to be created.

Using the method of encapsulation the programmer cannot access the class directly.
Data Hiding De nition:
Data hiding is a technique especially practised in object-oriented programming (OOP).Data hiding is hiding the details
of internal data members of an object.

Data hiding is also known as Information hiding.

Sometimes Data Hiding includes Encapsulation. Thus Data Hiding is heavily related to Abstraction and
Encapsulation.

Data Hiding is the one most important OOP mechanism. Which is hide the details of the class from outside of

the class.

The Class used by only a limited set of variables and functions, others are hidden by the class.

Access Speci ers in C++


Access speci ers de ne how a member's variables and member's functions of a class can be accessed from outside
the class. However, all members of a class can be accessed from within the class without any restriction. 
Class members can be declared as public, protected or private access speci ers, which is used to build the

encapsulation capabilities of the class.


There are three access speci ers.
Private members: These can be of the same class.

Protected members: These can be accessed only from within other members of the same class and its

derived classes.

Public members: These can be accessed from anywhere where the object is accessible.

By declaring the member variables and functions as a private in a class, the members are hidden from outside the
class.Those private members and data cannot be accessed by the object directly.

Encapsulation General Form:


class class_name {
private:
datatype member_variables;
datatype member_functions;
public:
datatype member_variables;
datatype member_functions;
};

main() {
class_name objectname1, objectname;
}

Encapsulation Example:
class Square {
private:
int Num;
public:
void Get() {
cout << "Enter Number:";
cin>>Num;
}
void Display() {
cout << "Square Is:" << Num*Num;
}
};

void main() {
Square Obj;
Obj.Get();
Obj.Display();
getch()
}

In the above example, the variable “Num” is private. Hence this variable can be accessed only by the members of the
same class and is not accessible anywhere else. Hence outside the classes will be unable to access this variable Which

is called data hiding.

At the same time, “Square” class contains two other methods namely “Get” and “Display” which has public members.

Here “Get” method just prints the value while “Display” method prints the square of the value in the variable “Num”.
Here the class “Square” implements Data Encapsulation concept by capsulation of the value in the variable “Num” and

thus allowing the user only to perform a restricted set of operations on the hidden variable “Num”.
Encapsulation is used to reduce the complexity and improve reusability.

Encapsulation de nes the interface clearly which improves the readability and understandability.

Encapsulation helps to hide important data and ensures enhanced Security.

The member variables and members are bundled into a single unit as a class which makes the maintenance
easy.

The encapsulated classes are straightforward and are easy to manage and improves the future development

of the application.

Classes in C++

1 OOPS Overview and Concepts (/c-tutorials/classes-in-c/oops-overview-and-concepts)

2 Introduction Of Classes ( OOPS ) (/c-tutorials/classes-in-c/introduction-of-classes-oops)

3 Constructors and Destructors in C++ Classes (/c-tutorials/classes-in-c/constructors-and-destructors-in-c-classes)

4 Data Hiding and Encapsulation using Access Speci ers in C++


[ Reading ] (/home/blog-1/datahidingandencapsulation)

5 Understanding Friend Function and Characteristics In C++ (/oops-in-c/classes-in-c/understanding-friend-function-and-


characteristics-in-c)

Read More Articles

1 Simple Merge Sort Program in C++ (/data-structures-in-c/sorting-programs/simple-merge-sort-program-in-c)

2 Scope Resolution Operator In C++ (/home/blog-1/scoperesolutionoperatorinc)

3 Simple Program for Virtual Functions Using C++ Programming (/c-programs/simple-program-for-virtual-functions-using-c-


programming)

4 Simple Class Example Program For Find Prime Number In C++ (/c-programs/c-c-class-example-programs/Simple-Class-
Example-Program-For-Find-Prime-Number-In-C)

5 Simple Example Program For Parameterized Constructor In C++ (/c-programs/c-constructor-example-programs/simple-


example-program-for-parameterized-constructor-in-c)

6 De ne Constructor in Outside Class Example Program In C++ (/c-programs/simple-program-for-constructor-using-c-


programming)
7 Simple Example Program For Copy Constructor
Master In In C+
C+++(/c-programs/c-constructor-example-programs/simple-example-
- Free Android App →
program-for-copy-constructor-in-c)
i (https://www.addthis.com/website-tools/overview?utm_source=AddThis%20Tools&utm_medium=image&utm_campaign=Marketing%20tool%20logo)
Lets Download (https://play.google.com/store/apps/details?id=com.thiyagaraaj.masterlearncpp)
(index.html)
8 Simple Program for Function Overloading Using C++ Programming (/c-programs/simple-program-for-function-overloading-
using-c-programming)

9 Simple Program for Single Inheritance Using C++ Programming (/c-programs/simple-program-for-single-inheritance-using-c-


programming)

10 Simple Program for Inline Function without Class Using C++ Programming (/c-programs/simple-program-for-inline-function-
using-c-programming)

11 Simple Addition ( Add Two Integers ) Example Program (/c-programs/c-basic-example-programs/simple-addition-add-two-


integers-example-program)

12 Factorial Using Function Example Program In C++ (/c-programs/c-common-example-program/factorial-using-function-


example-program-in-c)

13 Simple Example Program For Constructor In C++ (/c-programs/c-constructor-example-programs/simple-example-program-for-


constructor-in-c)

14 Simple Program for Read user Input Using cin (/c-programs/c-basic-example-programs/simple-program-for-read-user-input-


using-cin)

15 Simple Example Program for Inline Function Using C++ Programming (/c-programs/c-function-example-programs/simple-
example-program-for-inline-function-using-c-programming)

16 Simple Example Program For Constructor Overloading In C++ (/c-programs/c-constructor-example-programs/simple-example-


program-for-constructor-overloading-in-c)

17 Factorial Using Loop Example Program In C++ (/c-programs/c-common-example-program/factorial-using-loop-example-


program-in-c)

18 Simple Stack Program in C++ Programming (/data-structures-in-c/stack-programs/simple-stack-program-in-c-programming)

19 Simple Program for Friend Function Using C++ Programming (/c-programs/simple-program-for-friend-function-using-c-


programming)

20 Simple Program for Static Data and Member Function Using C++ Programming (/c-programs/simple-program-for-static-data-
and-member-function-using-c-programming)

21 Simple Program for Unary Operator Overloading Using C++ Programming (/c-programs/simple-program-for-unary-operator-
overloading-using-c-programming)

22 Do While Loop Example Program In C++ (/c-programs/c-basic-example-programs/do-while-loop-example-program-in-c)

23 Simple Program for Binary Operator Overloading Using C++ Programming (/c-programs/simple-program-for-binary-operator-
overloading-using-c-programming)

24 Simple Program for Multiple Inheritance Using C++ Programming (/c-programs/simple-program-for-multiple-inheritance-


using-c-programming)
25 Simple Copy Constructor Example Program ForIn
Master Find
C+Factorial
+ - FreeInAndroid
C++ (/c-programs/simple-program-for-copy-constructor-using-
App →
c-programming)
i (https://www.addthis.com/website-tools/overview?utm_source=AddThis%20Tools&utm_medium=image&utm_campaign=Marketing%20tool%20logo)
Lets Download (https://play.google.com/store/apps/details?id=com.thiyagaraaj.masterlearncpp)
(index.html)

Queries

 Request for new program (https://docs.google.com/forms/d/e/1FAIpQLSckPDFRR7yWrBtMSc1h02cA-

02Uoph5AAc3rq9ZzZjVWaNScw/viewform)

Contact us

 support@littledrops.net (mailto:supoort@littledrops.net)

Android Apps

 Learn Python (https://play.google.com/store/apps/details?id=com.thiyagaraaj.learnpython)

 Learn Spring (https://play.google.com/store/apps/details?id=com.thiyagaraaj.springframework)

 PostgreSQL (https://play.google.com/store/apps/details?id=com.thiyagaraaj.postgresql)

 Perl (https://play.google.com/store/apps/details?id=com.thiyagaraaj.perl)

Android Apps

 Learn Java (https://play.google.com/store/apps/details?id=com.thiyagaraaj.learnjava)

 Learn Ubuntu (https://play.google.com/store/apps/details?id=com.thiyagaraaj.learnubuntu)

 Unix Guide (https://play.google.com/store/apps/details?id=com.thiyagaraaj.unixcommands)

 Learn C++ (https://play.google.com/store/apps/details?id=com.thiyagaraaj.masterlearncpp)

© 2007-2020 All Rights are reserved. | WWW.LITTLEDROPS.NET (http://www.littledrops.net) and WWW.THIYAGARAAJ.COM (http://www.thiyagaraaj.com)

You might also like