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
    Jun 2017
    Posts
    13

    Getters and Setters

    I still can't seem to grasp the whole setters and getters in C++

    This is the assignment:
    Code:
    In this file, you declare a class named MyString.
    – This class contains 3 private attributes, str, size, and strCount.
    – str is a char* that points to a dynamic array of characters.
    – size is an integer that contains the current size of the string.
    – strCount is a static member variable that keeps track of the number of MyString objects that currently
    exist in memory. Its value is initially 0 before any MyString object is created.
    – Define public getters and setters for the 2 instance variables, as well as a getter for the static variable.

    This is the code that I have so far,
    Code:
    #define MYSTRING_H
    #ifndef MYSTRING_H
    
    class MyString {
    
    private:
      char* str[];
      int size;
      static strCount = 0;
    
    public:
      void setStr(char * str) {
        str = s;
      }
      void getStr() {
        return str;
      }
      int setSize() {
        return size;
      }
      void getSize(int size) {
        size = sz;
      }
    };
    I would love if someone can point me in the right direction and help me define the static and char* getter and setters because I am having lots of trouble.

  2. #2
    Join Date
    Feb 2017
    Posts
    165

    Re: Getters and Setters

    Quote Originally Posted by Semirxbih View Post
    I still can't seem to grasp the whole setters and getters in C++
    In its simplest form a getter and a setter are a pair of class functions which provide public access to a private class variable.

    Say you have an object (obj) of a class defining a variable (var) of type T. If var is public you would access it like this

    Code:
    T v = obj.var; // get var directly
    obj.var = v; // set var directly
    If var instead is private but there are a getter and a setter available you would do this to the same effect,

    Code:
    T v = obj.getVar(); // get var indirectly
    obj.setVar(v); // set var indirectly
    For this to work the getter/setter must look like this,
    Code:
    T getVar () { // function to get var
        return var;
    }
    void setVar(T v) { // function to set var
        var = v;
    }
    Note that getters/setters should not be applied routinely. It is not that all private variables should have a public getter/setter pair. The purpose of getters/setters is to increase the level of abstraction but if overused without care they instead decrease abstraction and for this reason they are controversial.

    Also getters/setters are not C++ specific. They're used in most languages and are even part of some languages (where they then appear as a language element usually called properties).
    Last edited by wolle; July 13th, 2017 at 04:32 AM.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,835

    Re: Getters and Setters

    There are some issues with the code.

    Code:
    #define MYSTRING_H
    #ifndef MYSTRING_H
    These are in the wrong order! For this to do as required, the test comes first before the define! Also, where is the closing #endif statement?

    Code:
     static strCount = 0;
    The type of strCount is missing. In this case type int is assumed but should be stated

    Code:
     static int strCount = 0;
    also you can't initialise a non-const static variable in the class. It has to be declared in the class and defined outside like

    Code:
    static int strCount;
    and then outside of the class you have

    Code:
    int MyString::strCount = 0;
    Code:
    char* str[];
    This is trying to define an array of unknown size of type char *. From the spec, don't you mean
    Code:
    char* str;
    Code:
     void setStr(char * str) {
        str = s;
      }
    Where does variable s come into this?? Don't you mean
    Code:
     void setStr(char * s) {
        str = s;
      }
    also as the contents of the memory pointed to by s doesn't change and the value of the pointer doesn't change, then a better definition could be

    Code:
    void setStr(const char * const s) {
    Code:
    void getStr() {
        return str;
      }
    This is returning a value of type char *, but the function is defined as a void return type. Don't you mean

    Code:
    char* getStr() const {
        return str;
      }
    also getter functions should be defined as const as they don't change the values of class variables.

    Code:
     int setSize() {
        return size;
      }
    The code is for a getter, so don't you mean

    Code:
     int getSize() const {
        return size;
      }
    likewise,

    Code:
    void getSize(int size) {
        size = sz;
      }
    is for a setter, but where is sz defined?? Don't you mean

    Code:
    void setSize(int sz) {
        size = sz;
      }
    You also need to provide the getter for strCount.

    Note that for this exercise, use of setters for the variables is not a good idea and wouldn't be used in practice. Only getters would be used as these variables would be maintained by the class. As Wolle says in post #2, use of getters/setters should not be applied routinely (my highlight).
    Last edited by 2kaud; July 13th, 2017 at 05:31 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.4.4)

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 a Codeguru.com survey!


On-Demand Webinars (sponsored)