文章目录
一、日期类要实现的函数
类名为Date
bool CheckDate(); //检查所用日期是否合法(有无越界)
Date(int year = 1, int month = 1, int day = 1);//全缺省构造函数(初始化)
Date(const Date& d); //拷贝构造
int GetMonthDay(int year, int month);//获取某年某月的天数
void Print() const;//打印日期
//运算符重载
Date& operator+=(int day);
Date operator+(int day) const;// 加const后第一个参数变为了const Date* const this (第一个const),this指向的内容就不能被改变
Date& operator-=(int day);
Date operator-(int day) const;
//++d1(前置加加)
Date operator++();
//d1++(后置加加)为了区分前置和后置加加,规定后置加加要有形参
Date operator++(int);
//--d1
Date operator--();
//d1--
Date operator--(int);
//d1-d2
int 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;
bool operator>(const Date& d) const;
//流插入
ostream& operator<<(ostream& out, const Date& d);
//流提取
istream& operator>>(istream& in, Date& d);
二、函数实现
若函数写在类的外面,要在函数名前面加 类名::,在Date类中,为Date::
1、GetMonthDay获取对应月份的天数
//获取某年某月的天数
int GetMonthDay(int year, int month)
{
//断言,确保月份合法
assert(month > 0 && month < 13);
//下标对应月份 1 2 3 4 5 6 7 8 9 10 11 12
static int getmonthDay[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
//因为该函数(GetMonthDay)会被频繁调用,即会频繁创建数组,将数组放在静态区(static)更好
//闰年二月有29天
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
return 29;
return getmonthDay[month];
}
2、CheckDate检查日期
判断month是否在1~12之间,以及该月对应的天数是否符合
//检查传的日期是否正确
bool Date::CheckDate()
{
if (_month < 1 || _month>12 || _day <1 || _day>GetMonthDay(_year, _month))
return false;
else
return true;
}
3、Date构造函数
//全缺省构造
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!CheckDate())
{
cout << "非法日期:" << endl;
Print();
}
}
//拷贝构造
Date::Date(const Date& d)
{
//将d拷贝给要构造的对象
_year = d._year;
_month = d._month;
_day = d._day;
}
4、Print打印日期
void Date::Print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
5、运算符重载
1. += 、+、-=、-
//d1+=day(将d1直接改变了)
Date& Date::operator+=(int day)
{
//day可能为负
if (day < 0)
{
return *this -= (-day);
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_month = 1;
_year ++;
}
}
return *this;
}
//d1+day(d1的值不变)
Date Date::operator+(int day) const
{
Date tmp = *this;//拷贝构造
tmp += day;
return tmp;
}
//d1-=day(d1被改变了)
Date& Date::operator-=(int day)
{
//day可能为负
if (day < 0)
{
return *this += (-day);
}
//用-=复用-
//*this = *this-day;
//return *this;
_day -= day;
while (_day <= 0) //_day>0合法
{
_month--;
//_month为1月
if (_month == 0)
{
_month = 12;
_year--;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
//d1-day(d1不变)
Date Date::operator-(int day) const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
2. 前置++/–、后置++/–
//++d1(表达式++d1的值改变,但d1自增)
Date Date::operator++()
{
*this += 1;
return *this;
}
//d1++(表达式d1++的值不变,但d1自增)
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp; //表达式的值不变,但d1自增
}
Date Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
3. 两个日期类相减(求相差日期)
//d1-d2
int Date::operator-(const Date& d)const
{
int n = 0;
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
while (min != max)
{
++min;
++n;
}
return n * flag; //如果d1>d2 ,flag=1,天数相差为正数
//如果d1<d2 ,flag=-1,天数相差为负数
}
6、比较
//写两个比较运算符重载,剩下的都可以通过复用这两个来完成
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)
return _day < d._day;
}
return false;//剩下的全为false
}
bool Date::operator==(const Date& d)const
{
return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator<=(const Date& d)const
{ // d1<=d2 即 *this<=d
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 _year != d._year || _month != d._month || _day != d._day;
}
7、流插入、流提取(输入输出)
若重载函数写在类的外面,要在类中加入友元函数声明,使其可以访问类的成员变量(private)
//友元函数声明
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
//流插入
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;//能够连续打印
} //cout << d1 << d2;流插入是从左往右赋值
//返回out后,相当于将cout<<d1变为了out,此时,变为了out<<d2 ,进而将d2打印出来了!
//流提取
istream& operator>>(istream& in, Date& d)
{
有非法日期时无法判断
//cout << "请依次输入年月日:>";
//in >> d._year >> d._month >> d._day;
while (1)
{
cout << "请依次输入年月日:>";
in >> d._year >> d._month >> d._day;
if (!d.CheckDate())
{
cout << "该输入日期非法:";
d.Print();
cout << "请重新输入!!!" << endl;
}
else
break;
}
return in;
}
三、完整代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
//友元函数声明
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
bool CheckDate();
//全缺省构造函数
Date(int year = 1, int month = 1, int day = 1);
//打印
void Print() const;
//拷贝构造
Date(const Date& d);
//获取某年某月的天数(直接在头文件中写,默认是内联函数inline)
int GetMonthDay(int year, int month)
{
assert(month > 0 && month < 13);
//下标对应月份 1 2 3 4 5 6 7 8 9 10 11 12
static int getmonthDay[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
//因为该函数会被频繁调用,即会频繁创建数组,要放在静态区更好static
//闰年二月有29天
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
return 29;
return getmonthDay[month];
}
//日期的加、减
Date& operator+=(int day);
Date operator+(int day) const;// 加const后第一个参数变为了const Date* const this (第一个const),this指向的内容就不能被改变
Date& operator-=(int day);
Date operator-(int day) 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;
bool operator>(const Date& d) const;
//前/后置 ++
//后置++增加一个int形参(实际上编译器并不接收该形参),与前置++构成函数重载
//++d1
Date operator++();
//d1++
Date operator++(int);
Date operator--();
Date operator--(int);
//d1-d2
int operator-(const Date& d) const;
//流插入运算符重载 ,不符合用户习惯
//void operator<<(ostream& out);
private:
int _year;
int _month;
int _day;
};
//检查传的日期是否正确
bool Date::CheckDate()
{
if (_month < 1 || _month>12 || _day <1 || _day>GetMonthDay(_year, _month))
return false;
else
return true;
}
//全缺省构造
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!CheckDate())
{
cout << "非法日期:" << endl;
Print();
}
}
//拷贝构造
Date::Date(const Date& d)
{
//将d拷贝给要构造的对象
_year = d._year;
_month = d._month;
_day = d._day;
}
void Date::Print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
//d1+=day(将d1直接改变了)
Date& Date::operator+=(int day)
{
//day可能为负
if (day < 0)
{
return *this -= (-day);
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_month = 1;
_year ++;
}
}
return *this;
}
//d1+day(这里的d1不变)
Date Date::operator+(int day) const
{
Date tmp = *this;//拷贝构造
tmp += day;
return tmp;
}
Date& Date::operator-=(int day)
{
//day可能为负
if (day < 0)
{
return *this += (-day);
}
//用-=复用-
//*this = *this-day;
//return *this;
_day -= day;
while (_day <= 0) //_day>0合法
{
_month--;
//_month为1月
if (_month == 0)
{
_month = 12;
_year--;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day) const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
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)
return _day < d._day;
}
return false;//剩下的全为false
}
bool Date::operator==(const Date& d)const
{
return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator!=(const Date& d)const
{
return _year != d._year || _month != d._month || _day != d._day;
}
bool Date::operator<=(const Date& d)const
{ // d1<=d2 即 *this<=d
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;
}
//++d1
Date Date::operator++()
{
*this += 1;
return *this;
}
//d1++
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp; //表达式的值不变,但d1自增
}
Date Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
//d1-d2
int Date::operator-(const Date& d)const
{
int n = 0;
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
while (min != max)
{
++min;
++n;
}
return n * flag; //如果d1>d2 ,flag=1,天数相差为正数
//如果d1<d2 ,flag=-1,天数相差为负数
}
//void Date::operator<<(ostream& out)
//{
// out << _year << "年" << _month << "月" << _day << "日" << endl;
//}
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;//能够连续打印
} //cout << d1 << d2;流插入是从左往右赋值
//返回out后,相当于将cout<<d1变为了out,此时,变为了out<<d2 ,进而将d2打印出来了!
istream& operator>>(istream& in, Date& d)
{
有非法日期时无法判断
//cout << "请依次输入年月日:>";
//in >> d._year >> d._month >> d._day;
while (1)
{
cout << "请依次输入年月日:>";
in >> d._year >> d._month >> d._day;
if (!d.CheckDate())
{
cout << "该输入日期非法:";
d.Print();
cout << "请重新输入!!!" << endl;
}
else
break;
}
return in;
}
//测试
//成员函数的const要放到参数列表的后面
void Test01()
{ //+= 、 +
Date a(2025, 4, 13);
Date tmp = a + 100; //或 Date tmp(a+100);
a.Print();
tmp.Print();
a += 10000;
a.Print();
}
void Test02()
{ //-= 、 -
Date a(2025, 4, 14);
a -= 100;
a.Print();
}
void Test03()
{
Date a(2025, 4, 14);
Date b(2024, 4, 14);
cout << (a < b) << endl;
}
void Test04()
{
Date a(2025, 4, 14);
/*Date tmp1 = a++;
a.Print();
tmp1.Print();*/
Date tmp2 = ++a; // a.operator++();
a.Print();
tmp2.Print();
}
void Test05()
{ //a、b相差多少天
Date a(2025, 4, 15);
Date b(2024, 4, 31);
cout << (a - b) << endl;
}
void Test06()
{
Date d1;
Date d2;
//cout<<d1;
//d1 << cout;
//void operator<<(ostream & out); 参数列表有一个隐含的this指针,this为第一个参数,而out为第二个,故传参的时候,要将d1写在前面
//cout << d1 << d2;//有return可以实现连续打印
cin >> d1 >> d2;
cout << d1 - d2 << endl;
}
int main()
{
//Test01();
//Test02();
//Test03();
Test06();
return 0;
}