getters and setters
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: getters and setters

  1. #1
    Join Date
    Apr 2001
    Posts
    80

    getters and setters

    Why do we need to define getters and setters?


  2. #2
    Join Date
    Jan 2001
    Location
    Germany
    Posts
    222

    Re: getters and setters

    Getters and setters provide a public interface to your class with which you can manipulate the values of member variables. Setters are used to control if new values are allright.
    Furthermore (and possibly more important), getters and setters are used to maintain the concept of encapsulation. You declare all member variables as private and provide getters and setters for them.

    ----------------
    You can contact me directly at Christoph.Schulze@gmx.co.uk
    Don't forget your parsley cause you can't eat your dog after having stolen him from some animal shelter and having drowned him in the Atlantic Ocean.
    Teamwork Software - Stuff That Does Something

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: getters and setters

    Captian Nuss is right. In general, the class should have full responsibility for its own data, and other classes may only request that a change be made or some data returned. So getters and setters, together with private data, give the class complete control. This means that as long the signature of the getters and setters doesn't change, the internal data can be managed however the class sees fit by using the getters and setters to 'translate'. For example, the data could be held as class data members in memory or stored only as rows in a database, but the user of the class doesn't have to know or care. The data storage method and/or type in the class can be changed without affecting any other code that uses that class.

    The getters and setters are also the place to do checks on requests for data change or return. Invalid change values can be rejected, user permissions can be checked for access, changes can be logged, and debugging code can help you track what's happening to the data.

    Dave

    To email me remove '_spamjam' from my email address
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center