4-2 电子时钟中的运算符重载

4-2 电子时钟中的运算符重载

#include<iostream>
#include<cstring>
using namespace std;
int a,b,c;
class Time
{
public :
    Time (){hour=0;minute=0;sec=0;}
    Time(int a,int b,int c):hour(a),minute(b),sec(c){}
    Time operator ++();
    friend void putin(Time &t);
   friend  bool operator==( Time &a, Time &b) ;
   friend int getsum(Time &a) ;
   friend  bool operator >( Time &a, Time &b) ;
   void display();
private:
    int hour;
    int minute;
    int sec;
};
 bool operator==( Time &a, Time &b)
    {
        if(a.hour==b.hour&&a.minute==b.minute&&a.sec==b.sec)return 1;
        return 0;
    }
    int getsum(Time &a)
    {
        return a.hour*3600+a.minute*60+a.sec;
    }
    bool operator >( Time &a, Time &b)
    {
        if(getsum(a)>getsum(b))return 1;
        return 0;
    }
void putin(Time &t)
{
    cin>>t.hour>>t.minute>>t.sec;
    if(t.hour<0||t.hour>12)t.hour=12;
     if(t.sec<0||t.sec>59)t.sec=0;
      if(t.minute<0||t.minute>59)t.minute=0;
}
Time Time::operator++()
{
    if(++sec>=60)
    {
        sec-=60;


    if(++minute>=60)
    {
        minute-=60;
        ++hour;
    }
    }
    return *this;
}
void Time::display()
{
    if(hour<10)
        cout<<"0"<<hour<<":";
     else
        cout<<hour<<":";
    if(minute<10)
        cout<<"0"<<minute<<":";
    else
        cout<<minute<<":";
    if(sec<10)
        cout<<"0"<<sec;
    else
        cout<<sec;
    cout<<endl;
}
int main()
{
    Time t1,t2;
    putin(t1);
    putin(t2);
    if(t1>t2)
      cout<<"The begin time is not earlier than the end time!"<<endl;
       else
    {
        while(!(t1>t2))
        {
            t1.display();
            ++t1;
        }
    }
    return 0;
}
下面是一个基本的 C++ 电子时钟实现,其重载了 + 和 - 运算符: ```c++ #include <iostream> using namespace std; class Clock { private: int hour, minute, second; public: Clock(int h = 0, int m = 0, int s = 0) { hour = h; minute = m; second = s; } void display() { cout << hour << ":" << minute << ":" << second << endl; } Clock operator+(const Clock& c) const { int h = hour + c.hour; int m = minute + c.minute; int s = second + c.second; if (s >= 60) { s -= 60; m++; } if (m >= 60) { m -= 60; h++; } if (h >= 24) { h -= 24; } return Clock(h, m, s); } Clock operator-(const Clock& c) const { int h = hour - c.hour; int m = minute - c.minute; int s = second - c.second; if (s < 0) { s += 60; m--; } if (m < 0) { m += 60; h--; } if (h < 0) { h += 24; } return Clock(h, m, s); } }; int main() { Clock c1(10, 30, 45), c2(12, 50, 15); cout << "c1 = "; c1.display(); cout << "c2 = "; c2.display(); Clock c3 = c1 + c2; cout << "c3 = "; c3.display(); Clock c4 = c2 - c1; cout << "c4 = "; c4.display(); return 0; } ``` 在上面的代码,Clock 类具有小时、分钟和秒属性,并且有一个默认构造函数。display() 函数用于显示当前时间。operator+() 和 operator-() 分别重载了 + 和 - 运算符,以使 Clock 对象可以相加和相减。 在主函数,我们创建了两个 Clock 对象 c1 和 c2,并显示了它们的值。然后我们通过重载的 + 运算符将它们相加,将结果存储在 c3 ,并将其显示出来。接下来,我们通过重载的 - 运算符将 c2 从 c1 减去,将结果存储在 c4 ,并将其显示出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值