/* * Test Friend Function (TestFriend.cpp) */ #include using namespace std; class Point { // A friend function defined outside this class, but its argument of // this class can access all class members (including private members). friend void set(Point & point, int x, int y); // prototype private: int x, y; public: Point(int x = 0, int y = 0) : x(x), y(y) { } void print() const { cout << "(" << x << "," << y << ")" << endl; } }; // Friend function is defined outside the class void set(Point & point, int x, int y) { point.x = x; // can access private data x and y point.y = y; } int main() { Point p1; p1.print(); // (0, 0) set(p1, 5, 6); p1.print(); // (5, 6) }