6.11 The class of points in a plane 177
- Setbequal to theycoordinate of pointA:
doubleb=A.gety();
- Print the coordinates of pointA:
cout<<A.getx()<<""<<A.gety()<<endl;
Note that the main function is not able to access directly the private
variablesxandy, and must rely on member functions to fetch them.
We can directly add two points A and B by overloading the + operator.
This is done by inserting the following declaration in the public section of the
class:
point operator + (point);
and defining the algebra class addition function:
point point::operator + (point B)
{
point add;
add.x=x+B.x;
add.y=y+B.y;
return add;
}
Once this is done, we can state in the main program:
point C;
C=A+B;
We can refer to the class members by pointers, as discussed in Section
6.10. Thus, if A and Z are defined members, including in the main code the
statements:
point * pntname;
pntname = &A;
cout<<pntname<<endl;
point Z = *pntname;
Z.print();
pntname->print();
prints on the screen:
0xbfed5168
0.3 0.4
0.3 0.4