**
- 类是一个数据类型,它的变量就是一个对象,除了具有成员函数,还能容纳数据值。所以,在一个C++程序中,类的定义应该是一个数据类型定义,它描述变量能容纳哪些种类的值,以及成员函数有哪些。而下面定义的Data类型是一个类定义,这个类的容纳对象是日期值。
-
在生活中我们想知道某天之后或某天之前是哪一天?或者距离这一天距离那天还有多少天?
下面代码就是一个简单的模拟日历的计算器
**
代码如下:
#include <iostream>
using namespace std;
//日期类
class Date
{
public:
Date(int year = 2016, int month = 10, int day = 29)
: _year(year)
, _month(month)
, _day(day)
{
if (year < 0|| (month<0 || month>12) || (day<0 || day> Getmonthday(_year, _month)))
{
exit(1); //日期不合法,退出
}
}
Date& operator=(const Date& d)
{
if (*this != d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
~Date()
{}
int Getmonthday(int year, int month)
{
int arr[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))//考虑平年闰年二月份天数不同
{
arr[1] += 1;
return arr[month];
}
return arr[month];
}
//前置++
Date& operator++()
{
_day += 1;
if (_day > Getmonthday(_year, _month))
{
_month += 1;
if (_month > 12)
{
_year += 1;
_month = 1;
}
}
return *this;
}
// 后置++,返回旧值,先赋值,后自增
Date operator++(int)
{
return *this;
}
//前置--
Date& operator--()
{
_day -= 1;
if (_day == 0)
{
_month -= 1;
if (_month == 0)
{
_year -= 1;
_month = 12;
}
_day = Getmonthday(_year, _month);//天数也需要变化;
}
return *this;
}
//后置--
Date operator--(int)
{
return *this;
}
//day天之后的日期
Date operator+(int day)
{
if (day<0)
{
return *this - (-day);
}
_day += day;
while (_day > Getmonthday(_year, _month))
{
_day = _day - Getmonthday(_year, _month);
_month += 1;
if (_month > 12)
{
_year += 1;
_month = 1;
}
}
return *this;
}
// days天之前的日期
Date operator-(int day)
{
if (day < 0)
{
return *this + (-day);//如果减去天数小于0,则相当于向后+多少天
}
_day -= day;
while (_day <= 0)
{
_month -= 1;
if (_month == 0)
{
_month = 12;
_year -= 1;
}
int k = Getmonthday(_year, _month);
_day = k - (-_day);
}
return *this;
}
// 两个日期之间的距离
int operator-(const Date& d)
{
int count = 0;
Date min = *this;
Date max = d;
if (min > max)
{
Date temp = min;
min = max;
max = temp;
}
while (min != max)
{
max.operator--(); //大的日期自减,减一天count加一天,两个日期正好相等时,返回count
count++;
}
return count;
}
//逻辑运算符,与或非,&& || !
bool operator==(const Date& d)
{
return (_year == d._year && _month == d._month && _day == d._day);
}
bool operator!=(const Date& d)
{
return (_year != d._year || _month != d._month || _day != d._day);
}
bool operator>(const Date& d)
{
if (_year > d._year)
{
return true;
}
else
{
if (_month > d._month)
{
return true;
}
else
{
if (_day > d._day)
{
return true;
}
else
{
return false;
}
}
}
}
bool operator<(const Date& d)
{
if (_year < d._year)
{
return true;
}
else
{
if (_month < d._month)
{
return true;
}
else
{
if (_day < d._day)
{
return true;
}
else
{
return false;
}
}
}
}
void Display()
{
cout << _year << " " << _month << " " << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
void FunTest()
{
Date d1(2017, 10, 29);
d1.Display();
Date d2(2017, 10, 20);
int ret = d2 - d1;
cout << ret << endl;
}
int main()
{
FunTest();
getchar();
return 0;
}