?
Solved

Access functions, schmaccess functions

Posted on 1998-08-20
6
Medium Priority
?
179 Views
Last Modified: 2010-04-01
OK, I think understand the idea behind encapsulation and access functions. However one thing keeps bothering me: C++ books and FAQ's keep saying to use access functions to hide the implementation. Bruce Eckel, in "Thinking in C++" page 314, for example says "you may learn sometime later that it would be much more useful to represent the state information as a float rather than an int, but because int i is part of public interface you can't change it".

His example has a class with a members:

private:
int i;

public:
int read() const { return i; }
void set( int I ){ i = I; }

What if 'i' did become a float? The read() function now has to return a float and everyone using it has to change their code. The set function has to take a float argument and everyone using it has to change their code, or allow the compiler to do a possibly unintended implicit cast.

The only thing you've hid is the name 'i'. What's so great about that? So, how do you change i from an int to a float without anyone who uses your class knowing it happened?

Thanks,
NC
0
Comment
Question by:nchenkin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 1170824
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170825
The example suffers some from being a bit simplistic and suffers a lot from being incomplete.

Now first of all, what this guards against is cases where you change how data is stored within the class, but not how other code interfaces with the class.  So in the previous example, if if you change i from int to float,  the read() function would continue to return an int.  It would have to convert the float to an int and return the int.  This is one change as opposed to making the same change everywhere the number would have been accessed directly if read() hadn't been used.  Similarly, set() would continue to take an int, it would have to convert the int to a float and store the float.  Again, the change would only have to be made in one place not every place the  number is set.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1170826
Nietod - that was  a bit "evil" to claim coming when you hadn't written anything!!!

That aside, the problem is this is a really bad (meaning unclear) example.

A better example might be a case where (1) i had to be calculated, (2) stored in another place (e.g. a file or database) or (3) another variable also needed to be updated whenever i was updated.

Here are some cases of the above

(1) I won't give an example for this, but if you figure out that i may change more than one way (e.g. it contains a time, or the result of some calculations), then you can imagine that directly writing to i is more likely to require future changes to calling code than by using access functions.  

A simpler example is :

(2) Suppose i is currently stored in an int variable, but in future might be stored in a database.  If you write a class like this

class someclass
{
 public:
   int i ;
}

then all the calling code will have to change when you decide that i should go into a database, file or whatever.

However if you INITIALLY write the class like this

class someclass
{
private:
int i;

public:
int read() const { return i; }
void set( int I ){ i = I; }
}

When you later decide that i belongs in a file/database rather than in an int variable, you simply remove the int i member function, and rewrite the read and set functions.  The advantage is the calling code would not ever have to change, just the class definition.


(3) Suppose the class also contains a variable called i_squared, which is supposed to contain i*i at all times [I'm sure you can think of a real world example with a more complex calculation that takes a long time].  If this variable is added after the initial definition of the class, all calling code would have to change (to update this variable too) unless you used access functions.


Finally in terms of the book example, he is really talking about a situation similar to my case (2).  He is saying that suppose i was stored internally as a float but presented itself externally to the world as an int.  In this case you wouldn't have to change the calling code, if you used access functions.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 22

Expert Comment

by:nietod
ID: 1170827
Now what you probably find unsatisfying about that example/answer is that you are thinking that the read() and set() functions are not "sufficient" if they continue to deal with ints.  They cannot set i to a fractial number and they can only return the integer portion of i.  Thus they can no longer do a complete job.  Not quite right.  If the number expressed by i was always an integer and later the need arose for it to be fractional, then yes, this solution would be insufficient.  But that is a case where the interface to the class has changed.  That is, as far as code outside the class is concerned, the class has changed.  In those sort of cases, you would have to change read() and set() to deal with real numbers and change all the code that uses them.  That is not what this protects against.

This protects against a case where you need to store the number differently inside the class, but the number still has the same "appearance".  That is the number would still be integer in nature, only it would be stored as a floating point number.  Hor example, if the number is used in a long series of floating point calculations, it might be easier and faster to store it in a floating point format.  However, the number would always record an integer value, just in a different format, thus read() and set() could continue to work with ints and do a good job.

make sense?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170828
answers,  for questions requiring long answers (it took me 15 minutes on that one) we often lock the question with a short reply so that multiple experts don't work on the same question needlessly.  If you started typing first (and you probably did) and had done the same, I would have seent he question locked and I wouldn't have put in my answer.

However, out answers don't intersect too much, so nchenkin will benefit in any case.
0
 

Author Comment

by:nchenkin
ID: 1170829
nietod,

Thanks. I'm a satisfied customer. Access functions here I come...

NC
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question