Solved

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

Posted on 2011-09-12
2
472 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 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

13 Experts available now in Live!

Get 1:1 Help Now