C++日期类的实现

发布时间:2024年01月02日

前言:在类和对象比较熟悉的情况下,我们我们就可以开始制作日期表了,实现日期类所包含的知识点有构造函数,析构函数,函数重载,拷贝构造函数,运算符重载,const成员函数

1.日期类的加减运算

#define _CRT_SECURE_NO_WARNINGS 1
#include"data.h"
class Data
{
public:
	//获取某年某月的天数
	int GetMonthday(int year, int month)
	{
		int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		int day = days[month];
		if (month == 2)
		{
			if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
			{
				day += 1;
			}
		}
		return day;
	}
	Data(int year=1900,int month=1,int day=1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//拷贝构造
	//Data(const Data& d);

	//日期赋值
	Data& operator = (const Data & d) const
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		return *this;
	}


	Data& operator-=(int day)
	{
		_day -= day;
		while (_day <= 0)
		{
			if (_month == 1)
			{
				_year--;
				_month = 12;
			}
			else
			{
				_month--;
			}
			_day = GetMonthday(_year, _month) + _day;
		}
		return *this;
	}

	Data& operator+=(int day)
	{
		_day += day;
		while (_day > GetMonthday(_year, _month))
		{
			_day = _day - GetMonthday(_year, _month);
			_month++;
			if (_month == 13)
			{
				_year++;
				_month = 1;
			}
		}
		return *this;
	}

	void Print(const Data& d)
	{
		cout << _year << "年" << _month << "月" << _day << "日" << endl;
	}
	

private:
	int _year;
	int _month;
	int _day;

};



int main()
{
	Data d1(2023,12,31);
	Data d2(d1);
    
	d2 = d2.operator+=(10000);
	d2.Print(d2);
	return 0;
}

2 日期类的运算符重载

类里面有默认的拷贝构造函数(不显含),还有默认的构造函数(不显含),你也可以把这个默认的构造函数写出来,只是不显含的构造函数对类里面的内置类型不做处理,自定义类型会调用它的构造函数,就会处理一下。

//日期赋值
Data& operator = (const Data & d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}


Data& operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		if (_month == 1)
		{
			_year--;
			_month = 12;
		}
		else
		{
			_month--;
		}
		_day = GetMonthday(_year, _month) + _day;
	}
	return *this;
}

Data& operator+=(int day)
{
	_day += day;
	while (_day > GetMonthday(_year, _month))
	{
		_day = _day - GetMonthday(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

Data& operator++()//前置++
{
	_day++;
	if (_day > GetMonthday(_year, _month))
	{
		_day = _day - GetMonthday(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
		
	}
	return *this;
}

Data& operator++(int)//后置++
{
	Data d = *this;
	++_day;
	return d;
}

//运算符重载,比较两个日期的大小
bool operator>(const Data& d)
{
	return ((_year > d._year) || (_year==d._year&&_month > d._month) || (_year == d._year && _month == d._month&&_day > d._day));
}

bool operator>=(const Data& d)
{
	return !((_year < d._year) || (_year == d._year && _month < d._month) || (_year == d._year && _month == d._month && _day < d._day));
}

bool operator<(const Data& d)
{
	return ((_year < d._year) || (_year == d._year && _month < d._month) || (_year == d._year && _month == d._month && _day < d._day));
}

bool operator<=(const Data& d)
{
	return !((_year > d._year) || (_year > d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day));

}

bool operator!=(const Data& d)
{
	return !((_year == d._year) && (_month == d._month) && (_day == d._day));
}

int operator-(Data& d)
{
	
	Data max = *this;
	Data min = d;
	int flag = 1;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return flag * n;
}

3 我们将运算符重载的函数和两个日期相差的天数进行配合计算

int operator-(Data& d)
{
	
	Data max = *this;
	Data min = d;
	int flag = 1;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return flag * n;
}

int main()
{
	Data d1(2023,12,31);
	//Data d2(d1);
	Data d2(1900,11,30);
    
	//d2 = d2.operator+=(10000);
	d2.Print(d2);
	cout << d2.operator-(d1) << endl;
	return 0;
}

所有源码的实现如下

data.h


#pragma once
#include<iostream>
using namespace std;
class Data
{
public:
	//获取某年某月的天数
	int GetMonthday(int year, int month)
	{
		int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		int day = days[month];
		if (month == 2)
		{
			if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
			{
				day += 1;
			}
		}
		return day;
	}
	Data(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//拷贝构造
	//Data(const Data& d);

	//日期赋值
	Data& operator = (const Data& d);


	Data& operator-=(int day);
	
	Data& operator+=(int day);
	

	Data& operator++();//前置++
	

	Data& operator++(int);//后置++
	

	//运算符重载,比较两个日期的大小
	bool operator>(const Data& d);

	bool operator>=(const Data& d);

	bool operator<(const Data& d);

	bool operator<=(const Data& d);

	bool operator!=(const Data& d);

	int operator-(Data& d);

	void Print(const Data& d);


private:
	int _year;
	int _month;
	int _day;

};



data.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include"data.h"
Data& Data::operator = (const Data& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}


Data& Data::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		if (_month == 1)
		{
			_year--;
			_month = 12;
		}
		else
		{
			_month--;
		}
		_day = GetMonthday(_year, _month) + _day;
	}
	return *this;
}

Data& Data::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthday(_year, _month))
	{
		_day = _day - GetMonthday(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

Data& Data::operator++()//前置++
{
	_day++;
	if (_day > GetMonthday(_year, _month))
	{
		_day = _day - GetMonthday(_year, _month);
		_month++;
		if (_month == 13)
		{
			_year++;
			_month = 1;
		}

	}
	return *this;
}

Data& Data::operator++(int)//后置++
{
	Data d = *this;
	++_day;
	return d;
}

//运算符重载,比较两个日期的大小
bool Data::operator>(const Data& d)
{
	return ((_year > d._year) || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day));
}

bool Data::operator>=(const Data& d)
{
	return !((_year < d._year) || (_year == d._year && _month < d._month) || (_year == d._year && _month == d._month && _day < d._day));
}

bool Data::operator<(const Data& d)
{
	return ((_year < d._year) || (_year == d._year && _month < d._month) || (_year == d._year && _month == d._month && _day < d._day));
}

bool Data::operator<=(const Data& d)
{
	return !((_year > d._year) || (_year > d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day));

}

bool Data::operator!=(const Data& d)
{
	return !((_year == d._year) && (_month == d._month) && (_day == d._day));
}

int Data::operator-(Data& d)
{

	Data max = *this;
	Data min = d;
	int flag = 1;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return flag * n;
}

void Data::Print(const Data& d)
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}



int main()
{
	Data d1(2023,12,31);
	//Data d2(d1);
	Data d2(1900,11,30);
    
	//d2 = d2.operator+=(10000);
	d2.Print(d2);
	cout << d2.operator-(d1) << endl;
	return 0;
}

文章来源:https://blog.csdn.net/2301_79811170/article/details/135327357
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。