自增\自减运算符的重载

自增运算符++ ,自减运算符–有前置/后置之分,为了区分所重载的是前置运算符还是后置运算符,C++规定:
前置运算符作为一元运算符重载
//重载为成员函数时
T & operator ++()
T & operator --()
//重载为全局函数时
T & operator ++ (T&)
T & operatot -- (T&)
后置运算符作为二元运算符重载,多写一个没用的参数
//重载为成员函数时
T & operator ++(int n)
T & operator --(int n)
//重载为全局函数时
T & operator ++(T &,int n)
T & operator --(T &,int n)
但是在没有后置运算符重载而有前置运算符重载的情况下,在VS中,obj++也调用前置重载,在dev中,则obj++编译出错
class CDemo{
    int n;
    public:
    CDemo(int i=0):n(i){}
    CDemo operator++(int);//后置
    CDemo &operator++();//前置
    friend CDemo operator--(CDemo &, int);//全局后置
    friend CDemo &operator--(CDemo &);//全局前置
    friend ostream & operator<<(ostream &os,const  CDemo &d);
};
ostream &operator <<(ostream & os,const CDemo & d){
    os << d.n;
    return os;
}
CDemo CDemo::operator++(int){//成员后置
    CDemo tmp(*this);
    n++;
    return tmp;
}
CDemo & CDemo::operator++(){//成员前置
    n++;
    return *this;
}
CDemo operator--(CDemo & d,int){//全局后置
    CDemo tmp(d);
    d.n--;
    return tmp;
}
CDemo & operator--(CDemo & d){//全局前置
    d.n--;
    return d;
}

int main() {
    CDemo d(5);
    cout << (d++) << ',';//等价于d.operator++(0),成员
    cout << d << ',';
    cout << (++d) << ',';//等价于d.operator++(),成员
    cout << d << ',';
    cout << endl;
    cout << (d--) << ',';//等价于operator--(d,0),全局
    cout << d << ',';
    cout << (--d) << ',';//等价于operator--(d),全局
    cout << d << ',';
    cout << endl;
    return 0;
}
写<<运算符重载的时候发现一个问题,就是第二个参数对CDemo 的引用必须是常引用,不加const编译不过,使用的是dev,有点纳闷
ostream &operator <<(ostream & os,CDemo & d){
    os << d.n;
    return os;
}
  • 1
    点赞
  • 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、付费专栏及课程。

余额充值