Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

773 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