将普通函数声明为友元函数
1 #include <iostream> 2 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 class Time 6 { 7 public: 8 Time(int,int,int); 9 friend void display(Time&); 10 private: 11 int hour; 12 int minute; 13 int sec; 14 }; 15 16 Time::Time(int h,int m,int s) 17 { 18 hour=h; 19 minute=m; 20 sec=s; 21 } 22 23 void display(Time&t) 24 { 25 cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl; 26 } 27 int main(int argc, char** argv) { 28 Time t1(10,13,56); 29 display(t1); 30 return 0; 31 }