用日期类增强对几个默认函数的的理解

首先写一个日期类:包括打印,根据月份判断天数
在这里插入图片描述

用类创建对象默认需要构造函数;且也要判断构造出的日期是否符合常理:
在这里插入图片描述

在程序结束时需要一个析构函数来释放空间,(日期类的对象不会开空间这里只是写出来演示一下)
在这里插入图片描述
利用拷贝构造创建新对象
在这里插入图片描述
运用运算符重载,实现日期之间的比较,和日期+=天数 日期+天数
1、判断两日期是否相等在这里插入图片描述
2、日期+=天数
在这里插入图片描述

3、日期+天数(不改变原来的日期)直接复用+=
在这里插入图片描述
其他的也一样,
总代码:
date.h

#pragma once
#include<iostream>
#include<assert.h>using namespace std;class Date{public:// 获取某年某月的天数int GetMonthDay(int year, int month);//打印void Print();// 全缺省的构造函数Date(int year = 1900, int month = 1, int day = 1);// 拷贝构造函数Date(const Date& d);// 析构函数~Date();// 赋值运算符重载Date& operator=(const Date& d);// 日期+=天数Date& operator+=(int day);//日期+天数Date operator+(int day);// 日期-天数Date operator-(int day);// 日期-=天数Date& operator-=(int day);// 前置++Date& operator++();// 后置++Date operator++(int);// 后置--Date operator--(int);// 前置--Date& operator--();// >运算符重载bool operator>(const Date& d);// ==运算符重载bool operator==(const Date& d);// !=运算符重载bool operator != (const Date& d);// >=运算符重载bool operator >= (const Date& d);// <运算符重载bool operator < (const Date& d);// <=运算符重载bool operator <= (const Date& d);// 日期-日期 返回天数int operator-(const Date& d);private:int _year;int _month;int _day;};

date.cpp

#define _CRT_SECURE_NO_WARNINGS#include"date.h"// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{assert(year >= 1 && month >= 1 && month < 13);int arr[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 arr[month];
}//打印
void Date::Print()
{cout << _year << "-" << _month << "-" << _day << endl;
}// 全缺省的构造函数
Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;if (year < 1 || month < 1 || month>12 || day<1 || day>GetMonthDay(year, month)){assert(false);}
}// 拷贝构造函数
Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}// 析构函数
Date::~Date()
{_year = 0;_month = 0;_day = 0;
}// >运算符重载
bool Date::operator>(const Date& d)
{if (_year > d._year){return true;}else if (_year == d._year && _month > d._month){return true;}else if (_year == d._year && _month == d._month && _day > d._day){return true;}return false;
}// ==运算符重载
bool Date::operator==(const Date& d)
{if (_year == d._year && _month == d._month && _day == d._day){return true;}return false;
}
// !=运算符重载
bool Date::operator != (const Date& d)
{return !(*this == d);
}
// >=运算符重载
bool Date::operator >= (const Date& d)
{return *this > d || *this == d;
}// <运算符重载
bool Date::operator < (const Date& d)
{return !(*this >= d);
}// <=运算符重载
bool Date::operator <= (const Date& d)
{return !(*this > d);
}// 赋值运算符重载(两个已存在的对象进行赋值)
Date& Date::operator=(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;return *this;
}// 日期+天数
Date Date::operator+(int day)//运算后不会改变原来的日期
{Date tmp(*this);tmp._day += day;while(tmp._day > GetMonthDay(tmp._year, tmp._month)){tmp._day -=  GetMonthDay(tmp._year, tmp._month);tmp._month++;if (tmp._month == 13){tmp._month = 1;tmp._year++;}}return tmp;
}// 日期+=天数
Date& Date::operator+=(int day)
{*this = *this + day;return *this;
}// 日期-=天数
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)
{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)
{Date tmp(*this);*this -= 1;return tmp;
}
// 前置--
Date& Date::operator--()
{*this -= 1;return *this;
}// 日期-日期 返回天数int Date::operator-(const Date& d)
{int flag = 1;Date max = *this;Date min = d;if (*this<d){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++min;++n;}return n * flag;
}