-2

While the internet offers examples as given below when learning CPP:

#include <iostream>
class Employee {
  private:
    int salary;
  public:
    Employee() : salary(0){ }
    Employee(int s) : salary(s){ }
    Employee(const Employee& ref) : salary(ref.salary){ }
    void setSalary(int s){
      salary = s;
    }
    int getSalary() {
      return salary;
    }
};
int main() {
  Employee e1(10000);
  e1.setSalary(50000);
  std::cout << e1.getSalary() << "\n";
  return 0;
}

Queries:

  1. I am really confused on the usage of "private" keyword here!! Isn't "private" supposed to stop external functions from accessing the data members of a class. What is the point of making the data as "private" and then providing access to them through "public" member functions of the class??
  2. Is this how classes are developed in real-time?? Am I missing something??
Pisers
  • 103
  • 5
  • 2
    There are different design philosophies. [Some call for getters/setters](https://stackoverflow.com/questions/565095/are-getters-and-setters-poor-design-contradictory-advice-seen/565290#565290) to simplify later design changes etc. [Some think this won't work anyway](https://stackoverflow.com/questions/1568091/why-use-getters-and-setters-accessors-for-private-vars-instead-of-making-vars-p/12108025#12108025). Re. constructor: The module building an object is often different from the one using it, see dependency injection. Only one needs to change when the constructor changes – Homer512 Aug 10 '23 at 07:18
  • 3
    The point of data hiding is to **control** access to member variables, not **prevent** access to member variables. A class could be written with getters and setters, or it could be written with getters only, or it could be written with more indirect access to member variables, But if member variables are completely hidden then by definition they might as well not exist. It's hard to illustrate with toy examples like the code you are quoting but in the real world classes are written in a variety of ways that give the users of that class just the right level of access to the class. – john Aug 10 '23 at 07:18
  • If at some point the `private` data changes, the getter function can be updated to cope without callers of the function having to care. – Jesper Juhl Aug 10 '23 at 07:20
  • As a thought experiment, assume that you want to store employees in a database instead of memory. You could change the implementation of get and setSalary to do database operations and the code that calls these methods would not need to change at all. – Botje Aug 10 '23 at 07:20
  • "Is this how classes are developed in real-time?" - What does that mean? – Jesper Juhl Aug 10 '23 at 07:21
  • 1
    A common reason for data hiding is to maintain *class invariants*, for example a vector has a size (perhaps an integer) and a pointer to some allocated elements. Obviously the number of elements allocated and the size must match up. Data hiding helps ensure this. – john Aug 10 '23 at 07:21
  • 4
    Incidentally I'm not sure where you got that code from but it's not the best quality. The copy constructor is redundant and should be defaulted, and the `getSalary` method should be declared `const`. Maybe you need a better quality tutorial? – john Aug 10 '23 at 07:27
  • The example shows what you *could* do, but perhaps not what you *should* do. Saw a conference video on Safety last night, and it concluded that AI bots produce bad C++ code "Because they are trained on the code volume on the internet, and all the C++ code is wrong." – BoP Aug 10 '23 at 08:17

0 Answers0