C++是基于面向过程与面向对象的语言,类和对象是其中比较重要的部分,也是较为难理解的部分,这一部分需要大量的代码练习。下面为类和对象—日期类。主要功能相当于一个日期计算器,可以实现日期的推后、提前、日期差等功能。
#define _CRT_SECURE_NO_WARNINGS 1
//日期类
#include <iostream>
#include <stdlib.h>
using namespace std;
class Date
{
public:
Date(int year, int month, int day) //构造函数
{
_year = year;
_month = month;
_day = day;
}
Date(const Date & d) //拷贝构造函数
{
_year = d._year;
_month = d._month;
_day = d._day;
}
void Display() //打印
{
cout << _year << "-" << _month << "-" << _day << endl;
}
~Date() //析构函数
{ }
bool operator==(const Date & d) //相等
{
return (_year == d._year)&& (_month == d._month)&& (_day == d._day);
}
bool operator<(const Date & d) //小于
{
return (_year < d._year)|| (_year == d._year && _month < d._month)
|| (_year == d._year && _month == d._month && _day < d._day);
}
bool operator<=(const Date & d) //小于等于
{
return *this < d || *this == d;
}
int _GetMonthDay(int year, int month) //判断每月的天数
{
int day;
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
{
if (month == 2)
{
day = 29;
}
else if (month == 1 || month == 3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12)
{
day = 31;
}
else
{
day = 30;
}
}
else
{
if (month == 2)
{
day = 28;
}
if (month == 1 || month == 3 || month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12)
{
day = 31;
}
else
{
day = 30;
}
}
return day;
}
Date operator+(int day) //推后day
{
if (day < 0)
{
return *this - (-day);
}
Date tmp(*this);
tmp._day = tmp._day + day;
while (tmp._day > _GetMonthDay(tmp._year, tmp._month))
{
tmp._day -= _GetMonthDay(tmp._year, tmp._month);
if (tmp._month == 12)
{
tmp._year++;
tmp._month = 1;
}
else
{
tmp._month++;
}
}
return tmp;
}
Date & operator+=(int day) //推后多少天
{
*this = *this + day;
return *this;
}
Date & operator++() //加1天,返回增加后的日期
{
*this += 1;
return *this;
}
Date operator++(int) //加一天,返回增加前的日期
{
Date tmp(*this);
*this += 1;
return tmp;
}
Date operator-(int day) //提前day天
{
if (day < 0)
{
return *this + (-day);
}
Date tmp(*this);
tmp._day -= day;
while (tmp._day <= 0)
{
if (tmp._month == 1)
{
tmp._month = 12;
tmp._year--;
}
else
{
tmp._month--;
}
tmp._day -= _GetMonthDay(tmp._year, tmp._month);
}
return tmp;
}
Date & operator-=(int day) //减等于
{
*this = *this - day;
return *this;
}
Date & operator--() //减减、后置
{
*this -= 1;
return *this;
}
Date operator--(int) //减减、前置
{
Date tmp(*this);
*this -= 1;
return tmp;
}
bool operator!=(const Date & d) //不等于
{
return (_year != d._year)|| (_year == d._year && _month != d._month)
|| (_year == d._year && _month == d._month && _day != d._day);
}
int operator-(const Date & d) //两个日期的差
{
int flag = 1;
Date max = *this;
Date min = d;
if (max < min)
{
swap(max._year, min._year);
swap(max._month, min._month);
swap(max._day, min._day);
flag = -1;
}
int day = 0;
while (max != min)
{
++min;
++day;
}
return day*flag;
}
private:
int _year;
int _month;
int _day;
};
//测试用例
void Test1()
{
Date d1(2015, 1, 1);
d1 += 35;
d1.Display();
d1 += 10;
d1.Display();
}
void Test2()
{
Date d2(2016, 1, 15);
d2 -= 30;
d2.Display();
d2 -= 10;
d2.Display();
}
void Test3()
{
Date d3(2016, 2, 9);
d3--;
d3.Display();
d3++;
d3.Display();
}
//主函数
int main()
{
//Test1();
Test2();
//Test3();
system("pause");
return 0;
}
本文出自 “无心的执着” 博客,转载请与作者联系!