Accessors "get & set"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mahdialkhatib
    New Member
    • Aug 2008
    • 1

    Accessors "get & set"

    Hello everybody,

    I'm wondering about these accessors and what the benefit that I would get from them i understood what "set function" is for,

    but i didn't get it with the "get function ".


    even thought i searched about this topic in this site and one "Expert " tried to explain it through the car example and something with speed......
    any way,


    by the way some people even the author D.S Malik use at the end of heading of it the keyword "const" so what the benefit of it if it can't change the values

    may be this example will deliver my point

    [code=cpp]
    consider this in the main function
    int main()
    {
    clockType myclock;
    clockType yourclock;

    int hours;
    int minutes;
    int seconds;
    .
    .
    .
    .
    }

    void clockType::getT ime (int &hours, int &minutes, int &seconds) const
    {
    cout << "getTime is started at the body of it \n";
    hours = hr;
    minutes = min;
    seconds = sec;
    cout << "getTime is ended at the body of it \n";
    }
    [/code]
    ** and how the compiler would know which object to deal with "myclock" or "yourclock" //consider the parameters list it is called by reffrence



    I didn't get it, so please if any one explain it in the easiest way i would be greatful for him

    ********My english is not that good so please use simple terms********** *********


    thanks alot
    Last edited by Banfa; Aug 11 '08, 01:13 PM. Reason: Removed bold tags added [code]...[/code] tags
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    Consider the following version instead:
    [code=cpp]
    int main()
    {
    Clock myclock;
    Clock yourclock;

    int hours;
    int minutes;
    int seconds;

    myclock.setTime (23,12,54);
    yourclock.setTi me(12,0,0);
    // etc ...

    std::cout << "The time of myclock is "

    myclock.getTime (hours,minutes, seconds);

    std::cout << hours << ":" << minutes << ":" seconds << std::endl;
    }
    [/code]

    Lines 3,4: myclock and yourclock are not clock types but are actually clocks themselves, so we call the class Clock not clockType.

    Lines 10,11: The setTime member function sets the internal variables of the clock objects. When I call myclock.setTime(23,12, 54); I will set the time of myclock to 23:12:54, when I call yourclock.setTime(12,0,0 ); I will set the time of yourclock to lunchtime.

    Line 16: The getTime function reads the time of the clock object and writes it into the variables you supplied to it: hours, minutes seconds. It does not change the state of the clock object and we use the 'const' keyword to enforce this.
    myclock.get(hou rs,minutes,seco nds); gets the time of myclock, not yourclock.

    Please, next time don't write your whole message in bold, and please use [code]...[/code] tags around your code.

    Comment

    Working...