c++ 运算符的重载

1、实现代码如下:

#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<assert.h>
using namespace std;


class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
		:_year(year)
		, _month(month)
		, _day(day)
	{
		if (!IsInvalid()) // this->IsInvalid(this)                    
		{
			assert(false);
			//cout<<"非法日期"<	//exit(-1); 
		}
	}
	Date(const Date& d)     //拷贝构造
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	~Date()//析构函数
	{}
	bool IsInvalid(){                                                                         //判断输入的日期是否合法
		if (_year > 0 && _month > 0 && _month <13 && _day>0 && (_day <= GetMonthDay(_year, _month))){
			return 1;
		}
		{
			return 0;
		}
	}
	bool isLeapYear(int year){                                                   //判断是否为润年
		if (_year % 4 == 0 && _year % 100 != 0 || _year % 400 == 0){
			return 1;
		}
		return 0;
	}
	int GetMonthDay(int year, int month){                              //某年某月的天数 
		int days[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (isLeapYear(year)){
			days[2] = 29;
		}
		return days[month];
	}
	void Show()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	bool operator==(const Date& d){                       //运算符==的重载
		return(_year == d._year && _month == d._month && _day == d._day);
	}
	bool operator!=(const Date& d){                        //运算符!=的重载
		return!(_year == d._year&&_month == d._month && _day == d._day);
	}
	bool operator>=(const Date& d){                      //运算符>=的重载
		return(*this > d || *this == d);
	}
	bool operator<=(const Date& d){                        //运算符<=的重载
		return(*this > d);
	}
	bool operator>(const Date& d)                         //运算符>的重载
		// d1 >d2 
	{
		return(_year > d._year && _month > d._month &&_day > d._day);
	}
	Date& operator=(const Date& d)                         //赋值运算符的重载
		//d1=d2
	{
		this->_year = d._year; 
		this->_month = d._month;
		this->_day = d._day;
		return *this;
	}
	bool operator<(const Date& d)                      //运算符<的重载
		//d1<d2
	{
		if ((_year < d._year) || (_year == d._year &&_month < d._month) || (_year == d._year &&_month == d._month && _day < d._day)){
			return 1;
		}
		else{
			return 0;
		}
	}
		// d1 + 10 
	Date operator+(int day){                                     //运算符+的重载
		Date ret(*this);                                                 //因为this指针不能改变,所以这里通过this拷贝一个指针
		ret._day += day;
		while (ret.IsInvalid()== 0){
			int monthday = GetMonthDay(ret._year, ret._month);
			ret._day -= monthday;                                //若要加的天数多于当前月份的天数,则用这个天数减去当前月份的天数,然后月份再++
			ret._month++;
			if (ret._month > 12){                                         //若月份大于12则年份++
				ret._year++;
				ret._month = 1;
			}
		}
		return ret;
	}
	Date& operator+=(int day){                                //运算符+=的重载
		*this = *this + day;
		return *this;
	}
	Date operator-(int day){                                       //运算符-的重载
		if (day < 0){
			return *this + (-day);
		}
		Date ret(*this); 
		ret._day -= day;
		while (ret.IsInvalid() == 0){
			if (ret._month == 12){
				ret._month = 1;
				--ret._year;
			}
			else{
				--ret._month;
			}
			int monthday = GetMonthDay(ret._year, ret._month);
			ret._day += monthday;
		}
		return ret;
	}
	Date& operator-=(int day){                                                      //运算符-=的重载
		*this = *this - day;
		return *this;
	}
 int operator- (const Date& d){                                                  //计算两个日期之间相差的天数this-d 
		int flag = 1;
		Date max(*this);
		Date min(d);
		if (*this < d)                                                                            //如果*this<d则交换两个数的值
		{
			min = *this;
			max = d;
			flag = -1;
		}
		int days = 0;
		while(min < max){                                                                   //小的数一直往大的数上加,加一次days++,这样就可以计算出两个日期之间相差多少天
			min++;
			days++;
		}
		return days*flag;
	}


	//++d1 
	Date& operator++() // 前置 ++
	{
		*this = *this + 1;
		return *this;
	}
		//d1++ 
	Date operator++(int) // 后置 ++
	{
		Date ret(*this);
		*this = *this - 1;
		return ret;
	}
	Date operator--()   //前置--
	{
		*this = *this - 1;
		return *this;
	}
	Date operator--(int) //后置--
	{
		Date ret(*this); {
			*this = *this - 1;
			return ret;
		}
	}
private:
	int _year;
	int _month;
	int _day;
};


int main()
{
	Date d1(2018,2,14);
	d1.Show(); 
    Date d2(2018, 3, 28);
	d2.Show(); 
	cout << "运算符==的重载: expect is 0, actual is " << d1.operator==(d2) << endl;
	cout << "运算符!=的重载: expect is 1, actual is " << d1.operator!=(d2) << endl;
	cout << "运算符>=的重载: expect is 0, actual is " << d1.operator>=(d2) << endl;
	cout << "运算符<=的重载: expect is 1, actual is " << d1.operator<=(d2) << endl;
	cout << "运算符>的重载:  expect is 0, actual is " << d1.operator>(d2) << endl;
	cout << "运算符<的重载:  expect is 1, actual is " << d1.operator<(d2) << endl;


	cout << "运算符+的重载:  expect is 2018-2-24, actual is " << endl;
	Date d3 = d1.operator+(10);
	d3.Show();


	cout << "运算符+=的重载:  expect is 2018-2-19, actual is " << endl;
	d1 = d1.operator+=(5);
	d1.Show();


	cout << "运算符-的重载:  expect is 2018-2-4, actual is " << endl;
	Date d4 = d1.operator-(10);
	d4.Show();


	cout << "运算符-=的重载:  expect is 2018-2-9, actual is " << endl;
	d1 = d1.operator-=(5);
	d1.Show();
	
	int days = d1.operator-(d2);
	cout << "运算符-的重载:  expect is 14, actual is " <<days<< endl;


	cout << "运算符前置++的重载:  expect is 2018-2-15, actual is " << endl;
	Date d5 = ++d1;
	d5.Show();


	
	Date ret1= d2++;
	cout << "运算符后置++的重载:  expect is 2018-3-28, actual is "<< endl;
	ret1.Show();
	
	cout << "运算符前置--的重载:  expect is 2018-2-14, actual is " << endl;
	Date d6= --d5;
	d6.Show();


	Date ret2 = d2--;
	cout << "运算符后置--的重载:  expect is 2018-3-27, actual is " << endl;
	ret2.Show();


	//++d1; // d1.operator++(&d1) 
	//d1++; // d1.operator++(&d1, 0); 
	system("pause");
	return 0;
}

2、运行结果如下图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值