Link to home
Start Free TrialLog in
Avatar of Rugoingwme
Rugoingwme

asked on

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

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
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rugoingwme
Rugoingwme

ASKER

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.