8.3. Accessing instance variables

You can read the values of an instance variable using the same syntax we used to write them:

int x = blank.x;

The expression blank.x means “go to the object named blank and get the value of x.” In this case we assign that value to a local variable named x. Notice that there is no conflict between the local variable named x and the instance variable named x. The purpose of the member access operator is to identify which variable you are referring to unambiguously.

You can use the member access operator as part of any C++ expression, so the following are legal.

cout << blank.x << ", " << blank.y << endl;
double distance = sqrt(blank.x * blank.x + blank.y * blank.y);

In the active code below, we access the instance variables of point object blank and output their values. Next, we display the distance from the origin.

You have attempted of activities on this page