• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Classes » What is Encapsulation in C++
Next →
← Prev

What is Encapsulation in C++

By Dinesh Thakur

Encapsulation is a mechanism that binds together data and functions that manipulate a single logical unit. It keeps the data safe from any external interference and misuse. In other words, Encapsulation is the wrapping up of data and related functions in a single unit called class. The only way to access the data is through the function, which is wrapped in the class. These functions prepare the class’s interfaces through which the outside world (other classes) can access them.

Consider two classes. Library class and a Student class. The Library class encapsulates data (like bookid, bookauthor, booktitle, no_of_copies) and functions (like issue(), book_status() and book description ()) into a single unit as shown in figure. Similarly, the Student class encapsulates data and functions, as shown in the figure.

The two classes are independent of each other. The Student class has no details about the Library class, so it cannot directly access a book’s details. However, if a student wants to access some information about a book from a Library class, they can indirectly access it using the Library class’s appropriate function.

encapsulation in C++The technique shown in the above example is the comer stone of OOP (Object Oriented Programming). It gives the programmer who defined the total class control over what attributes (data) are accessed and how they are manipulated and displayed.

In C++, the concept of Encapsulation is implemented using a class. Data and Functions of a class may be private or public. Private data or function of a class cannot be accessed from outside the class, and public data or functions of a class can be accessed from anywhere (other classes). Normally data of a class is private, so it cannot be accessed by function outside the class, reducing data misuse. A class’s public functions should be coded carefully to protect data from accidental manipulation by outside function.

With Encapsulation, data hiding can be accomplished. Data hiding can be defined as a mechanism of hiding the data Of a class from the outside world (other classes) so that any access to it either intentionally or unintentionally can’t modify the data. Data hiding can be achieved by making the data of a class private.

Advantages of Encapsulation in C++ :

The following are the advantages of Encapsulation:

• It protects the data of a class from accidental manipulation or misuse by the functions outside the class.
• Any changes made in the data and functions of a class do not affect other classes.
• Classes are independent of each other. Therefore, each class can be appropriately studied for a better understanding of the design.

You’ll also like:

  1. Write• A C++ Program To Depict Concept Of Abstraction, Data Hiding, Encapsulation.
  2. TCP/IP Encapsulation
  3. Data Encapsulation
  4. What is Encapsulation in Java? – Definition
  5. Difference Between Abstraction and Encapsulation
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW