首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

iostream重载解决思路

2012-03-01 
iostream重载CODE:[Copytoclipboard]classDate{intmonthintdayintyearinthourintminuteintsecond//a

iostream重载
CODE:
[Copy   to   clipboard]
class   Date{
    int   month;
    int   day;
    int   year;
    int   hour;
    int   minute;
    int   second;
//   and   the   get   set   function
}

//   the   iostream
//   format:   mm/dd/yyyy   hh:mm:ss   \n

//mm/dd/yyyy   hh:nn:ss   \n
ostream   &operator < <(ostream&   out,   const   Date&   date){
    out   < <   date.getMonth()   < <   "/ "
                    < <   date.getDay()   < <   "/ "
                    < <   date.getYear()   < <   "   "
                    < <   date.getHour()   < <   ": "
                    < <   date.getMinute()   < <   ": "
                    < <   date.getSecond()   < <   "   "   < <   endl;
}

istream   &operator> > (istream&   in,   Date&   date){
    char   temp;
    int   month,   day,   year,   hour,   minute,   second;
    in   > >   month   > >   temp
                  > >   day   > >   temp
                  > >   year   > >   temp
                  > >   hour   > >   temp
                  > >   minute   > >   temp
                  > >   second   > >   temp;

    date.setMonth(month);
    date.setDay(day);
    date.setYear(year);
    date.setHour(hour);
    date.setMinute(minute);
    date.setSecond(second);
    cout   < <   endl;
}
这样子为什么不行呢   呵呵   求助

//   test   code
        stringstream   s;
        s   < <   "8/17/1976   1:2:3\n ";
     
        Date   d;
        s   > >   d;

        if   (d.getMonth()   ==   8   &&
                d.getDay()   ==   17   &&
                d.getYear()   ==   1976   &&
                d.getHour()   ==   1   &&
                d.getMinute()   ==   2   &&
                d.getSecond()   ==   3)   {


     
                return   true;      
        }
        else   {
                return   false;
        }

[解决办法]
> > year > > hour > > temp

[解决办法]
stringstream
和ostream,istream的继承关系

热点排行
Bad Request.