Encapsulation in C++

 #include <iostream>

#include <string>

using namespace std;


class myClass {

  public:

    void setName(string x) {

      name = x;

    }

    string getName() {

      return name;

    }

  private:

    string name;

    int age = 4;

};


int main() {

  myClass myObj;

  myObj.setName("John");

  cout << myObj.getName();



  return 0;

}


Comments