C++练笔例子5操作符重载

本文介绍了一个C++时间类的设计与实现,重点讲解了如何通过运算符重载来支持时间类之间的加减等操作,并展示了如何使用友元函数来定制输出。此外,文章还讨论了不同类型转换在运算符重载中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>

class Mytime
{
      private:
              int hours;
              int minutes;
      public:
             Mytime(int h = 0, int m = 0):hours(h),minutes(m){}
            
             Mytime(const Mytime &other):hours(other.hours), minutes(other.minutes){}
            
             void addMin(int m)
             {
                  minutes += m;
                  hours += minutes / 60;
                  minutes %= 60;
             }
            
             void addHr(int h)
             {
                  hours += h;
             }
            
             void reset(int h, int m)
             {
                  hours = h;
                  minutes = m;
             }
            
             Mytime operator+(const Mytime &other) const
             {
                    Mytime sum;
                    sum.minutes = other.minutes + minutes;        
                    sum.hours = other.hours + hours + sum.minutes / 60;
                    sum.minutes %= 60;
                    return sum;
             }
            
             Mytime operator-(const Mytime &other) const
             {
                    Mytime diff;
                    int total_1, total_2;
                    total_1 = hours * 60 + minutes;
                    total_2 = other.hours * 60 + other.minutes;
                    diff.hours = (total_1 - total_2) / 60;
                    diff.minutes = (total_1 - total_2) % 60;
                    return diff;
             }
            
             Mytime operator*(double mult) const
             {
                    Mytime result;
                    long total_min = static_cast<long>(static_cast<double>(hours) * mult * 60.00 + static_cast<double>(minutes) * mult);
                    result.hours = total_min / 60;
                    result.minutes = total_min % 60;
                    return result;
             }
            
             friend  std::ostream & operator<<(std::ostream &c, Mytime &other)
             {
                    return c << "time: " << other.hours << ":" << other.minutes;
             }
                                           
};

 

int main()
{
     Mytime time(11,22);
     Mytime time2(10,21);

     time2 = time2 - time;

    std::cout << time2 << std::endl;
    system("pause");
    return 1;
}

 

     这个例子主要是对 重载运算符的练习,

 

     <C++PL> 11.2.1 二元和一元运算符       这一节值得看.

      

     <C++PL> 11.5.2 友元和成员 这一节也值得看

 

     貌似如果类有一个参数的构造函数, 并且非 explicit 的, 如 Mytime(int i), 那么如 +, - , * 等算术操作符一般定义为非成员函数, 因为操作对象能进行隐式类型转换,

 

     如Mytime operator+(const Mytime &lhs, const Mytime &rhs) ,

 

     有操作如: i + time1 将被隐式转换为 Mytime(i) + time1

 

     就不用重载Mytime operator+(int i, const Mytime &rhs) 了, 但是隐式转换有好处也有坏处. 可能 i + time1 是写错了的, 但是编译却通过了, 然后你又没发现, 以后找错误就麻烦了.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值