| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 705 人关注过本帖
标题:c++运算符重载
只看楼主 加入收藏
guchao2009
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:101
专家分:106
注 册:2009-4-13
结帖率:77.78%
收藏
已结贴  问题点数:10 回复次数:2 
c++运算符重载
#include<iostream.h>
class time
{
private:
    int hour;
    int minute;
    int second;
public:
    time();
    time(int h,int m,int s);                     //初始化
    time operator++(time &x);                   //完成秒数上加一的功能
    friend ostream &operator<<(ostream &out,time c);
};

time::time()
{
    hour=12;
    minute=0;
    second=0;
}

time::time(int h,int m,int s)
{
    hour=h;
    minute=m;
    second=s;
}

time operator++(time &x);
{
    x.second+=1;
    return x;
}

ostream &operator<<(ostream &out,time c)
{
    if(c.minute<10&&c.second>=10)
        out<<c.hour<<":"<<"0"<<c.minute<<":"<<c.second<<endl;
    else if(c.minute<10&&c.second<10)
        out<<c.hour<<":"<<"0"<<c.minute<<":"<<"0"<<c.second<<endl;
    else if(c.minute>=10&&c.second>=10)
        out<<c.hour<<":"<<c.minute<<":"<<c.second<<endl;
    else
        out<<c.hour<<":"<<c.minute<<":"<<"0"<<c.second<<endl;
    return out;
}

void main()
{
    time t1(13,25,36),t2;
    cout<<t1<<endl;
    cout<<t2<<endl;
}
搜索更多相关主题的帖子: 运算符 重载 
2009-11-29 20:09
guchao2009
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:101
专家分:106
注 册:2009-4-13
收藏
得分:0 
回复 楼主 guchao2009
补充一下;
错误是
--------------------Configuration: 3 - Win32 Debug--------------------
Compiling...
3.cpp
E:\计算机语言\c++\c++上机作业\运算符重载\3.cpp(11) : error C2807: the second formal parameter to postfix 'operator ++' must be 'int'
E:\计算机语言\c++\c++上机作业\运算符重载\3.cpp(30) : error C2447: missing function header (old-style formal list?)
执行 cl.exe 时出错.


错误看不懂······
2009-11-29 20:10
smd151858485
Rank: 2
等 级:论坛游民
帖 子:1
专家分:10
注 册:2009-11-30
收藏
得分:10 
帮你看了一下希望能有帮助.
(1)
time operator++(time &x);                   //完成秒数上加一的功能
(2)
time operator++(time &x);
{
    x.second+=1;
    return x;
}
//改成
(1)
time& operator++();
(2)
time& time::operator ++()
{
    this->second++;
    return *this;   
}
    运算符++和—有前置和后置两种形式,如果不区分前置和后置,则使用operator++( )或operator--( )即可;否则,要使用operator++( )或operator--( )来重载前置运算符,使用operator++(int)或operator--(int)来重载后置运算符,调用时,参数int被传递给值0。
2009-11-30 01:32
快速回复:c++运算符重载
数据加载中...
 
   



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

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