//example of friend function #include #include class line; class box { int color; // color of box int upx, upy; // upper left corner int lowx, lowy; // lower right corner public: friend int same_color(line l, box b); void set_color(int c); void define_box(int xl, int yl, int x2, int y2); void show_box(void); }; class line { int color; int startx, starty; int len; public: friend int same_color(line l, box b); void set_color(int c); void define_line(int x, int y, int l); void show_line(); }; // return true if line and box have same color. int same_color(line l, box b) { if(l.color==b.color) return 1; return 0; } void box::set_color(int c) { color = c; } void line::set_color(int c) { color = c; } void box::define_box(int x1, int y1, int x2, int y2 ) { upx = x1; upy = y1; lowx = x2; lowy = y2; } void box::show_box(void) { cout << "Box: "; cout << " color" << color << " upx" << upx << " upy" << upy; cout << " lowx" << lowx << " lowy" << lowy << endl; } void line::define_line(int x, int y, int l) { startx = x; starty = y; len = l; } void line::show_line(void) { cout << "Line: "; cout << " color" << color << " startx" << startx << " starty" << starty; } main(void) { box b; line l; b.define_box(10, 10, 15, 15); b.set_color(3); b.show_box(); l.define_line(2, 2, 10); l.set_color(2); l.show_line(); if(!same_color(l, b)) cout << endl << "not the same" << endl; cout << endl << "press a key" << endl; getch(); // now, make line and box the same color l.define_line(2, 2, 10); l.set_color(3); l.show_line(); ; if(same_color(l, b)) cout << "are the same color"; return 0; }