首页 > 编程 > C++ > 正文

C++运算符重载例子代码

2019-11-10 18:02:04
字体:
来源:转载
供稿:网友
#include <iostream>using namespace std;class Time{PRivate:    int h,m,s;public:    Time();    Time(int mh,int mm,int ms);    //重载++运算符    Time Operator++(int);    void ShowMe();    friend ostream& operator <<(ostream& output,Time &t);};ostream& operator<<(ostream& output,Time& t){    output<<t.h<<":"<<t.m<<":"<<t.s<<endl;    return output;}Time Time::operator++(int){    //保存原值    Time tmp = *this;    //将原值加    s = s+1;    if(s == 60)    {        s = 0;        m = m+1;        if(m==60)        {            m = 0;            h=h+1;            if(h == 24)            {                h = 0;            }        }    }    //返回保存的那个原值    return tmp;}Time::Time(){    h=0;    m=0;    s=0;}Time::Time(int mh,int mm,int ms){    h=mh;    m=mm;    s=ms;}void Time::ShowMe(){    cout<<h<<":"<<m<<":"<<s<<endl;}int main(){    Time t(23,58,59);    t++;    //(t++).ShowMe();    cout<<t;    return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选