C++ Encapsulation

The basic tenet of object-oriented programming is encapsulation. It entails combining data methods and properties. Sensitive information must remain hidden from users.

Encapsulation is regarded as a good practise, in which qualities should always be made private so they can’t be changed unless needed. As a result, the data is ultimately more secure. Methods to access or modify members should be declared once they have been made private.

An illustration of the process of encapsulation is

#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
Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

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

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in