previous | start | next

## Accessing Data Fields

- Only member functions of a class are allowed to access the private data fields of objects of that class.

  >     void raise_salary(Employee& e, double percent)
  >     {
  >        e.salary = e.salary * (1 + percent / 100 ); /* Error */
  >     }

- Data fields must be accessed by accessor and mutator functions.

  >     void raise_salary(Emplyee & e, double percent)
  >     {
  >        double new_salary = e.get_salary() * (1 + percent / 100);
  >        e.set_salary(new_salary);
  >     }

previous | start | next