Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Something I don't understand about Classes, Objects, Method, Members..

Status
Not open for further replies.

ApexCin777

Programmer
Jun 1, 2002
2
0
0
US
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!
 
Think Like This.

You have one variable in you class
public CString Name ;

You used this variable with all the function everywhere,
like
void TakeInfo()
{
classObj.Name = "Manish"
}
....at many function....

Now my boss will tell me i want to see name like this...

&quot;< Manish >&quot;;
Now For change
What you must do is open everyfunction which uses Name variable and Update that with &quot;< >&quot;.

===========================
Now Issue with Accessors...

You have made function..

CString GetName()
{
return Name;
}
void SetName(CString tmpName)
{
Name = tmpName;
}

And you used that accessors function throughout your project.
void TakeInfo()
{
classObj.SetName(&quot;Manish&quot;)
}
... And So On in many function...

Now if my boss urges changes , i will do it in a minute by
changing this code...

void SetName(CString tmpName)
{
Name = &quot;<&quot;+tmpName+&quot;>&quot;;
}

That's All.
If ne difficulties in this.. reply.
===============
Manish(7496764)
Good Way to Learn is Log You Errors.
===============
 
The idea is for the public interface to remain constant while the underlying mechanism can change.

For example, suppose in payroll system a sales employee can take a cash advance. You specify a public function:

TakeCashAdvance(double currency_value, string employee_ID);

Now what if the company decides to change the BUSINESS RULES for cash advances. Suppose they impose an upper limit, or decide to charge interest. To the application programmer, the function TakeCashAdvance does not change. The business rules are encapsulated in the private and protected section of the class. Low level functions can be modified to satisfy the new business rules, and the maintenance programmer is none the wiser.

Get it?
 
Thanks everyone for your help. I understand now. Thanks Alot!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top