?
Solved

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

Posted on 2011-09-12
2
Medium Priority
?
491 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
[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
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
Starting up a Project
Suggested Courses

777 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