学习类和对象时候练习的一个日期类,适合初学者,供大家参考。
Date.h
#pragma once
#include<iostream>
#include<assert.h>
class Date
{
//友元函数 -- 这个函数内部可以使用Date对象私有保护的成员
friend std::ostream& operator<<(std::ostream& out, const Date& d);
friend std::istream& operator>>(std::istream& in, Date& d);
public:
// 频繁调用写成内联
int GetMonthDay(int year, int month)
{
//静态频繁调用
static int days[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 days[month];
}
}
bool CheckDate(Date & d)
{
return d._year >= 1 &&
d._month >= 1 && d._month <= 12
&& d._day >= 1 && d._day <= GetMonthDay(_year, _month);
}
//全缺省构造函数
Date(int year = 1970, int month = 1, int day = 1);
//拷贝构造
Date(const Date& d);
//赋值运算符重载
Date& operator=(const Date& d);
//析构函数
~Date();
//取地址运算符重载
Date* operator&()
{
return this;
}
//通过这样设置可以保证,类外面的成员只能访问地址,不能修改地址。
const Date* operator&()const
{
return this;
}
//运算符重载
//日期+=天数
Date& operator+=(int day);
//日期+天数
Date operator+(int day) const;
//日期-天数
Date operator-(int day) const;
//日期-天数
Date& operator-=(int day);
// 前置++
Date& operator++();
//后置 ++
Date operator++(int);
// 前置--
Date& operator--();
//后置 --
Date operator--(int);
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;
void Print() const;
//void operator<<(std::ostream& out);
//日期减日期
int operator-(const Date& d) const;
private:
int _year;
int _month;
int _day;
};
// 因为会频繁的调用,所以搞成内联函数
//又因为内联函数声明和定义最好不要分离,所以写一块
//inline std::ostream& operator<<(std::ostream& out, const Date& d);
//<<运算符重载
// 第二个参数是传this 用的
inline std::ostream& operator<<(std::ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << std::endl;
return out;
}
//流提取
inline std::istream& operator>>(std::istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
assert(d.CheckDate(d));
return in;
}
Date.cpp
#include<iostream>
#include<assert.h>
#include"Date.h"
using namespace std;
// 构造
//本身日期就有问题
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
//日期非法
assert(CheckDate(*this));
}
// 拷贝构造
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
cout << "Date" << endl;
}
//赋值运算符重载
Date& Date::operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
//析构函数
Date::~Date()
{
cout << "~Date" << endl;
}
//运算符重载
//日期+=天数
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 > 12)
{
_year++;
_month -= 12;
}
}
return *this;
}
//日期+天数
Date Date::operator+(const int day) const
{
Date d(*this);
return d += day;
}
//日期-=天数
Date& Date::operator-=(const int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
//注意1月-1,变成0月
if (1 == _month)
{
_month = 12;
_year--;
}
else
{
_month--;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
//日期-天数
Date Date::operator-(const int day) const
{
Date d(*this);
return d -= day;
}
// 前置++
Date& Date::operator++()
{
*this += 1;
return *this;
}
//后置 ++
Date Date::operator++(int)
{
//先用再加
Date tem(*this);
*this += 1;
return tem;
}
//前置--
Date& Date::operator--()
{
return *this -= 1;
}
//后置--
Date Date::operator--(int)
{
Date tem(*this);
*this -= 1;
return tem;
}
bool Date::operator>(const Date& d) const
{
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;
}
else
{
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);
}
bool Date::operator<=(const Date& d) const
{
return !(*this > d);
}
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
//打印日期
void Date::Print() const
{
cout << _year << "-" << _month << "-" << _day << endl;
}
//日期减日期
int Date::operator-(const Date& d) const
{
Date Max(*this);
Date Min(d);
int flag = 1;
if (*this < d)
{
Max = d;
Min = *this;
flag = -1;
}
int day = 0;
while (Min != Max)
{
++Min;
++day;
}
return day * flag;
}
因为代码逻辑并不复杂,这里就不再赘述逻辑,请大家谅解。