C++类与对象的简单应用【日期类的简单实现】

在咱们刚接触C++,写一个日期类进行练习,实现从能比较多,如计算日期差,设置日期,输出日期…
下面我会展示一个简单的日期类

声明

#include 
#include 
using namespace std;

class Date
{
public:
	bool CheckInvalid();
	Date(int year = 1, int month = 1, int day = 1);
	
	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);

	//频繁调用直接写内联函数
	int GetMonthDay(int year, int month)
	{
		assert(month > 0 && month < 13);
		static int monthDays[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 monthDays[month];
	}

	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}

	//友元声明
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

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

ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in,Date& d);

主体程序

#include "Date.h"

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;

	if (!CheckInvalid())
	{
		cout << "日期非法" << endl;
	}
}

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)
{
	return *this < d || *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 _year  == d._year
		 &&_month == d._month
		 &&_day   == d._day;
}

bool Date::operator!=(const Date&d)
{
	return !(*this == d);
}

Date& Date::operator+= (int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year,_month);
		++_month;
		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}

	return *this;
}

Date Date::operator+ (int day)
{
	//这两种都是拷贝构造
	//Date tmp(*this);
	Date tmp = *this;
	tmp += day;
	
	return tmp;
	//这里我就重载了,下面就注释了
	//tmp._day += day;
	//while (tmp._day > GetMonthDay(tmp._year, tmp._month))
	//{
	//	tmp._day -= GetMonthDay(tmp._year, tmp._month);
	//	++tmp._month;
	//	if (_month == 13)
	//	{
	//		++tmp._year;
	//		tmp._month = 1;
	//	}
	//}
	//
	//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;
}

bool Date::CheckInvalid()
{
	if (_year <= 0 || _month<1 || _month>12 || _day<1 || _day>GetMonthDay(_year, _month))
	{
		return false;
	}
	else
	{
		return true;
	}
}

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;

	return out;//要返回值是要连续流提取/插入
}

istream& operator>>(istream& in, Date& d)
{
	while (1)
	{
		cout << "输入日期:";
		in >> d._year >> d._month >> d._day;

		if (!d.CheckInvalid())
		{
			cout << "日期非法" << endl;
		}
		else
		{
			break;
		}
	}


	return in;//要返回值是要连续流提取/插入
}

测试和应用

加天数后的日期

#include "Date.h"

int main()
{
	Date d1(2024, 1, 29);
	Date d2 = d1 + 20;
	d2.Print();
	d1.Print();
	return 0;
}

用流来进行提取和插入

#include "Date.h"

int main()
{
	Date d1;
	Date d2;
    //函数重载,this指针是第一个参数,Date必须是左参数了
	operator<<(cout,d1);
	cin >> d2>>d1;
	cout << d2 << d1;
	return 0;
}

你可能感兴趣的:(c++,开发语言)