#include class Count { friend Count add( Count &, Count &); // friend declaration friend bool equal_to( Count &, Count &); public: Count() { x = 0; } // constructor void print() const { cout << x << endl; } // output void setX( int ); // function of Count to set X private: int x; // data member }; // setX is a function of class Count void Count::setX( int val ) { x = val; // legal: setX is a function of Count, // so it can access the private data member } // add is declared as a friend function of Count Count add( Count &c1, Count &c2) { Count temp; temp.x = c1.x + c2.x; return temp; } bool equal_to( Count &c1, Count &c2) { if ( c1.x == c2.x) return true; else return false; } int main() { Count counter1, counter2, result; cout << "counter1.x after instantiation: "; counter1.print(); cout << "counter2.x after instantiation: "; counter2.print(); cout << "counter1.x after call to setX function: "; counter1.setX( 8 ); // set x with a friend counter1.print(); cout << "counter2.x after call to setX function: "; counter2.setX( 5 ); // set x with a friend counter2.print(); cout << "result.x after add counter1 and counter2 by calling add friend function: "; result = add(counter1, counter2); result.print(); if (equal_to(counter1, counter2)) cout << "counter1.x is equal to counter2.\n"; else cout << "counter1.x is not equal to counter2.\n"; return 0; }