类和对象的应用练习——日期类

#ifndef _DATE_H_
#define _DATE_H_

#include <iostream>
using namespace std;

class Date
{
public:
	int GetMonthDay(int year, int month);//获取某年某月的天数

	Date(int year = 1999, int month = 1, int day = 1);//全缺省构造函数

	void Print();//打印函数;

	Date(const Date &d);//拷贝函数

	//~Date();//析构函数,Date类不需要自己写析构函数

	//赋值运算符重载
	Date &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--(int);//后置--
	Date &operator--();//前置--

	//运算符重载
	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);//!=运算符重载

	int operator-(const Date &d);//日期-日期,返回天

private:
	int _year;
	int _month;
	int _day;
};


#endif
#include "date.h"


int Date:: GetMonthDay(int year, int month)//获取某年某月的天数
{
	//一年12个月,除了2月份其余月份的天数是固定的,因此定义一个数组来保存天数
	//加static的原因是会多次调用,加上static变成变量,在静态区保存,不需要重复定义
	static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

	if (month != 2)
	{
		return days[month];
	}
	else
	{
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)//4年一润且百年不润,400年再润,闰年2月多1天
		{
			return 29;
		}
		return 28;
	}
}

Date::Date(int year, int month, int day)//全缺省构造函数
{
	_year = year;
	_month = month;
	_day = day;
	if (!(year > 0 && month >= 0 && month < 13 && day<=GetMonthDay(year, month)))//判断年月日是否合法
	{
		cout << "日期非法" << endl;
	}
}

void Date::Print()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}

Date::Date(const Date &d)//拷贝函数
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

Date &Date::operator=(const Date &d)//赋值运算符
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	return *this;//d1=d2,返回的是d1的引用
}

Date &Date::operator+=(int day)//日期+=天数
{
	if (day<0)
	{
		return *this -= -day;//加一个负数,等于这个数-负数的绝对值
	}

	_day += day;
	while (_day > GetMonthDay(_year, _month))//先将天数全部加在一起,再进行判断
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month > 12)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;//返回日期引用
}

Date Date::operator+(int day)//日期+天数
{
	//Date ret(*this);//调用拷贝构造函数
	//ret._day += day;

	//while (ret._day > GetMonthDay(ret._year, ret._month))//先将天数全部加在一起,再进行判断
	//{
	//	ret._day -= GetMonthDay(ret._year, ret._month);
	//	ret._month++;
	//	if (ret._month > 12)
	//	{
	//		ret._year++;
	//		ret._month = 1;
	//	}
	//}

	//return ret;//不能返回引用,这是一个临时变量,出了作用域就会被销毁

	//上述代码和重载运算符+=类似,因此有如下变化
	Date ret(*this);
	ret += day;
	return ret;
}

Date Date::operator-(int day)//日期-天数
{
	Date ret(*this);
	if (day < 0)//减负数=+这个负数的绝对值
	{
		return ret += day;
	}
	ret._day -= day;//天数先运算在一起,再向前借位

	while (ret._day<=0)//天数小于等于0,则需要向前借
	{
		ret._month--;//借一位
		ret._day += GetMonthDay(ret._year, ret._month);

		if (ret._month == 0)
		{
			ret._month = 12;//变成上一年的12月份
			ret._year--;
		}
		if (ret._year < 0)
		{
			cout << "日期错误" << endl;
			break;
		}
	}
	return ret;

}
Date &Date::operator-=(int day)//日期-=天数
{
	//-=和-的逻辑同样类似

	*this = *this - day;

	return *this;
}

//为了区分前置++和后置++的重载函数,后置++的参数中有个int,
//int可以是随意的整形值,因为参数只是用来进行区分重载函数
Date &Date::operator++()//前置++  
{
	//直接利用已经写好的符号重载函数
	*this += 1;
	return *this;//前置++,返回引用
}

Date Date::operator++(int)//后置++
{
	Date ret(*this);//拷贝一份
	*this += 1;

	return ret;//后置++返回自增前的备份,临时变量不能返回引用
}

Date Date::operator--(int)//后置--
{
	Date ret(*this);
	*this -= 1;

	return ret;
}

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

//运算符重载
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;
}

bool Date::operator==(const Date &d)//==运算符重载
{
	if (_year == d._year&&_month == d._month&&_day == d._day)
		return true;
	return false;
}
bool Date::operator>=(const Date &d)//>=运算符重载
{
	if (*this > d || *this == d)//直接利用前面的函数
		return true;
	return false;
}
bool Date::operator<(const Date &d)//<运算符重载
{
	if (!(*this >= d))
		return true;
	return false;
}
bool Date::operator<=(const Date &d)//<=运算符重载
{
	if (!(*this>d))
		return true;
	return false;
}
bool Date::operator!=(const Date &d)//!=运算符重载
{
	if (!(*this == d))
		return true;
	return false;
}


int Date::operator-(const Date &d)//日期-日期,返回天数
{
	Date max = *this;
	Date min = d;

	int flag = 1;
	if (max < min)
	{
		max = d;
		min = *this;//交换过来
		flag = -1;
	}

	int n = 0;
	while (max != min)
	{
		min++;
		n++;
	}
	return n*flag;

}
#include "date.h"


int main()
{
	Date d1(2021, 2, 3);
	Date d2(2021, 9, 1);

	cout << d1-d2 << endl;

	//Date d1(2021, 2, 3);
	//Date d2(d1);
	//Date d3(d1);
	//Date d4(d1);
	//Date d5(d1);
	//Date d6(d1);


	//d5 = d1 + 100;
	//d5 -= -100;
	//d2 += 100;
	//d3 = d2 - 10000;
	//
	d5 -= 10000;

	//Date d7=d6++;
	//Date d8 =++d6;

	//


	//cout << "------D1------" << endl;
	//d1.Print();
	//cout << "------D2------" << endl;
	//d2.Print();
	//cout << "------D3------" << endl;
	//d3.Print();
	//cout << "------D4------" << endl;
	//d4.Print();
	//cout << "------D5------" << endl;
	//d5.Print();
	//cout << "------D6------" << endl;
	//d6.Print();
	//cout << "------D7------" << endl;
	//d7.Print();
	//cout << "------D8------" << endl;
	//d8.Print();

	//if (d1 >= d3)
	//	cout << "hh" << endl;
	//if (d3 <=d4)
	//	cout << "ss" << endl;
	//else
	//{
	//	cout << ":kk" << endl;
	//}

	//if (d1 >= d4)
	//	cout << "hhfff" << endl;



	system("pause");
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值