目录
前言
这是一篇练习博客,在学习和初步熟悉了类的构建,以及类的六个默认构造函数之后,构建了一个日期类,对日期进行各种操作。
功能简介
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month);
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);
// 拷贝构造函数
// d2(d1)
Date(const Date& d);
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d);
// 析构函数
~Date();
// 日期+=天数
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;
};
类的构建
#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
int GetMonthDay(int year, int month)const;
//全缺省默认构造函数
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//拷贝构造函数,注意要加const,在遇到不能修改的数据时非常必要,临时变量具有常性
Date(const Date& d1)
{
_year = d1._year;
_month = d1._month;
_day = d1._day;
}
//赋值操作符重载
Date& operator=(const Date& d1)
{
_year = d1._year;
_month = d1._month;
_day = d1._day;
return *this;
}
//+=重载,日期加天数
Date& operator+=(int day)
{
_day += day;
int temp = 0;
while (_day > (temp = GetMonthDay(_year, _month)))
{
_day -= temp;
if (_month < 12)
{
_month++;
}
else
{
_year++;
_month = 1;
}
}
return *this;
}
//日期加天数, + 重载
Date operator+(int day)
{
Date d1(*this);
d1._day += day;
int temp = 0;
while (d1._day > (temp = GetMonthDay(d1._year, d1._month)))
{
d1._day -= temp;
if (d1._month < 12)
{
d1._month++;
}
else
{
d1._year++;
d1._month = 1;
}
}
return d1;
}
//日期减天数, -=
Date& operator-=(int day)
{
while (_day <= day)
{
if (_month == 1)
{
_year--;
_month = 12;
}
else
{
_month--;
}
day -= _day;
_day = GetMonthDay(_year, _month);
}
_day -= day;
return *this;
}
//日期减天数, -
Date operator-(int day)const
{
Date d1(*this);
while (d1._day <= day)
{
if (d1._month == 1)
{
d1._year--;
d1._month = 12;
}
else
{
d1._month--;
}
day -= d1._day;
d1._day = GetMonthDay(d1._year, d1._month);
}
d1._day -= day;
return d1;
}
//前置++
Date& operator++()
{
*this += 1;
return *this;
}
//后置++
Date operator++(int)
{
Date d1(*this);
*this += 1;
return d1;
}
//前置--
Date& operator--()
{
*this -= 1;
return *this;
}
//后置--
Date operator--(int)
{
Date d1(*this);
*this -= 1;
return d1;
}
//运算符重载>
bool operator>(const Date& d1)const
{
return (_year > d1._year)
|| (_year == d1._year && _month > d1._month)
|| (_year == d1._year && _month == d1._month && _day > d1._day);
}
//运算符重载==
bool operator==(const Date& d1)const
{
return (_year == d1._year && _month == d1._month && _day == d1._day);
}
//运算符重载>=
bool operator>=(const Date& d1)const
{
return *this > d1 || *this == d1;
}
//运算符重载<
bool operator<(const Date& d1)const
{
return !(*this >= d1);
}
//运算符重载<=
bool operator<=(const Date& d1)const
{
return !(*this > d1);
}
//运算符重载!=
bool operator!=(const Date& d1)const
{
return !(*this == d1);
}
//日期相减
int operator-(const Date& d1)const
{
int ret = 0;
if (*this <= d1)
{
Date d2(*this);
while (d2 != d1)
{
d2 += 1;
ret--;
}
return ret;
}
else
{
Date d2(d1);
while (d2 != *this)
{
d2 += 1;
ret++;
}
return ret;
}
}
private:
int _year;
int _month;
int _day;
};
int Date::GetMonthDay(int year, int month)const
{
static int MonthDayList[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
//int MonthDayList[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)))
{
return 29;
}
else
{
return MonthDayList[month];
}
}