About setter function

How will the getter fn be called in this eg ?What will happen if we don’t write the getter function? the print() function will have access to the setter function and hence can access the value of price

@Senjuti256,
Our aim is not to hide private data member from member functions, we want to hide the data member from being accessed by someone who is not a object of class. Basically we don’t want to change the value of variable unless it is done by a class’s member function only. Here, print function does allows the user to change value of variable.

how can print fn allow data to be changed?How is the getter function called?

@Senjuti256,

  • why would you want to change data through print function, it makes no sense, we have made a setter function for that.
  • just write object_name.get(new_value).

I cannot understand when the getter fn will be called inside the class?

@Senjuti256,
Whenever you have a private data member, then you won’t be able to access that variable in main for e.g, thus a getter function would be necessary to get the variable’s value.

will the getter fn be called automatically by the object?

@Senjuti256,
No, by the user.

bt where have bhaiya called the getter fn in his code? He has only called the setter fn in the main ()

@Senjuti256,
No, he hasn’t, but it can be done, rewrite the program and call it yourself.