🎁个人主页:我们的五年
🔍系列专栏:C++课程学习
🎉欢迎大家点赞👍评论📝收藏⭐文章
目录
🍩1.头文件
🍩2.实现文件:
🍩3.分析:
🍟3.1Data的构造函数和析构函数:
🍟3.2拷贝构造:
前言:
类学的差不多的,我们就日期类来对前面类的学习做一个巩固,然后学习一下运算符重载。
🍩1.头文件
#pragma once
#include<iostream>
#include<stdbool.h>
#include<assert.h>
#include<math.h>
using namespace std;class Data {friend ostream& operator<<(ostream& out, Data& d);friend istream& operator>>(istream& in, Data& d);
public://全缺省的构造函数Data(int year = 1, int month = 1, int day = 1) {_year = year;_month = month;_day = day;}//拷贝构造函数Data(const Data& d){_year = d._year;_month = d._month;_day = d._day;}//运算符重载bool operator==(const Data& d);bool operator<(const Data& d);bool operator>(const Data& d);bool operator<=(const Data& d);bool operator>=(const Data& d);bool operator!=(const Data& d);Data& operator = (const Data& d);//日期+天数Data& operator+=(int day);Data operator+(int day);//日期-天数Data& operator-=(int day);Data operator-(int day);//日期的前置和后置++Data& operator++();Data operator++(int);//日期的前置和后置--Data& operator--();Data operator--(int);//日期减日期=相差的天数int operator-(const Data& d);//void operator<<(ostream& out);//内联函数,获取year年第month的天数int GetDay(int year, int month) {assert(month > 0 && month < 13);static int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//闰年的二月返回29if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))return 29;return days[month];}void print() {cout << _year << "-" << _month << "-" << _day << endl;}int Getyear() {return _year;}int Getmonth() {return _month;}int Getday() {return _day;}//析构函数~Data() {_year = -1;_month = -1;_day = -1;}
private:int _year;int _month;int _day;
};
🍩2.实现文件:
#include"Data.h"
bool Data::operator==(const Data& d) {return _year == d._year&& _month == d._month&& _day == d._day;
}bool Data::operator<(const Data& d) {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;
}bool Data::operator<=(const Data& d) {return (*this == d) || (*this < d);
}bool Data::operator>(const Data& d) {return !(*this <= d);
}bool Data::operator>=(const Data& d) {return (*this == d) || (*this) > d;
}
bool Data::operator!=(const Data& d) {return !(*this == d);
}Data& Data::operator = (const Data& d) {_year = d._year;_month = d._month;_day = d._day;return *this;
}Data& Data::operator+=(int day) {if (day < 0){return *this -= -day;}_day += day;while (_day > GetDay(_year,_month)){_day -= GetDay(_year, _month);_month++;if (_month == 13){_month = 1;_year++;}}return *this;
}Data Data::operator+(int day) {Data a = *this;a += day;return a;
}
Data& Data::operator-=(int day) {if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (_month == 0){_month = 12;--_year;}_day += GetDay(_year, _month);}return *this;
}Data Data::operator-(int day) {Data a = *this;a -= day;return a;
}
Data& Data::operator++() {return *this += 1;
}Data Data::operator++(int) {Data a = *this;*this += 1;return a;
}Data& Data::operator--() {return *this -= 1;
}Data Data::operator--(int) {Data tmp = *this;*this -= 1;return tmp;
}int Data::operator-(const Data& d) {int day = 0;Data max = *this;Data min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}while (min != max){++min;++day;}return day*flag;
}ostream& operator<<(ostream& out,Data& d){out << d._year << "-" << d._month << "-" << d._day << endl;return out;
}istream& operator>>(istream& in, Data& d) {in >> d._year >> d._month >> d._day;return in;
}
🍩3.分析:
日期的基本成员就是年(year),月(month),和日(day)。
int _year;
int _month;
int _day;
🍟3.1Data的构造函数和析构函数:
构造函数我们用了全缺省的构造函数:
构造函数:
如果没有传参数的,我们就初始化为1。其实换成year的缺省值换为1970,moth换成1,day换成1,也是可以的,时间戳从这时候开始计时。
//全缺省的构造函数
Data(int year = 1, int month = 1, int day = 1) {
_year = year;
_month = month;
_day = day;
}
析构函数:
没有资源要进行清理,我们把类的属性都变为-1。
//析构函数
~Data() {
_year = -1;
_month = -1;
_day = -1;
}
🍟3.2拷贝构造:
加上const修饰一下d,只读。
//拷贝构造函数
Data(const Data& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}