return statemens in member functions
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: return statemens in member functions

  1. #1
    Join Date
    Jun 2009
    Location
    Kansas
    Posts
    3

    return statemens in member functions

    I'm a noob just getting into classes in VC++ (2008). I've seen a lot of examples where one member function will call another, and the called member function will have nothing but a return statement. Here's the latest example I was looking at:
    __________________________
    -within the class "BankAccount":
    public:
    double get_balance();
    private:
    double balance;
    _________________________
    -never mentioned in driver file!
    ____________________________
    -in implementation file:
    double BankAccount::get_balance()
    {
    return balance;
    }
    ____________________________

    Is this a formality? Planning for later? Or does it actually set a value somewhere?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,273

    Re: return statemens in member functions

    Given this style, it's probably set in a corresponding set_balance() method.

  3. #3
    Join Date
    Nov 2006
    Posts
    1,611

    Re: return statemens in member functions

    Since you're new to C++, this may simply be a convention you've not read about, but you will.

    These are usually referred to as "get/set" pairs. They're typically used to control access to private data, and indeed, to new C++ programmers they seem superfluous, like a formality without meaning.

    In "real world" C++ work, it's much more than a formality. There are situations where the data in a class is treated more like a public record, just fields that need no control access, like structs were typically used in C.

    Private data has much more important meaning, though. It represents values that are internal to the operation of the object, and should not be exposed to the outside world directly. Simple example would be an angle value. Angles should be limited to a single rotation. In degrees you might establish the convention that they be limited to -180 + 180, or the limit of radians. Whatever that limit is, you can't enforce that if the double or float representing the angle is public - code could change it to anything. By forcing the access through a "get/set" pair, the "set" function can control the input value (wrapping in the case of angles).

    As with most study example material, the illustration hardly does the technique justice. In real production code, the concept is even more important that examples suggest.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  4. #4
    Join Date
    Jun 2009
    Location
    Kansas
    Posts
    3

    Re: return statemens in member functions

    Thanks for the quick responses! We're supposed to be covering chaps 1-7 in this class, and I'm reading 10 and 12 to keep up with the teacher (very difficult to understand). So, I appreciate you guys taking the time. I'm going to research get/set more thoroughly now

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center