标题:关于重载<<运算符的问题
只看楼主
子楠
Rank: 3Rank: 3
来 自:武汉
等 级:论坛游侠
帖 子:111
专家分:164
注 册:2013-4-9
结帖率:100%
 问题点数:0 回复次数:0 
关于重载<<运算符的问题
#include <iostream>
using namespace std;
class Clock
{
public:
    Clock(int hour=0, int minute=0, int second=0):hour(hour), minute(minute), second(second) {}
    //void showTime();
    Clock&  operator++ ();
    Clock operator++ (int);
    friend ostream & operator<< (ostream &out,const Clock &c);
private:
    int hour, minute, second;
};

//void Clock::showTime()
//{
//    cout<<hour<<":"<<minute<<":"<<second<<endl;
//}
ostream & operator<< (ostream &out ,const Clock &c)
{
    out<<c.hour<<":"<<c.minute<<":"<<c.second<<"\n";
    return out;
}
Clock &Clock::operator++()
{
    second++;
    if(second>=60)
    {
        minute++;
        second = second%60;
        if(minute>=60)
        {
            hour = (hour+1)%24;
            minute = minute%60;
        }
    }
    return * this;
}
Clock Clock::operator++(int)
{
    Clock old = * this;
    ++ ( * this);
    return old;
}

int main()
{
    Clock c(23,58,59);
    cout<<c;
    //c.showTime();
    //(c++).showTime();
    //(++c).showTime();
    return 0;
}
程序有错,求大神解
搜索更多相关主题的帖子: private namespace void include public 
2013-05-31 14:08



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-412669-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.448221 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved