🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

C++ Encapsulation

C++ Encapsulation

Encapsulation in C++

Encapsulation is the first pillar of Object Oriented Programming. It means wrapping up data attributes and methods together. The goal is to keep sensitive data hidden from users.

Encapsulation is considered a good practice where one should always make attributes private for them to become non-modifiable until needed. The data is ultimately more secure as a result of this. Once members are made private, methods to access them or change them should be declared.

Example of Encapsulation


#include <iostream>
using namespace std;

class class_name
{
private:
    int a;

public:
    void setA(int num)
    {
        a = num;
    }

    int getA()
    {
        return a;
    }
};

int main()
{
    class_name obj;
    obj.setA(5);
    cout << obj.getA() << endl;
}

Output:

5

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.