ApexCin777
Programmer
Hi, I'm a beginner in programming with Visual C++ hoping someone with a little more experience could explain this thing about declaring classes and stuff. I have this book that tells me to declare data members or member variables as private and use public accessor methods or class functions to access the variables. Read this paragraph from the book and if you can, explain it to me. Thank YOU very much!
From the book:
Accessor functions enable you to separate the details of how the data is stored from how it is used. This enables you to change how the data is stored without having to rewrite functions that use the data.
If a function accesses a variable from a class directly, it would have to be rewritten if you changed how that data is stored in the class. With public accessor methods, the class will return the right value no matter how you arrive at the variable's value. The calling function doesn't need to know whether you are storing it as an unsigned integer or a long, or whether you are computing it as needed.
Back to me:
For some reason I don't understand why it is better to declare data members as private and use public accessor functions or methods. If anyone can explain this to me, or send me links to pages that can, THANK YOU!
From the book:
Accessor functions enable you to separate the details of how the data is stored from how it is used. This enables you to change how the data is stored without having to rewrite functions that use the data.
If a function accesses a variable from a class directly, it would have to be rewritten if you changed how that data is stored in the class. With public accessor methods, the class will return the right value no matter how you arrive at the variable's value. The calling function doesn't need to know whether you are storing it as an unsigned integer or a long, or whether you are computing it as needed.
Back to me:
For some reason I don't understand why it is better to declare data members as private and use public accessor functions or methods. If anyone can explain this to me, or send me links to pages that can, THANK YOU!