in draw(int x, int y)x argument hides class variable with same name. So all instances of x in method will refer to argument, not member variable. To access it we should specify it usind pointer member access: this->x
Alternatively you could initialize it in initialization list: draw(int x, int y): x(x), y(y) {} or rename arguments, so x would refer to class member again:
struct MyClass{
int myVar;
};
int main(){
Myclass object;
MyClass* object_ptr = &object;
//The following mean the same thing
(*object_ptr).myVar = 5;
object_ptr->myVar = 5;
}
Cycle::Cycle(int x, int y, int radius) : Draw(x, y) {
this->radius = radius;
}
"radius" refers to a new value to be assigned the object's radius member variable.
"this->radius" refers to the current object's member radius variable.
Although the two variables have the same name, they are different variables entirely. Using "this->" will disambiguate the two variables. Another approach is this (but I could be wrong):
1 2 3 4 5 6
Cycle::Cycle(int x, int y, intnewradius) : Draw(x, y) {
radius = newradius;
/*
Now radius refers to member variable and newradius refers to parameter
*/
}