1

I'm watching C++ tutorials on youtube, and I'm on a video titled Using Variables In Classes. In the video he explains that if we were to make variables public in classes, that it's bad programming. It'll work, but it's still bad programming. The program he wrote out consisted of a class with a private variable, and he used two functions to access the variable. Code looks like this:

#include <iostream>
#include <string>
using namespace std;

class MyClass {
public:
    void setName(string x)
    {
        name = x;
    }
    string getName()
    {
        return name;
    }
private:
    string name;


};

int main()
{
    MyClass TO;
    TO.setName("Taylor");
    cout << TO.getName();
}

My question is, why did we have to create a separate function to return name, instead of returning it in the first function? In my code, I returned name in the first function and it went well.

Tay
  • 101
  • 1
  • 1
  • 5
  • 1
    This is a well laid out question, but it's a bit unclear at the end. A "separate function to return a name" is what is called a "getter". Class members are typically private, so as to enforce encapsulation. So it might seem a bit redundant, but that's the way it's typically done. – mjuarez Feb 28 '15 at 04:52
  • Are you asking why `setName` doesn't return `name`? The question title could be improved... – Emil Laine Feb 28 '15 at 05:11
  • Yeah, that's what I'm asking. – Tay Feb 28 '15 at 05:29

4 Answers4

0

There might be cases when you want to set the variable, perform some other operations and then print the variable, hence the two functions.

If you just want to input the variable and print it, one function is enough.

Anuja
  • 124
  • 2
  • 6
  • "If you just want to input the variable and print it, one function is enough." - *input* and *print*, that seems to me like two functions, exactly like in the OP's code. – Emil Laine Feb 28 '15 at 05:02
0

Declare variables are private and use public functions are interface to set and get variables is a best practice. Why is not returning value from set function is an implication of 'separation of concern rule'. Please read this to get more info about it http://en.wikipedia.org/wiki/Separation_of_concerns

Steephen
  • 14,645
  • 7
  • 40
  • 47
0

Member functions that are prefixed with set are called setters, and member functions that are prefixed with get are called getters.

They can have other names too, of course, but those are the common naming conventions. The main idea is that

  • getters "get" (return) a variable from inside a class, while
  • setters "set" (change) a variable inside a class to some specific value.

For the rationale behind the use of getters and setters, see the answers here. They cover a lot of good points on why getters and setters are a good thing in object-oriented programming.

Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • He actually used the term setter and getter. I just didn't see the significance of them, but I understand it a bit more after looking at the link you provided. Thank you. – Tay Feb 28 '15 at 05:14
0

Because otherwise you wouldn't be able to write code to get the name if you don't happen to also be setting the name.

Consider trying to write a function to print the name of a MyClass passed as a parameter:

void printName(MyClass my_class){
    std::cout << my_class.getName() << "\n";
}

How would you write this with a setName function but no getName function?

In your trivial example you don't need getName but you don't actually need MyClass either. You could just write:

std::cout << "Taylor\n";

but clearly that is not the point of the tutorial.

Chris Drew
  • 14,926
  • 3
  • 34
  • 54