You are on page 1of 1

HOME C++ TUTORIALS 📱 Master

OOPS IN C++
In C++ 📱PROGRAMS
C++ - Free AndroidOOPS
AppPROGRAMS
→ DATA STRUCTURES
Lets Download

Home C C++ Java Kotlin

 Apps  Facebook  Mail

C++ PROGRAMMING CONCEPTS Search the site... Go

Data Hiding and Encapsulation using


All Articles/Programs In OOPS in C++

Access Speci ers in C++ Classes in C++

1 OOPS Overview and Concepts


Facebook Twitter Print Email Pinterest More

2 Introduction Of Classes ( OOPS )

In this page: 3 Constructors and Destructors in C++ Classes

Encapsulation De nition:
4 Data Hiding and Encapsulation using Access
Speci ers in C++
Data Hiding De nition:
[ Reading ]

Access Speci ers in C++


5 Understanding Friend Function and
Characteristics In C++
Encapsulation General Form:

Encapsulation Example:

Features and Advantages of Data Encapsulation:

Top Pages

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 accessed only from within the members 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”.

Features and Advantages of Data Encapsulation:

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.

Top Pages

1 Simple Merge Sort Program in C++

2 Scope Resolution Operator In C++

3 Simple Program for Virtual Functions Using C++ Programming

4 Simple Class Example Program For Find Prime Number In C++

5 Simple Example Program For Parameterized Constructor In C++

6 De ne Constructor in Outside Class Example Program In C++

7 Simple Program for Single Inheritance Using C++ Programming

8 Simple Example Program For Copy Constructor In C++

9 Simple Program for Function Overloading Using C++ Programming

10 Factorial Using Function Example Program In C++

Queries Android Apps Android Apps

 Queries/Request programs/Contact  Request for new program  Learn Python  Learn Java

 Learn Spring  Learn Ubuntu


Contact us
 PostgreSQL  Unix Guide

 support@littledrops.net  Perl  Learn C++

All Rights are reserved 2018. © www.littledrops.net and www.thiyagaraaj.com

You might also like