日期类的实现
C++中日期类的实现代码,方便各位查阅
✍详细介绍在类与对象(中)
1、声明
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class Date
{
//友元声明(类中的任意位置)
friend inline ostream& operator<<(ostream& out, const Date& d);
friend inline istream& operator>>(istream& in, Date& d);
private:
int _year;
int _month;
int _day;
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month);
// 全缺省的构造函数
Date(int year = 2000, int month = 1, int day = 1);
// 拷贝构造函数
// d2(d1)
Date(const Date& d);
//日期类打印
void Print() const;
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d);
// 析构函数
~Date();
// 日期+=天数
Date& operator+=(int day);
// 日期+天数
Date operator+(int day) const;
// 日期-天数
Date operator-(int day) const;
// 日期-=天数
Date& operator-=(int day);
// 前置++
Date& operator++();
// 后置++
Date operator++(int);
// 前置--
Date& operator--();
// 后置--
Date operator--(int);
// >运算符重载
bool operator>(const Date& d) const;
// ==运算符重载
bool operator==(const Date& d) const;
// >=运算符重载
bool operator >= (const Date& d) const;
// <运算符重载
bool operator < (const Date& d) const;
// <=运算符重载
bool operator <= (const Date& d) const;
// !=运算符重载
bool operator != (const Date& d) const;
// 日期-日期 返回天数
int operator-(const Date& d) const;
};
// 流插入的重载
inline ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
// 流提取的重载
inline istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}
2、实现:
#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"
using namespace std;
//1.月份内的天数
int Date::GetMonthDay(int year, int month)
{
int monthDay[] = {
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;
}
else
{
return monthDay[month];
}
}
//2.构造函数
//这里最好写一个全缺省的构造函数,这样即使不传参对象也会被初始化
Date::Date(int year, int month, int day)
{
//这里的缺省值已经在声明的时候给出了
_year = year;
_month = month;
_day = day;
}
//3.拷贝构造函数
//拷贝构造函数必须是传值引用传参,并且最好加上const防止拷贝的时候实参被改变
//这里的日期类可以不用书写,编译器会自动给出
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//4.析构函数
Date::~Date()
{
_year = 0;
_month = 0;
_day = 0;
}
//5.打印日期
void Date::Print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
//6.赋值重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
//7.日期+=天数
//这个属于赋值运算符重载,由内置类型+=符号运算,会改变左操作数的值;
//因此在返回时我们直接返回运算之后的对象即可
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)
{
_month = 1;
_year++;
}
}
return *this;
}
//8.日期+天数
//复用上述日期+=天数即可
//需要注意这里不会改变日期原本的值
//而是返回一个临时产生的对象
Date Date::operator+(int day) const
{
Date tmpObject(*this); //产生临时对象
return tmpObject += day; //复用
}
//9.日期-=天数
//同理,会改变日期,直接返回即可
Date& Date::operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
_month--;
_day += GetMonthDay(_year, _month);
//月份减完年减一
if (_month < 1)
{
_month = 12;
_year--;
}
}
return *this;
}
//10日期-天数
//同理,复用日期-=天数即可
Date Date::operator-(int day) const
{
Date tmpObject(*this);
return tmpObject -= day;
}
//11.==运算符重载
//意思是判断日期是否相等,返回的是0/1(bool型)
bool Date::operator==(const Date& d) const
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
//12.>运算符重载
//比较日期的大小:先比年-年相等比月-年月相等比日
bool Date::operator>(const Date& d) const
{
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;
}
//13.>=、<、<=等运算符重载
//均可以通过复用>、==来实现
bool Date::operator>=(const Date& d) const
{
return *this > d || *this == d;
}
bool Date::operator<(const Date& d) const
{
return !(*this > d);
}
bool Date::operator<=(const Date& d) const
{
return !(*this > d || *this == d);
}
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
//14.日期-日期
//日期-日期是有意义的
//首先需要区分日期间的大小日期,我们直接小的日期每次循环+1天
//循环次数即为间隔天数
int Date::operator-(const Date& d) const
{
//默认是当前对象日期大
//很经典的找大的方法:先假设
Date min = d;
Date max = *this;
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
//计算相差天数
int count = 0;
while (min != max)
{
min++;
count++;
}
return count * flag;
}
//15.前置和后置++/--
//前置++,先加再用,因此直接对该对象加减即可
Date& Date::operator++()
{
//引用返回
return *this += 1;
}
//后置++,先用再加,返回加之前的值
//规定:后置++多一个参数,是为了与前置++做出区分,构成重载,无其它作用
Date Date::operator++(int)
{
Date tmpObject(*this);
*this += 1;
//传值返回
return tmpObject;
}
// 前置--:返回减后的值
Date& Date::operator--()
{
return *this -= 1;
}
//后置--:后置会多两次拷贝
Date Date::operator--(int)
{
Date tmpObject(*this);
*this -= 1;
return tmpObject;
}
3、测试:
#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"
using namespace std;
void TestDate1()
{
Date d1(2023, 3, 1);
d1 -= 10;
d1.Print();
Date d2(2023, 3, 2);
d2 += 100;
d2.Print();
Date d3(2023, 3, 2);
d3 += -100;
d3.Print();
Date d4(2023, 3, 1);
(d4++).Print();
Date d5(2023, 3, 1);
(++d5).Print();
}
void TestDate2()
{
Date d1(2023, 3, 1);
Date d2(2023, 9, 1);
cout << (d2 - d1) << endl;
}
void TestDate3()
{
Date d1;
Date d2;
cin >> d1 >> d2;
cout << d1 << d2;
}
void TestDate4()
{
Date d1(2023, 1, 1);
const Date d2(2023, 3, 1);
d2.Print();
}
int main()
{
TestDate1();
TestDate2();
TestDate3();
TestDate4();
return 0;
}
测试结果:(供参考)