C++赋值重载和运算符重载(日期类)

目录

赋值运算符重载

运算符重载


赋值运算符重载

赋值运算符重载是⼀个默认成员函数,用于完成两个已经存在的对象直接的拷贝赋值,这⾥要注意跟拷贝构造区分,拷贝构造用于⼀个对象拷贝初始化给另⼀个要创建的对象。

注意:无显式调用时,编译器会自动生成默认赋值运算符重载,默认赋值运算符重载行为跟默认拷贝构造函数类似,对内置类型成员变量会完成浅拷贝。对于自定义类型,没有显式调用时会造成内存泄漏和析构两次的问题。

Date& Date::operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
		return *this;
}

运算符重载 

1.格式:由operator和后面要定义的运算符共同构成。

2.⼀元运算符有⼀个参数,二元运算符有两个参数,二元运算符的左侧运算对象传给第⼀个参数,右侧运算对象传给第⼆个参数。

3.⼀个重载运算符函数是成员函数,则它的第⼀个运算对象默认传给隐式的this指针,因此运算符重载作为成员函数时,参数比运算对象少⼀个。

4. .*    ::    sizeof    ?:    .    注意以上5个运算符不能重载。

5.C++规定,后置++重载时,增加⼀个int形参,跟前置++构成函数重载,方便区分。

+,+=,-,-=

Date& Date::operator+=(int day)//改变自己
{
	_day += day;
	while(_day > getMonthDay(_year, _month))
	{
		_month++;
		_day -= getMonthDay(_year, _month);
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp +=  day;
	return tmp;
}
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day < 0)
	{
		_month--;
		_day += getMonthDay(_year, _month);
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
	}
	return *this;
}
Date Date::operator-(int day)
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}

++,--

//前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
//后置++
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
//前置--
Date& Date::operator++()
{
	*this -= 1;
	return *this;
}
//后置--
Date Date::operator++(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}

< > <= >= == !=

bool Date::operator==(const Date& d)
{
	if (_year == d._year
		&& _month == d._month
		&& _day == d._day)
		return true;
	else
	{
		return false;
	}
}
bool Date::operator>(const Date& d)
{
	if (_year > d._year)
		return true;
	else if (_year == d._year && _month > d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day > d._day)
	{
		return true;
	}
	return false;
}
bool Date::operator<(const Date& d)
{
	return !(*this >= d);
}
bool Date::operator>=(const Date& d)
{
	return (*this == d) || (*this > d);
}
bool Date::operator<=(const Date& d)
{
	return !(*this > d);
}
bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}

<< >> 

ostream& operator<<(ostream& out, Date d)
{
	cout << d._year << d._month << d._day << endl;
	return out;
}
istream& operator>>(istream& in, Date d)
{
	cin >> d._month >> d._year >> d._day;
	return cin;
}

头文件:

#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year, int month, int day);
	void print();
	int getMonthDay(int year,int month)
	{
		int montharr[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		{
			return 29;
		}
		return montharr[month];
	}
	Date& operator=(const Date& d);

	//实现日期类加减
	Date& operator+=(int day);
	Date operator+(int day);
	Date& operator-=(int day);
	Date operator-(int day);

	bool operator==(const Date& d);
	bool operator>(const Date& d);
	bool operator<(const Date& d);
	bool operator>=(const Date& d);
	bool operator<=(const Date& d);
	bool operator!=(const Date& d);

	//前置++
	Date& operator++();
	//后置++
	Date operator++(int);
	//前置--
	Date& operator--();
	//后置--
	Date operator--(int);
	
	int operator-(const Date d);
	
private:
	int _year;
	int _month;
	int _day;
};

源文件: 

#define _CRT_SECURE_NO_WARNINGS
#include"Date.h"
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
void Date::print()
{
	cout << _year << '/' << _month << '/' << _day  << endl;
}
Date& Date::operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
		return *this;
}
Date& Date::operator+=(int day)//改变自己
{
	_day += day;
	while(_day > getMonthDay(_year, _month))
	{
		_month++;
		_day -= getMonthDay(_year, _month);
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}
Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp +=  day;
	return tmp;
}
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day < 0)
	{
		_month--;
		_day += getMonthDay(_year, _month);
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
	}
	return *this;
}
Date Date::operator-(int day)
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}
bool Date::operator==(const Date& d)
{
	if (_year == d._year
		&& _month == d._month
		&& _day == d._day)
		return true;
	else
	{
		return false;
	}
}
bool Date::operator>(const Date& d)
{
	if (_year > d._year)
		return true;
	else if (_year == d._year && _month > d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day > d._day)
	{
		return true;
	}
	return false;
}
bool Date::operator<(const Date& d)
{
	return !(*this >= d);
}
bool Date::operator>=(const Date& d)
{
	return (*this == d) || (*this > d);
}
bool Date::operator<=(const Date& d)
{
	return !(*this > d);
}
bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}


//前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
//后置++
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
//前置--
Date& Date::operator++()
{
	*this -= 1;
	return *this;
}
//后置--
Date Date::operator++(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}
int Date::operator-(const Date d)
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		min++;
		n++;
	}
	return n;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值