Trying to use a member function without an object is a syntax error. There is no getX() function, there is only the getX() function that is a member of the Point class. (Point::getX() is the full name of this function - βthe getX that is a part of Pointβ.)
So when we say p1.getX() we are asking that object to execute the getX() code. When that code refers to return m_x;, it means βthe m_x value of p1β. Later on, if we say p2.getX(), the line return m_x;, it now means βthe m_x value of p2β.
Run this Codelens sample. Pay special attention each time it enters the getX function. You will see a stack frame for Point::getX(), and in that stack frame, you will see a variable called this that points at the object that is running the code. During the first call to Point::getX(), it will point to p1 since we called p1.getX() on line 23. Then, on line 26 when we call p2.getX(), this will be pointing at p2.
We will learn more about exactly what this is in SectionΒ 18.9. For now, you can think of it as an alias for βthe object running the codeβ. When we say m_x in a member function, it is understood to mean this->m_x or βthe m_x variable of the object running this codeβ.
The class itself does not have a m_red value. The class is just a blueprint for creating objects. The m_red variable is a member of each instance of the class.
If we want to provide the ability for outside code to modify a member variable, we can add a setter function. These are functions that take a value and use that to set a member variable. This version of the Point class adds a setter for m_x:
As with getters, setters provide a limited way for outside code to manipulate the object. Outside code canβt directly change m_x, it has to use the interface to ask a given point to please make the change to itself. This means that we have loose coupling and also that the class can βprotect itselfβ from unsafe or unwanted changes.
Say you are creating a Person class and you want to make sure that the age of a person never gets set to a negative value. You could design the class to prevent that from happening:
When other code tries to set a negative age, the Person class ignores that request and prints an error message. (It would make even more sense in a real program to throw an exception there.)
The person programming Person is the expert who knows all the important ins and outs of the Person class. They know that a negative value is going to cause problems later. The person programming main does not realize that issue even exists. If they could set `p1.m_age = -5;` directly, they could end up causing the program to do something horrible later on.
By forcing others to use the public interface and then preventing unwanted modifications in that interface, the programmer of the Point class is able to limit the potential for such issues.
Setters are a special kind of mutator function. A mutator is any function that changes the state of an object. While a setter sets a member to a particular value, another kind of mutator might set more than one value or change a variable in a way that doesnβt allow outside code to set a particular value.
In main we want to create a Circle, set the radius to 2.4 and output the radius. Then change the radius to 3.6 and output the new radius. We want to use the public interface of the Circle class and not depend on the private details of the class.