Previous: Introduction Up: Data Transfer Functions Next: Structures with Local Pointers
class Point {
float x_coordinate;
float y_coordinate;
friend CCVoid& operator<<(CCVoid&,const Point&);
friend CCVoid& operator>>(CCVoid&,Point&);
friend ostream& operator<<(ostream&,const Point&);
friend istream& operator>>(istream&,Point&);
};
CCVoid& operator<<(CCVoid& v,const Point& p_out)
{
v << p_out.x_coordinate << p_out.y_coordinate;
return v;
}
ostream& operator<<(ostream& v, const Point& p_out)
{
v << p_out.x_coordinate << p_out.y_coordinate;
return v;
}
CCVoid& operator>>(CCVoid& v,Point& p_in)
{
v >> p_in.x_coordinate >> p_in.y_coordinate;
return v;
}
istream& operator>>(istream& v, Point& p_in)
{
v >> p_in.x_coordinate >> p_in.y_coordinate;
return v;
}
Notice the similarities between the data transfer functions and the input/output stream functions for class Point. The data transfer functions are declared friends of Point so that they may access the private data members of Point.
Both istream& operator>> and CCVoid& operator>> operate on an object for which memory has already been allocated and initialized. The compiler invokes the default constructor to initialize an object, and then invokes CCVoid& operator>> with the initialized object. Thus, a default constructor must be defined for each type. Like C++, CC++ will automatically generate a default constructor for a type if there is no other constructor defined for that type.