一、利用多文件实现日期类
当我们了解了类中默认的6个成员函数,我们就可以利用这些函数实现一个简单的类完成日期之间的计算。比如,计算两个不同时间之间隔了多少天,n天之后是几年几月几号,等等
首先创建一个头文件Date,h,包含头文件,命名空间,函数声明,和定义类。
#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
//获得每个月天数的函数
int GetMonthDay(int year, int month);
void Print();//打印函数
Date(int year = 1900, int month = 1, int day = 1);
//全缺省的构造函数
Date& operator=(const Date& d);
~Date();//析构函数
Date(const Date& d);
//拷贝构造函数
Date& operator+=(int days);//让自定义类实现运算符+=
Date operator+(int day);//让自定义类实现运算符+
Date& operator-=(int days);//让自定义类实现运算符-=
Date operator -(int days);//让自定义类实现运算符-
Date operator++(int);//让自定义类实现运算符后置++
Date& operator++();让自定义类实现运算符前置++
Date& operator--();//让自定义类实现运算符前置--
Date operator--(int);//让自定义类实现运算符后置--
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);//让自定义类实现运算符-
private:
int _year;
int _month;
int _day;
};
创建一个Date.cpp定义函数
1、实现某年某月的天数函数GetMonthDay(int year,int month)
一年有12个月,只有每年的2月和年份有关,其他月份的天数都是固定的,将没一月的天数放在一个数组里,
然后根据月份得到天数,再判断年份,是闰年则2月天数加一,否则不变
int Date::GetMonthDay(int year, int month)
{
static int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int monthdays = days[month];
if (month == 2)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
monthdays += 1;
}
}
return monthdays;
}
2、打印函数Print()
void Date::Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
3、构造函数Date(const Date& d)
全缺省的构造函数,编译器自己调用
Date::Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
if ((_year < 0)
|| (_month > 12 || _month < 0) ||
(_day < 0 || _day>GetMonthDay(_year, _month)))
{
cout << "非法日期" << endl;
}
}
4、析构函数
日期类可以你用写析构函数,编译器默认生成的够用。
Date::~Date()//析构函数
{
}
5、拷贝构造函数
Date::Date(const Date& d)
//拷贝构造函数
{
_year = d._year;
_month = d._month;
_day = d._day;
}
6、运算符重载==(bool operator==(Date& d))
判断两个日期是否是同一天
bool Date::operator==(const Date& d)
{
return _year == d._year &&
_month == d._month &&
_day == d._day;
}
7、运算符重载=(bool operator=(const Date& d))
将d的内容拷贝给的d1(*this)
Date& Date::operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
8、运算符重载+=(Date& operator+=(const Date& d))
Date& Date::operator+=(int days)
{
if (days < 0)
{
return *this -= -days;
}
else
{
_day += days;
while(_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
}
给一个日期加上天数,得到一个新日期,首先算出天数和,然后判断天数和是否大于这个月的天数,如果大于,减去这个月的天数,再判断减去之后的天数和下个月的天数,前者如果小就可以却定日期了。很抽象,画图理解一下:
9、运算符重载+(Date operator+=(const Date& d))
Date Date::operator+(int day)
{
Date ret(*this);
ret += day;
return ret;
}
10、运算符重载-=(Date& operator-=(const Date& d))
Date& Date::operator-=(int days)
{
int monthday = GetMonthDay(_year,_month);
_day -= days;
while (_day < 0)
{
_day += GetMonthDay(_year, _month - 1);
_month--;
if (_month == 0)
{
_year--;
_month = 12;
}
}
return *this;
}
11、运算符重载-(Date operator-=(const Date& d))
Date Date::operator -(int days)
{
Date ret(*this);
ret -= days;
return ret;
}
12、运算符重载后置++(Date operator++(const Date& d))
Date Date::operator++(int)
{
Date ret(*this);
ret += _day;
return ret;
}
13、运算符重载前置++(Date& operator++(const Date& d))
Date& Date::operator++()
{
this->_day += 1;
return *this;
}
14、运算符重载前置--(Date& operator++(const Date& d))
Date& Date::operator--()
{
_day -= 1;
return *this;
}
15、运算符重载后置--(Date& operator--(const Date& d))
Date Date::operator--(int)
{
Date ret(*this);//调用拷贝构造函数
ret._day -= 1;
return ret;
}
16、大于,小于,大于等于,小于等于
bool Date::operator>(const Date& d)
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month > d._month)
{
return true;
}
else if (_month == d._month)
{
if (_day > d._day)
{
return true;
}
}
}
return false;
}
bool Date::operator==(const Date& d)
{
return _year == d._year &&
_month == d._month &&
_day == d._day;
}
inline bool Date::operator>=(const Date& d)
{
return (*this == d )|| (*this > d);
}
bool Date::operator<(const Date& d)
{
return !(*this >= d);
}
bool Date::operator<=(const Date& d)
{
return !(*this > d);
}
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
17、日期减日期
int Date::operator-(const Date& d)
{
int flag = 1;
Date max=*this;
Date min=d;
if (*this < d)
{
min = *this;
max = d;
flag = -1;
}
int n=0;
while (min < max)
{
n++;
min._day++;
}
return n*flag;
}
三、测试文件text.c,主要负责d代码测试。
#include"Date.h"
int main()
{
Date d1(2010, 5, 16);
Date d2(2010, 5, 21);
int c(d1 - d2);
cout << c << endl;
d1.Print();
return 0;
}
这里只是测试了一点点。