ApexCin777
June 1st, 2002, 06:43 PM
I am reading a book title "Sam's Teach Yourself C++ in 21 Days" and it's real good. But now I'm stuck on a section and need help.
It says that it is a rule of thumb to declare Member variables of a class as private and declare the Member function public. Then you can access the variables by making the functions public accessors.
It says this way is better than just making the variables public.
I understand how to do this but I don't understand why it is better. The book tells you why it is better but I still don't understand. Maybe you can help me by reading what the book says below. THANKS to anyone who can help!
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 that needs to know a variable's value accesses that variable directly, that function would need to be rewritten if you was to change how that variable in that class was stored.
(I don't understand why you would need to rewrite it...EXAMPLE PLEASE!)By having the function call a public accessor of the class, the class can easily 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:
I think what it means by 'the way the variable was stored' is it's data type like integer or double. I'm not sure. I just need an example of why it is better. Is it like symbolic constants are better than literal constants because you can change it once and it changes all? Please help?
It says that it is a rule of thumb to declare Member variables of a class as private and declare the Member function public. Then you can access the variables by making the functions public accessors.
It says this way is better than just making the variables public.
I understand how to do this but I don't understand why it is better. The book tells you why it is better but I still don't understand. Maybe you can help me by reading what the book says below. THANKS to anyone who can help!
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 that needs to know a variable's value accesses that variable directly, that function would need to be rewritten if you was to change how that variable in that class was stored.
(I don't understand why you would need to rewrite it...EXAMPLE PLEASE!)By having the function call a public accessor of the class, the class can easily 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:
I think what it means by 'the way the variable was stored' is it's data type like integer or double. I'm not sure. I just need an example of why it is better. Is it like symbolic constants are better than literal constants because you can change it once and it changes all? Please help?