posts - 7, comments - 13, trackbacks - 0, articles - 37
   :: 首页 :: 新随笔 :: 联系 ::  :: 管理

c++友元函数实现例子

Posted on 2009-05-12 22:25 岁月流逝 阅读(660) 评论(0)  编辑 收藏 引用
#include using namespace std; class Clock { public: Clock(int NewH=0,int NewM=0,int NewS=0); void ShowTime(); // void operator ++(); friend void operator ++(Clock c1); // void operator ++(int); friend void operator ++(Clock c1,int); private: int Hour,Minute,Second; }; Clock::Clock(int NewH,int NewM,int NewS) { if(NewH>=0&&NewH <24&&NewM>=0&&NewM <60&&NewS>=0&&NewS <60) { Hour=NewH; Minute=NewM; Second=NewS; } else { cout <<"Time error!" <=60) if(c1.Second>=60) { // Second=Second-60; c1.Second=c1.Second-60; // Minute++; c1.Minute++; // if(Minute>=60) if(c1.Minute>=60) { // Minute=Minute-60; c1.Minute=c1.Minute-60; // Hour++; c1.Hour++; // Hour=Hour%24; c1.Hour=c1.Hour%24; } } cout <<"++myClock"; } //void Clock::operator ++(int) void operator ++(Clock c2,int) { // Second++; c2.Second++; // if(Second>=60) if(c2.Second>=60) { // Second=Second-60; c2.Second=c2.Second-60; // Minute++; c2.Minute++; // if(Minute>=60) if(c2.Minute>=60) { // Minute=Minute-60; c2.Minute=c2.Minute-60; // Hour++; c2.Hour++; // Hour=Hour%24; c2.Hour=c2.Hour%24; } } cout <<"myClock++"; } int main() { Clock myClock(23,59,59); cout <<"First time output:"; myClock.ShowTime(); myClock++; myClock.ShowTime(); ++myClock; myClock.ShowTime(); }

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理