C++——类和对象——定义一个日期类Date
实现一个Date类
话不多说,直接上代码:
函数声明:
#DateClass.h#
#pragma once
#include<iostream>
using namespace std;
class Date
{
protected:
friend ostream& operator<<(ostream& out, const Date& d);
public:
//全缺省构造
Date(int year = 1999, int month = 1, int day = 1);
//拷贝构造
Date(const Date& d);
//赋值运算符重载
Date& operator=(const Date& d);
//析构
~Date();
//+=运算符重载
Date& operator+=(int n);
//+重载
Date operator+(int n);
//-运算符重载
Date operator-(int n);
//-=运算符重载
Date& operator-=(int n);
//前++重载
Date& operator++();
//后++重载
Date& operator++(int);
//后--重载
Date& operator--(int);
//前--重载
Date& operator--();
// >运算符重载
bool operator>(const Date& d);
// ==运算符重载
bool operator==(const Date& d);
// >=运算符重载
inline 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);
//获取某年某月的天数、
int GetMonthDay(int year, int month);
//获取某年的天数
int GetYearDay(int year);
//获取星期
int GetWeek(int year, int month, int day);
//计算某年某月有多少天
int Countday()
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "." << d._month << "." << d._day;
return out;
}
void Swap(Date a, Date b)
{
Date tmp = a;
a = b;
b = tmp;
}
实现上面的接口:
#DateClass.cpp#
#pragma once
#include"DateClass.h"
//全缺省构造
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
//拷贝构造
Date::Date(const Date& d)
{
this->_day = d._day;
_month = d._month;
_year = d._year;
}
//赋值运算符重载
Date& Date::operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
//析构
Date::~Date()
{}
//+=运算符重载
Date& Date::operator+=(int n)
{
Date tmp = *this + n;
*this = tmp;
return *this;
}
//+重载 日期+天数
Date Date::operator+(int n)
{
int year = _year;
int month = _month;
int day = _day;
int days = GetMonthDay(year, month);
while (day + n > days)
{
month++;
if (month > 12)
{
year++;
month = 1;
}
n -= days;
days = GetMonthDay(year, month);
}
day += n;
return Date(year, month, day);
}
//-运算符重载 日期-天数
Date Date::operator-(int n)
{
int day = _day;
int month = _month;
int year = _year;
int days;
while (n>=day)
{
month--;
if (month<1)
{
year--;
month = 12;
}
days = GetMonthDay(year, month);
n -= days;
}
day -= n;
return Date(year, month,day);
}
//-=运算符重载
Date& Date::operator-=(int n)
{
Date tmp = *this - n;
*this = tmp;
return *this;
}
//前++重载
Date& Date::operator++()
{
*this += 1;
return *this;
}
//后++重载
Date& Date::operator++(int)
{
Date tmp = *this;
++(*this);
return tmp;
}
//前--重载
Date& Date::operator--()
{
*this -= 1;
return *this;
}
//后--重载
Date& Date::operator--(int)
{
Date tmp = *this;
--(*this);
return tmp;
}
//==重载
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)
{
if (_year > d._year ||
(_year == d._year && _month > d._month) ||
(_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)
{
if (_year < d._year ||
(_year == d._year && _month < d._month) ||
(_year == d._year && _month == d._month && _day < d._day))
return true;
return false;
}
// >=运算符重载
inline bool Date::operator >= (const Date& d)
{
return !(*this < d);
}
// !=运算符重载
bool Date::operator != (const Date& d)
{
return !(*this == d);
}
//日期-日期 返回天数
int Date::operator-(const Date& d)
{
int dy = 0;
if (_year < d._year)
{
Swap(*this, d);
}
//如果_year-d._year>1,计算d._year+1/1/1到_year/1/1的天数
if (_year >= d._year)
{
if (_year - d._year >= 1)
{
int tail = _year - d._year;
for (int i = 1; i < tail; i++)
{
dy += GetYearDay(d._year + i);
}
//计算_year/1/1到_year/_month/1的天数
for (int i = 1; i < _month; i++)
{
dy += GetMonthDay(_year, i);
}
dy += _day;//在加上_day的天数
//计算d._year/d._month/GetMonthDay(d._year,d.month)-1的天数
for (int i = 1; i <= 12 - d._month; i++)
{
dy += GetMonthDay(d._year, d._month + i);
}
dy += (GetMonthDay(d._year, d._month) - d._day + 1);
}
else if (_year == d._year)
{
if (_month > d._month)
{
for (int i = 1; i < _month - d._month - 1; i++)
dy += GetMonthDay(_year, GetMonthDay(_year, d._month) + i);
dy += (GetMonthDay(_year, d._month) - d._day + 1);
dy += _day;
}
dy += (_day - d._day);
}
else
{
dy += (_day - d._day);
}
}
if (dy < 0)
dy = -dy;
return dy;
}
//获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
break;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
return 29;
else
{
return 28;
}
break;
default:
break;
}
/*static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = days[month];
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
day += 1;
}
return day;*/
}
int Date::GetYearDay(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return 366;
}
return 365;
}
//计算某年某月有多少天
int Date::Countday()
{
int day=0;
for(int i=1;i<_month;i++)
{
day+=GetMonthDay(_year,_month-i);
}
day+=_day;
return day;
}
测试类可以按自己需求去自己写。