Solved

Please give me an example of an accessor for a new programmer to C++

Posted on 2011-09-12
2
476 Views
Last Modified: 2012-05-12
Hi,  

I just started using C++.  I'm brand new to programming.  I am using a book that does a poor job of explaining what an accessor is, and how to use an accessor to call a private variable.  I know why you use an accessor.  You use it to examine or modify members of a class.  Accessors are typically used to access private variables in order to check how those variables are modified. What I don't understand is how to write the code properly to create an accessor without the compiler choking.

I've searched around the internet, and found an explanation that is somewhat helpful, but i have further questions about it that I want to ask here.  The somewhat helpful explanation/example is as follows:
----------------------------------------------------------
class Square
{
private:
           int side;
public:
          void set_side(int length)
{
side = length
};
          int get_side() const;
          int calc_area() const;
};
In the above example, set_side and get_side are accessor methods.

I would add that there are often good reasons to use accessors, rather
than making data public. The accessors give you a chance to make sure
data are valid before actually modifying the object, as well as a chance
to log changes.

Suppose, in the above example, that "side" takes on a value you do not
intend. You can validate changes to the variable, and log failures,
from the accessor:

void set_side( int length )
{
if( length > 0 )
side = length;
else
std::cerr << "warning: can't set_side( " << length << " )\n";
};
----------------------------------------------------

my questions are (based on the above example):
1.  does the accessor function have to start with "void"?
2.  is it the practice to create a private variable "side", then a accessor that is "get_side"?
3. is the "int length" variable the variable that is doing the accessor?
4.  it looks like the accessor is being declared and defined in the above code.  Is that the case?
5. if it is being defined and declared, what is the accessor?
 is it this:  
set_side( int length )
or this
void set_side( int length )

I've attached code where i'm trying to create my own accessor.  I think my accessor is

void setwheelSize(int wheelSize);

for the private variable
int wheelSize;

and the relevant code is

/ defining the function wheelsize
int Tricycle::setwheelSize(int wheelSize) //got rid of the semicolon, then cut/pasted exact statement in public declarations on
//set wheelsize to what was in the public variables and functions compiled and didn't work    so used Tricycle class
{
      if (wheelSize >= 4)
      {
            setwheelSize = wheelSize;
            else
            std::cout << "wheelsize is too small! must be greater than 4";
      
      }                        
            
}

any explanations on debugging my code are also greatly appreciated.  Thanks.  newtricyclewheel3.cpp
0
Comment
Question by:Rugoingwme
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 36526966
OK, let me try to assess that point by point

>>1.  does the accessor function have to start with "void"?

Not necessarily. In your example, instead of using

std::cerr << "warning: can't set_side( " << length << " )\n";

you could also return 'bool' to indicate success or failure, e.g.

boolset_side( int length )
{
  if( length > 0 )
  {
    side = length;
    return true;
  }

  return false;
};

>>2.  is it the practice to create a private variable "side", then a accessor >>that is "get_side"?

It is a common and good practise, since the function's names simply tell you what they are in fact doing.

>>3. is the "int length" variable the variable that is doing the accessor?

Yes, that is for the one that is suposed to set the value.

>>4.  it looks like the accessor is being declared and defined in the above
>>code.  Is that the case?

Yes, that is true - yu can provide both the declaration and the implementation in the same place, yet that has some caveats - you might run into trouble in more complex situations.

>>5. if it is being defined and declared, what is the accessor?
>> is it this:  
>>set_side( int length )
>>or this
>>void set_side( int length )

The latter - you need to specify a return type plus a complete argument list.
0
 

Author Closing Comment

by:Rugoingwme
ID: 36530744
Thanks for your help, especially addressing my questions point by point.  I realize that the way I cut and pasted the question might have been a bit confusing.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

930 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now