Answers for "set and get in c++"

C++
2

set and get in c++

/*Get or Set are methods to Access Private Members 
for example */

class Employee {
  private:
    // Private attribute
    int salary;

  public:
    // Setter
    void setSalary(int s) {
      salary = s;
    }
    // Getter
    int getSalary() {
      return salary;
    }
};

int main() {
  Employee myObj;
  myObj.setSalary(50000);
  cout << myObj.getSalary();
  return 0;
}
Posted by: Guest on December-23-2020

Browse Popular Code Answers by Language