0

My teacher said that get and set functions are used in C++ as from them we can get value at runtime. Is it true? Aren't constructors used for this purpose to set value at runtime.

Mark
  • 21
  • 5
  • 2
    Getters and setters are used so you can read and write private variables. – erip Sep 29 '15 at 17:36
  • We can also read write private variables with any functions(we create to manipulate variables) or constructors. Than why to use get and set anything else can also be used – Mark Sep 29 '15 at 17:40
  • If the code using your class needs a value, it cannot access it without a getter. If the value needs to change during the execution of your program, a setter will be user. – erip Sep 29 '15 at 17:43
  • That is the line my teacher said, but still unable to understand can you kindly explain it through an example. " If the value needs to change during the execution of your program, a setter will be user." – Mark Sep 29 '15 at 17:46
  • 3
    This is a very confused question. Your teacher appears to be teaching you bad practices and treating it as if you have no choice. – Lightness Races in Orbit Sep 29 '15 at 17:46
  • 1
    I just want the explanation of the line by erip " If the value needs to change during the execution of your program, a setter will be user." – Mark Sep 29 '15 at 17:51
  • She was right, but its only the getter that gives access to get values. Setters are for assigning new values. Using getters and setters gives control on private data member access. – Walker Sep 29 '15 at 18:32

1 Answers1

1

Constructors are used to generate the "default" values in an object.
Once created, however, "getters" and "setters" are simply methods that allow you to access private members of that object. They're named as such because one name their methods getValue() to get a private variable named value from an object or setValue(int) to set it.

It is often also convenient to do error-checking in these methods, and to call a selection of "setters" in the constructor to save on code or easily create multiple constructors.

Here is an example:

class MyClass
{
private:
    int value;
public:
    MyClass(int);

    void setValue(int);
    int getValue();
};

MyClass::MyClass(int _value)
{
    setValue(_value); // pass to "setter"
}

void MyClass::setValue(int _value)
{
    if (_value > 0) // error-checking here
        value = _value;
    else
        value = 0;
}

int MyClass::getValue()
{
    return value;
}
ti7
  • 16,375
  • 6
  • 40
  • 68
  • 1
    Perhaps mentioning the advantages that a) all access is via methods to the object, b) limit access to the values (can be read, write, read/write). Also the value can be calculated or just passed a member variable - this enables one to modify the implementation – Ed Heal Sep 29 '15 at 17:42
  • 2
    Perhaps also mention that getters and setters are an anti-pattern and should be avoided. – Lightness Races in Orbit Sep 29 '15 at 17:47
  • 1
    @LightnessRacesinOrbit anti-pattern insomuch as `setFoo` and `getFoo` aren't realistic operations on `Foo`? – erip Sep 29 '15 at 17:50
  • 4
    @erip: Anti-pattern insomuch as `setX` and `getX` and `setY` and `getY` and `setZ` and `getZ` for each of your members as a rule is not all that different from just making your members public in the first place (and [the "you can add validation later" argument is a bit of a red herring](http://stackoverflow.com/a/12108025/560648)). You should have decent semantic operations like `move`, which we wouldn't really call a "setter". – Lightness Races in Orbit Sep 29 '15 at 17:59
  • How about making `value` unsigned and public? Wouldn't that have the same effect and save even more code? ;-) – Bo Persson Sep 29 '15 at 18:41
  • @BoPersson: Making it `unsigned` would prevent all error checking for positive values, though, i.e. you could no longer respond to something like `setValue(x - y)` when `x` is smaller than `y`. (Not that the error checking in this example is particularly useful...) – Christian Hackl Sep 29 '15 at 19:12
  • 1
    In addition to all other problems mentioned by others, `getValue` should be `const`. Generally, don't try to write Java in C++. – Christian Hackl Sep 29 '15 at 19:13
  • @LightnessRacesinOrbit: One minor advantage of getters/setters I can think of is the possibility of adding non-critical side effects such as logging every value write. – Christian Hackl Sep 29 '15 at 19:17
  • @ChristianHackl: That doesn't contradict my advice, in which you may still add non-critical side effects such as logging every value write. And, meh, set watchpoints. – Lightness Races in Orbit Sep 29 '15 at 19:40
  • @LightnessRacesinOrbit: Of course. I'm not trying to play the devil's advocate here, anyway :) – Christian Hackl Sep 29 '15 at 20:00