The example plausibly separates the setting and extracts the values, because there are more between them than you see in this main function. If this does not happen, you are absolutely right that you should combine them and even exclude the class in favor of a free function.
For example, the code might look something like this:
void example(Test obj) { if (something) { process(obj.tester()); } } int main() { int x, y;
Why do we need private members?
Not. They are more documentation than anything else. However, accessibility (public / protected / private) is documentation that is checked by the compiler. This check is useful mainly for encapsulating values, but encapsulating is much more than just noticing something non-public. For example, if you (from a public method) return a reference to a non-public data element, you have associated the implementation of this element with the public interface of the class.
When and how should destructors be used?
If you need special logic of destruction. Read about Rule Three . Write them as if they were a class method with the name "~" plus the class name, without parameters and omitting the return type (even invalid), similarly for ctors.
How do I write my classes?
You do not need to define methods outside the class. In fact, implement all the methods inside the class to start with it, and then move the methods as necessary. Itβs much harder to mess up when you learn the important basics of the language and are much more convenient for the types of classes that you will write initially. Use ctor initializers when they are as possible as assigning them in a ctor package.
struct Test { Test(int x, int y)
Note. I used "struct" to declare this class instead of "class". The result is identical, except that I skip the βpublicβ on any base classes (they are not here, but the databases are more often public than not), and the availability of elements by default is also βpublicβ: this leads to reduction and a little more clean up the code.