目录
日期类的实现
代码分享
Date.h
cpp
#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
class Date
{
//友元函数声明
friend ostream& operator<<(ostream& out, Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
// 全缺省的构造函数
Date(int year = 1, int month = 1, int day = 1);
// 拷贝构造函数
Date (const Date& d);
// 赋值运算符重载
// d2 = d3; -> d2.operator=(&d2, d3);
Date& operator=(const Date& d);
// 析构函数
~Date();
// 直接定义类里面,他默认是inline
// 频繁调用
//得到月的天数
int GetMonthDay(int year, int month)
{
assert(month>0&& month<13);
static int MonthDay[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 MonthDay[month];
}
bool CheckDate();
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;
Date& operator += (int day);
Date operator + (int day)const;
Date& operator ++ ();
// 为了区分,构成重载,给后置++,强行增加了一个int形参
// 这里不需要写形参名,因为接收值是多少不重要,也不需要用
// 这个参数仅仅是为了跟前置++构成重载区分
Date operator ++ (int);
Date& operator -= (int day);
Date operator - (int day)const;
//日期减日期
int operator -(const Date& d)const;
Date& operator -- ();
Date operator -- (int);
// 流插入
// 不建议,因为Date* this占据了一个参数位置,使用d<<cout不符合习惯
//void operator<<(ostream& out);
void Printf()const
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
private:
int _year = 1;
int _month = 1;
int _day = 1;
};
Test.cpp
cpp
#include"Date.h"
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;
}
}
//Date d2(d1);
//Date d2 = d1;
// 拷贝构造函数
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
// 赋值运算符重载
// d2 = d3; -> d2.operator=(&d2, d3);
Date& Date::operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
// 析构函数
Date::~Date()
{
_year = 1;
_month = 1;
_day = 1;
}
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 !(*this == d);
}
bool Date::operator <(const Date& d)const
{
if(this->_year<d._year)
{
return true;
}
else
{
if (this->_year == d._year && this->_month < d._month)
{
return true;
}
if (this->_year == d._year && this->_month == d._month && this->_day < d._day)
{
return true;
}
}
return false;
}
bool Date::operator > (const Date& d)const
{
return (!(*this < d)) && *this != d;//易错,或者!(*this<=d),但是要在<=函数后面
}
bool Date::operator <=(const Date& d)const
{
return *this < d || *this == d;
}
bool Date::operator >= (const Date& d)const
{
return *this > d || *this == d;
}
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 == 13)
{
++_year;
_month = 1;
}
}
return *this;
}
Date Date::operator + (int day)const
{
Date tmp=*this;
tmp += day;
return tmp;
}
Date& Date::operator ++ ()
{
*this += 1;
return *this;
}
Date Date::operator ++ (int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
Date& Date::operator -= (int day)
{
if(day<0)
{
return *this += -day;
}
_day -= day;
while (_day < 1)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day+= GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator - (int day)const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
//日期减日期
int Date::operator -(const Date& d)const
{
int flag = 1;
int count = 0;
//假设this大,d小
Date max = (*this);
Date min = d;
if (max < min)
{
max = d;
min = (*this);
flag = -1;
}
while (max>min)
{
++min;//一般用前置
++count;
}
return count * flag;
}
Date& Date::operator -- ()
{
*this -= 1;
return *this;
}
Date Date::operator -- (int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
ostream& operator<<(ostream& out,Date& d)
{
out << d._year << "年" <<d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
cout << "请输入年,月,日" << endl;
in >> d._year >> d._month >> d._day;
if (!d.CheckDate())
{
cout << "日期非法" << endl;
}
return in;
}
测试代码Test.cpp
cpp
#include"Date.h"
using namespace std;
void Test1()
{
Date d1(2022,6,21);
Date d2 = d1;
Date d3(2022,6,1);
if (d3 < d1)
{
cout << "d3<d1" << endl;
}
}
void Test2()
{
Date d1(2024, 4, 21);
Date d2 = d1;
d2 += 3000;
d2.Printf();
Date d3 = d1 + 300;
d3.Printf();
Date d4= d3++;
d4.Printf();
++d3;
d3.Printf();
}
void Test3()
{
Date d1(2024,4,21);
Date d2 = d1 - 5000;
d1 -= 20;
d1.Printf();
d2.Printf();
}
void Test4()
{
Date d1(2024,4,21);
Date d2(2002,2,14);
int day = d2 - d1;
cout << day << "Ìì" << endl;
}
void Test5()
{
Date d1(2024,4,21);
Date d2=d1--;
d2.Printf();
d1.Printf();
--d1;
d1.Printf();
}
void Test6()
{
Date d1(2024, 4, 14);
Date d2 = d1 + 30000;
// operator<<(cout, d1)
cout << d1;
cout << d2;
cin >> d1 ;
d2 = d1 + 30000;
cout << d1 << d2;
}
int main()
{
/*Test6();*/
Date d1(2024, 4, 21);
Date d2(2024, 4, 21);
if (d1 == d2)
{
cout << "d1 == d2" << endl;
}
return 0;
}
这个博客如果对你有帮助,给博主一个免费的点赞就是最大的帮助❤
欢迎各位点赞,收藏和关注哦❤
如果有疑问或有不同见解,欢迎在评论区留言哦❤
后续我会一直分享双一流211西北大学软件(C,数据结构,C++,Linux,MySQL)的学习干货以及重要代码的分享