Understanding C++ Encapsulation | Learn Simple

AI Basics


Encapsulation, as the name suggests, is used to encapsulate data and methods together. This is used to hide the value from direct access outside the class. Similar to capsules, data and methods are bundled together to protect them from access. Learn all about encapsulation in this tutorial on encapsulation in C++.

Graduate Program: Full-Stack Web Development

Cooperation with Caltech CTMERegister now

Graduate Program: Full-Stack Web Development

What is Data Hiding?

Encapsulation usually involves the process of hiding data. This data hiding, also known as information hiding, is one of the key features of object-oriented programming and helps hide details from outside the class.

Data hiding is related to two key concepts in oops: encapsulation and abstraction.

Now you have an understanding of C++ encapsulation.

What is C++ Encapsulation?

Encapsulation is a feature used to hide information. This is the process of binding data and functions into a single unit called a class. This data is also called data members, and functions can be called methods that operate on these data members. Encapsulation restricts direct access to class data by making variables and functions private.

Encapsulation_in_Cpp_Example1.

Now you can understand why encapsulation is necessary in C++.

Why do we need encapsulation?

Data members and functions are important details that exist within a class, and it is unsafe to leave these methods publicly accessible. Encapsulation helps hide these important details from the user. Bind data and methods to prevent direct access to object details.

Encapsulation has many advantages. This will be explained later in this tutorial. But before that, understand how encapsulation hides your data.

New Course: Full-Stack Development for Beginners

Learn Git commands, Angular, NodeJS, Maven and moreRegister now

New Course: Full-Stack Development for Beginners

C++ access modifiers

Data is hidden from outside the class with the help of access modifiers. Access modifiers or access specifiers are used to determine the scope of class members, that is, whether they are accessible or restricted to members outside the class.

There are three types of access modifiers:

1. Private: A member with private scope is a member that can only be accessed from within the same class. External members or functions cannot access private members.

2. Public: A member with public scope is a member that can be accessed both internally and externally, i.e. a member that can be accessed from anywhere.

3. Protected: Members with protected scope cannot be accessed outside the class, but are easily accessible from derived classes.

Learn how to access private data in C++ here.

Accessing private data in C++

As you can see, data members with private scope cannot be accessed outside the class, but is there a way to access them?

These private data members can be accessed using setter and getter methods. These methods are declared at public scope within the class and are useful for accessing private data.

A setter method is used to write a value or assign a value to a private variable within a class.

Getter methods return values ​​or allow functions outside the class to read data assigned to private variables.

Now with an example it will be easier to understand.

Encapsulation_in_Cpp_Example2.

In this example, we have a class named Company with a private variable Salary. We need access to the private variable Salary because that’s the only way we can assign the total salary value to the private variable.

The setSalary() function is the setter function and the getSalary() function is the getter function. The main function passes two values ​​as arguments to the setSalary() function: 30000 (full-time salary) and 6000 (overtime). Inside the setter function we add both salaries and assign the sum to the private salary variable.

The getSalary() function returns the value of the salary variable.

This is an example output.

Encapsulation_in_Cpp_Example3

Now understand the benefits of encapsulation in C++.

Full Stack Web Developer Course

How to become a MEAN Stack expertview the course

Full Stack Web Developer Course

Advantages of encapsulation

  • Encapsulation restricts direct access to class members by hiding data and providing information security.
  • Encapsulation properly encapsulates your code, making it easier to maintain and read, and even easier to understand.
  • Encapsulation reduces code complexity because the encapsulated classes are easier and easier to manage.

Difference between encapsulation and abstraction

Encapsulation

abstraction

Encapsulation is a mechanism to keep code and data together in a single unit, hidden and secure.

Abstraction is a mechanism that hides implementation details and exposes functionality to the user.

Encapsulation focuses on the inner workings, how you do the encapsulation.

Focus on what to do instead of focusing on the inner workings.

Encapsulation is implemented using access modifiers.

Abstraction can be achieved using interfaces and abstract classes.

Encapsulation addresses issues that arise during the implementation stage.

Abstraction addresses problems that arise at the design stage.

Full Stack Web Developer – Advance your career as a MEAN Stack developer with the MEAN Stack Master Program. Register now!

Conclusion

After reading this tutorial on encapsulation in C++, you should understand what data hiding and encapsulation is in C++. Now I understand why encapsulation is necessary in C++. You also learned about access modifiers and how to access private data members with some examples. This tutorial also explained the difference between encapsulation and abstraction.

If you want to build a career in software development, check out Simplilearn’s full-stack development graduate program. You will find this to be the perfect solution to build your career in the right direction.

Do you have any questions about this Encapsulation in C++ tutorial? If so, write them in the comments section. We are here to help you with your questions. For more information on encapsulation in C++, click the link: Encapsulation in C++.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *