WEB DEVELOPER SITE
PYTHONPHPJAVA
 

C++ Encapsulation


Encapsulation

encapsulation means more than simply combining attributes and behavior together within a class; it also means restricting access to the inner workings of that class.

Encapsulation allow to hide details of class realization .
  • Control the way data is accessed or modified.
  • Code is more flexible and easy to change with new requirements.
  • Change one part of code without affecting other part of code.

Access Specifiers

The three levels of access specifiers are public, protected, and private.

public member is accessible from outside the class, and anywhere within the scope of the class object.



#include <iostream>
#include <string>
using namespace std;

class myClass {
    public:
        string name;
};

int main() {
    myClass myObj;
    myObj.name = "welookups";
    cout << myObj.name;

    return 0;
}
//output
welookups