C++(8)——类与对象(4)

目录

纠错

前置++重载

后置++重载

日期类的实现


纠错

上一篇博客写错了两个地方。一个是赋值运算符重载不能重载成全局函数,还有一个是关于不能运算符重载的五个运算符中的 “.*”而不是“* 。很对不起大家。

前置++重载

前置++返回的是+1后的结果。

所以我们用引用。

也就是

后置++重载

后置++是先使用,然后再+1,因此需要保存原来的旧值,所以就必须传值调用,也就是拷贝构造。

这里为了区分后置++,给了一个int 的形参。

然后这里的 

是同一种意思,都是拷贝构造,为什么第一行不是赋值呢?

因为赋值的前提是拥有两个已经存在的对象,才能赋值,而tmp是创建出来的,因此是拷贝构造,而不是赋值。

日期类的实现

我们用这几天所介绍的东西学习来写一下日期类,也就是实现日期的加减和比较。

头文件:

#include <assert.h>
#include <iostream>
using namespace std;
class Date
{
public:
	int _year;
	int _month;
	int _day;
	void print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
	int GetMonthDay(int year,int month)
	{
		assert(month > 0 && month < 13);
		static int arr[13] = { 0,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 arr[month];
	}
	Date(int year = 2024, int month = 1, int day = 30);
	~Date();
	//Date(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);
	bool operator<(const Date& d);
	//日期+=天数
	Date& operator+=(int day);
	//日期+天数
	Date operator+(int day);
	//日期-=天数
	Date& operator-=(int day);
	//日期-天数
	Date operator-(int day);
	//前置++
	Date& operator++();
	//后置++
	Date operator++(int);
	//前置--
	Date& operator--();
	//后置--
	Date operator--(int);
	//日期--日期
	int operator-(const Date& d);
 };

源文件:

#include "date.h"

Date::Date(int year , int month, int day )
{
	_year = year;
	_month = month;
	_day = day;
}
//Date(const Date& d);
//比较大小
bool Date::operator==(const Date& d)
{
	return _year == d._year &&
		_month == d._month &&
		_day == d._day;
}
bool Date:: operator>=(const Date& d)
{
	return !((*this) < d);
}
bool Date:: operator<=(const Date& d)
{
	return !(*this > d);
}
bool Date::operator!=(const Date& d)
{
	return !((*this) == d);
}
bool Date:: operator>(const Date& d)
{
	return !((*this) <= d);
}
bool Date:: operator<(const Date& d)
{
	if (_year < d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month < d._month)
		{
			return true;
		}
		else if (_month == d._month)
		{
			if (_day < d._day)
			{
				return true;
			}

		}
	}
	return false;
}
//日期+=天数
Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month == 13)
		{
			_month = 1;
			++_year;
		}
	}
	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;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}

		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
//日期-天数
Date Date:: operator-(int day)
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}
//前置++
Date& Date:: operator++()
{
	_day += 1;
	return*this;
}
//后置++
Date Date::operator++(int)
{
	Date tmp = *this;
	tmp += 1;
	return tmp;
}
//前置--
Date& Date:: operator--()
{
	_day -= 1;
	return *this;
}
//后置--
Date Date::operator--(int)
{
	Date tmp = *this;
	//Date tmp(*this);
	tmp -= 1;
	return tmp;
}
//日期--日期
int Date:: operator-(const Date& d)
{
	int flag = 1;
	int n = 0;
	Date max = *this;
	Date min = d;
	if (max < min)
	{
		flag = -1;
		max = d;
		min = *this;
	}
	while (min != max)
	{
		n++;
		++min;
	}
	return n * flag;
}

今天就这样了。

Man what can I say ?

Mamba out !

孩子们,我回来了。

  • 10
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值