当前位置: 首页 > news >正文

【C++】基础类之日期类

【C++】基础类之日期类

  • 1. DateClass.h
  • 2. DateClass.cpp
  • 3. Text.cpp

1. DateClass.h

#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;}elsereturn 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;
}

http://www.mrgr.cn/news/17041.html

相关文章:

  • c++140namespace和ioterm
  • RESTful API介绍
  • Linux C++ 开发6 - GDB调试入门指南
  • Vue2.0
  • 【精选】基于python的3D打印技术专利分析系统(全网独一无二,最新定制)
  • TCP流套接字编程
  • ASCII、GB2312、Unicode和UTF-8
  • 热力学温度
  • 【面试经验】京东-技术产品经理一面
  • 好用的宠物浮毛清理神器,希喂、IAM、范罗士宠物空气净化器大揭秘
  • c++11新特性-lambda表达式
  • 时序约束进阶二:set_max_time_borrow详解
  • 【网络安全】XSS之HttpOnly防护(附实战案例)
  • 华为云征文|下一代云服务器,Flexus X实例选购指导
  • (3) 插入排序
  • 个人笔记总结
  • minio最新源码编译(处理安全扫描中跨域访问、.js.map等不安全问题) 版本:RELEASE.2024-06-26T01-06-18Z
  • SOMEIP_ETS_076: Wrong_Method_ID
  • 随笔1:数学建模与数值计算
  • 报错记录3:imx6ull适配ov2640摄像头无法获取默认摄像头分辨率与格式参数