Classes help, why does this work?

I'm learning about classes, and public & private. Why does this code work? We are using the string "test" in public before we actually declare it in private, a few lines down? Doesn't it read the code from top to bottom?

And am I understanding the workflow correctly?
1) I'm assigning the word "testing" to a function called set_name.
2) string "test" in public immediately becomes testing.
3) Now does the word "testing" also go into private at this point in time, or only when I use my_object.get_name () ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>

using namespace std;

class my_class
{
public:
    void set_name (string x)
    {
       test = x;
    }

    string get_name ()
    {
        return test;
    }

private:
    string test;
};

int main ()
{
    my_class my_object;
    my_object.set_name ("Testing...");
    cout << my_object.get_name () << endl;

    return 0;
}
Last edited on
I am not entirely sure what you are asking here but I will attempt to answer your question.

One thing you probably should understand about "public" and "private" (as well as "protected" for that matter) is that they are coding conventions. They are meant to assist the programmer in organizing their class data in a way that prevents accidental changing of data that should not be changed externally. Whether a function or variable is declared public or private does not affect the code that is generated by the compiler (except that the compiler won't let you compile code that accesses a private member externally)

The class member "test" is declared a private variable which is meant to only be accessible by the class itself. Because of this, the only way to set and retrieve this data is through public functions which access "test" for you (specifically set_name and get_name). Since these functions are both public and owned by the class in question, they are capable of both changing and giving you access to the private data.

The advantage of this is that the variable itself cannot be changed without the class writers permission (at least not through typically coded means). Lets say for example that you wanted to assign a value to this variable if and only if it has nothing in it. Your "set_name" function could then check the length of the string and only assign the value "x" if the length was zero.

The bottom line is that classes are meant for specific tasks. By giving the programmer the ability to control the class data explicitly (including what can be accessed directly), the programmer can code the class in a manner that is meant to do the task and minimize user mistakes.
Last edited on
Thank for for explaining in detail more about classes. I guess my 2 questions are:

1) Why are we declaring "test" to be a string in line 20 rather than line 11? And why does it work only in line 20?
2) When we are assigning the string "test" as "Testing..." in line 26, does that change happen in lines 9 & 11 only, or lines 9, 11, & 20?

I hope my questions are more clear this time.
Topic archived. No new replies allowed.