【C++】基础类之日期类
六点半888 2024-09-04 13:05:02 阅读 53
【C++】基础类之日期类
1. DateClass.h2. DateClass.cpp3. Text.cpp
1. DateClass.h
<code>#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{ -- -->
public:
Date(int year, int month, int day);
Date(const Date& date)
{
_year = date._year;
_month = date._month;
_day = date._day;
}
void Print()
{
cout << "year:" << _year << "month:" << _month << "day:" << _day << endl;
}
//得到对应year(平年或瑞年)对应month的天数
int GetMonthDay(int year, int month);
//两日期比较
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+=(const int day);//改变
Date operator+(const int day)const;//不改变
//日期减天数,同上
Date& operator-=(const int day);
Date operator-(const int day)const;
//前后置++,改变当前日期本身
Date& operator++();//const Date& operator++();
Date operator++(int);
//前后置--,改变当前日期本身
Date& operator--();
Date operator--(int);
//两日期间隔天数
int operator-(const Date& date)const;
//日期赋值
Date& operator=(const Date& date);
// 流插入不能写成成员函数?
// 因为Date对象默认占用第一个参数,就是做了左操作数
// 写出来就一定是下面这样子,不符合使用习惯
//d1 << cout; // d1.operator<<(cout);
//void operator<<(ostream& out);
/*int GetYear()
{
return _year;
}*/
friend ostream& operator<<(ostream& out, const Date& date);
friend istream& operator>>(istream& in, Date& date);
private:
int _year = 0;
int _month = 0;
int _day = 0;
};
ostream& operator<<(ostream& out, const Date& date);
istream& operator>>(istream& in, Date& date);
2. DateClass.cpp
#include"DateClass.h"
int Date::GetMonthDay(int year, int month)
{
static int daysArr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30 };
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
{
return 29;
}
else
return daysArr[month];
}
Date::Date(int year, int month, int day)
{
if (month > 0 && month < 13 && day < GetMonthDay(year, month))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "非法日期" << endl;
assert(false);
}
}
bool Date::operator>(const Date& d)const
{
if (_year > d._year)
{
return true;
}
if (_year == d._year && _month > d._month)
{
return true;
}
if (_year == d._year && _month == d._month && _day > d._day)
{
return true;
}
return 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 !(*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);
}
bool Date::operator!=(const Date& d)const
{
return !(*this == d);
}
Date& Date::operator+=(const 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+(const int day)const
{
Date tmp = *this;
tmp += day;
return tmp;
}
Date& Date::operator-=(const int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while(_day <= 0)
{
_month--;
if (_month == 0)
{
_year--;
_month = 0;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(const int day)const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
//const Date& Date::operator++()
//{
//return *this + 1;
//}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
int Date::operator-(const Date& date)const
{
Date max = *this;
Date min = date;
int flag = 1;
if (*this < date)
{
max = date;
min = *this;
flag = -1;
}
int count = 0;
while (min != max)
{
min++;
count++;
}
return count * flag;
}
Date& Date::operator=(const Date& date)
{
if (this != &date)//注意用地址比较
{
_year = date._year;
_month = date._month;
_day = date._day;
}
return *this;
}
ostream& operator<<(ostream& out, const Date& date)
{
out << "year:" << date._year << "month:" << date._month << "day:" << date._day << endl;
return out;
}
istream& operator>>(istream& in, Date& date)
{
int year, month, day;
in >> year >> month >> day;
if (month > 0 && month < 13
&& day > 0 && day <= date.GetMonthDay(year, month))
{
date._year = year;
date._month = month;
date._day = day;
}
else
{
perror("非法日期");
assert(false);
}
return in;
}
3. Text.cpp
#include"DateClass.h"
void Text1()
{
Date d1(2024, 6, 26);
Date d2(2024, 6, 21);
cout << (d1 < d2) << endl; // 转换成cout << operator<(d1, d2);
//cout << operator<(d1, d2);
cout << (d1 < d2) << endl; // 转换成cout << d1.operator<(d2);
//cout << d1.operator<(d2);
}
void Text2()
{
Date d1(2024, 6, 26);
Date d2(2024, 6, 21);
cout << (d1 += 1);//此时输出:2024,6,27,d1被改变
cout << (d1 + 1);//此时输出:2024,6,28,d1未被改变
cout << (d1 -= 1);
cout << d1++;
cout << ++d1;
int gap = (d2 - d1);
cout << gap;
}
void Text3()
{
Date d1(2024, 6, 26);
Date d2(2024, 6, 21);
// 已经存在的两个对象之间复制拷贝 -- 运算符重载函数
d1 = d2;
cout << d1;
// 用一个已经存在的对象初始化另一个对象 -- 构造函数
Date d3(d1);
}
int main()
{
//Text1();
//Text2();
Text3();
return 0;
}
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。