自增,自减运算符的重载

前置++,前置--作为一元运算符重载

  • 重载为成员函数:T &operator++()
  • 重载为全局函数:T &operator++(T2)

 

后置++,后置--作为二元运算符重载,多写一个没用的参数

  • 重载为成员函数:T &operator++(int)
  • 重载为全局函数:T &operator++(T2, int)

 

#include <iostream>
using namespace std;

class CDemo{
private:
    int n;

public:
    CDemo(int i=0) : n(i) { }

    // 重载为成员函数
    CDemo &operator++();        //前置++作为一元运算符重载
    CDemo &operator++(int k);   //后置++作为二元运算符重载,多写一个无用参数

    //重载为全局函数
    friend CDemo &operator--(CDemo &d);         //前置--
    friend CDemo &operator--(CDemo &d, int);    //后置--

    operator int() { return n; }    //重载强制类型转换运算符
};

CDemo &CDemo::operator++()
{
    this->n++;
    return *this;
}

CDemo &CDemo::operator++(int k)
{
    CDemo temp(*this);
    this->n++;
    return temp;
}

CDemo &operator--(CDemo &d)
{
    d.n--;
    return d;
}

CDemo &operator--(CDemo &d, int)
{
    CDemo temp(d);
    d.n--;
    return temp;
}

int main()
{
    CDemo arr;
    cout << (++arr) << endl;    //1
    cout << (--arr) << endl;    //0
    cout << (arr++) << endl;    //0
    cout << (arr--) << endl;    //1

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 中,我们可以通过重载运算符来实现日期之间的差值计算。具体步骤如下: 1. 定义一个 Date 类,包含年、月、日等属性,并实现自增自减运算符重载。 ```c class Date { public: Date(int year, int month, int day): year_(year), month_(month), day_(day) {} Date& operator++(); // 自增运算符重载 Date operator++(int); // 后置自增运算符重载 Date& operator--(); // 自减运算符重载 Date operator--(int); // 后置自减运算符重载 private: int year_; int month_; int day_; }; ``` 2. 在重载运算符的实现中,我们需要注意日期的进位和借位问题。例如,当月份为 12 时,年份需要进位;当日期为 1 时,月份需要借位。 ```c Date& Date::operator++() { day_++; if (day_ > days_of_month(year_, month_)) { day_ = 1; month_++; if (month_ > 12) { month_ = 1; year_++; } } return *this; } Date Date::operator++(int) { Date temp(*this); ++(*this); return temp; } Date& Date::operator--() { day_--; if (day_ < 1) { month_--; if (month_ < 1) { month_ = 12; year_--; } day_ = days_of_month(year_, month_); } return *this; } Date Date::operator--(int) { Date temp(*this); --(*this); return temp; } ``` 3. 定义一个 days_between 函数,用于计算两个日期之间的天数差值。该函数可以通过调用重载自增自减运算符来实现。 ```c int days_between(const Date& d1, const Date& d2) { Date d(d1); int count = 0; while (d != d2) { if (d2 > d) { ++d; count++; } else { --d; count--; } } return count; } ``` 4. 注意,我们还需要实现一个 days_of_month 函数,用于计算给定年份和月份的天数。 ```c int days_of_month(int year, int month) { static const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 && is_leap_year(year)) { return 29; } return days[month - 1]; } bool is_leap_year(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } ``` 完整代码如下: ```c #include <iostream> class Date { public: Date(int year, int month, int day): year_(year), month_(month), day_(day) {} Date& operator++(); // 自增运算符重载 Date operator++(int); // 后置自增运算符重载 Date& operator--(); // 自减运算符重载 Date operator--(int); // 后置自减运算符重载 bool operator==(const Date& other) const { return year_ == other.year_ && month_ == other.month_ && day_ == other.day_; } bool operator!=(const Date& other) const { return !(*this == other); } bool operator<(const Date& other) const { if (year_ < other.year_) return true; if (year_ > other.year_) return false; if (month_ < other.month_) return true; if (month_ > other.month_) return false; return day_ < other.day_; } bool operator>(const Date& other) const { return other < *this; } bool operator<=(const Date& other) const { return !(other < *this); } bool operator>=(const Date& other) const { return !(*this < other); } private: int year_; int month_; int day_; }; int days_of_month(int year, int month) { static const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 && is_leap_year(year)) { return 29; } return days[month - 1]; } bool is_leap_year(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } Date& Date::operator++() { day_++; if (day_ > days_of_month(year_, month_)) { day_ = 1; month_++; if (month_ > 12) { month_ = 1; year_++; } } return *this; } Date Date::operator++(int) { Date temp(*this); ++(*this); return temp; } Date& Date::operator--() { day_--; if (day_ < 1) { month_--; if (month_ < 1) { month_ = 12; year_--; } day_ = days_of_month(year_, month_); } return *this; } Date Date::operator--(int) { Date temp(*this); --(*this); return temp; } int days_between(const Date& d1, const Date& d2) { Date d(d1); int count = 0; while (d != d2) { if (d2 > d) { ++d; count++; } else { --d; count--; } } return count; } int main() { Date d1(2020, 1, 1); Date d2(2020, 1, 10); std::cout << "Days between " << d1 << " and " << d2 << ": " << days_between(d1, d2) << std::endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值