您的位置:首页 > 编程语言 > C语言/C++

C++实现的简单日期类

2015-11-29 18:30 357 查看
主要实现了Date类的一些运算符重载

全部代码:

头文件 Date.h 类定义 成员函数声明

#include<assert.h>
#include<iostream>
using namespace std;
class Date
{
public:
//使用构造函数初始值列表来初始化  默认1900年1月1日
Date(int y=1900, int m=1, int d=1);
//拷贝构造函数 必须引用
Date(const Date & d);
void Display();
//析构函数
~Date();
//成员函数实现运算符重载
/*bool operator==(const Date& d)
{
return this->_year == d._year&&this->_month == d._month&&this->_day == d._day;
}*/
//友元函数实现运算符重载
//friend bool operator==(const Date &d1, const Date &d2);
bool operator==(const Date &d);
//  "="运算符重载  不能用友元
Date& operator=(const Date &d);
//运算符重载
//+ - -= +=  前置/后置++  前置/后置--
Date operator+(int day);
Date& operator+=(int day);
Date operator-(int day);
Date& operator-=(int day);
Date operator++();  //前置++
Date operator++(int);  //后置++
Date operator--();  //前置--
Date operator--(int);  //后置--
//静态函数
static bool Date::IsLeapYear(int year);
private:
int GetMonDay(int year, int month);
int _year;  //年
int _month;//月
int _day; //日
};


源文件 Date.cpp 成员函数实现 主要是一些运算符重载的练习

#include"Date.h"
Date::Date(const Date & d) :_year(d._year), _month(d._month), _day(d._day)
{
assert(_year > 1900);
assert(_month >0&&_month<13);
assert(_day>0 && _day <= GetMonDay(_year,_month));

}
Date::Date(int y, int m, int d) :_year(y), _month(m), _day(d){}
void Date::Display()
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
Date::~Date()
{
//cout << "析构函数!" << endl;
}
bool Date::operator==(const Date &d)
{
return (this->_year == d._year) && (this->_month == d._month) && (this->_day == d._day);
}
Date &Date::operator=(const Date & d)   //这样写可以保证 d1=d2=d3这样的表达式成立
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
return *this;
}
Date Date::operator+(int day)
{
Date tmp=*this;
int countDay=day;
//判断增加天数不超过本月
assert(countDay>= 0);
if (tmp._day + countDay <= GetMonDay(tmp._year, tmp._month))
{
tmp._day += countDay;
return tmp;
}
else  //超过了本月  要考虑未超出一年  超出一年
{
while (countDay--)
{
if (tmp._day != GetMonDay(tmp._year,tmp._month))
++tmp._day;
else
{
++tmp._month;
tmp._day = 1;  //置为1
if (tmp._month > 12)
{
++tmp._year;
tmp._month %= 12;
}
if (tmp._year > 4000)  //超过4000年就不能再计数了
{
tmp._year = 1900;
tmp._month = 1;
tmp._day = 1;
}
}

}
}
return tmp;
}
Date& Date::operator+=(int day)
{
*this = *this + day;  //调用 operator+ 函数
return *this;
}
Date Date::operator-(int day)
{
Date tmp = *this;
int countDay = day;
assert(day >= 0);
while (countDay--)
{
if (tmp._day != 1)
{
--tmp._day;
}
else
{
if (tmp._month != 1)  //不是1月
{
--tmp._month;  //减少一月
tmp._day=GetMonDay(tmp._year, tmp._month);
}
else  //是一月
{

--tmp._year;  //减少一年
if (tmp._year < 1900)  //如果到了1900年以前 终止 返回1900年1月1日
{
tmp._year = 1900;
tmp._month = 1;
tmp._day = 1;
return tmp;
}
tmp._month = 12;  //变为12月
tmp._day = GetMonDay(tmp._year, tmp._month);
}
}
}
return tmp;
}
Date& Date::operator-=(int day)
{
*this = *this - day;  //调用 operator- 函数
return *this;
}
Date Date::operator++()  //前置++
{

return *this+=1;
}
Date Date::operator++(int) //后置++
{
Date tmp = *this;
*this += 1;
return tmp;
}

Date Date::operator--()  //前置--
{
return *this -= 1;
}
Date Date::operator--(int) //后置--
{
Date tmp = *this;
*this -= 1;
return tmp;
}

//获取某一个月的天数
int Date::GetMonDay(int year, int month)
{
int MonDayNum[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
bool flag=IsLeapYear(year);
if (month == 2 && flag)
return MonDayNum[2]+1;  //闰年且是2月
else
return MonDayNum[month];
}

//
////友元函数的运算符重载
//bool operator==(const Date &d1, const Date &d2)
//{
//	return (d1._year == d2._year) && (d1._month == d2._month) && (d1._day == d2._day);
//}
//静态函数
bool Date::IsLeapYear(int year)
{
return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
}


源文件 Main.cpp 测试用例 测试所有函数

//测试
#include"Date.h"
void Test1()
{
Date d1(2015);
Date d2 = d1;
Date d3; //默认构造
d3.Display();
d3 = d2;
d3.Display();
//d2.Display();
}
void Test2()
{
Date d1(2015);
Date d2;
d1.Display();
d2.Display();
d2 = d1;
d2.Display();
}
void Test3()  //测试  + -
{
Date d1(2015);
Date d2 = d1 + 31;
Date d3 = d1 - 31;
//d3 = d1 - 23;
d2.Display();
d3.Display();
}
void Test4()   //测试  +=   -=
{
Date d1(2015);
Date d2(2015,3,1);
Date d3;

d1 += (31 + 28);
d2 += 3;
d3 = d2;
d3 -= 4;
d1.Display();//3月1日
d2.Display();//3月4日
d3.Display();//2月28日
}
void Test5()  //测试 前置后置++ 前置后置--
{
//Date d1(2015);
//Date d2(2012, 3, 1);
//Date d3= --d1;
//Date d4 =  d2--;
//d1.Display();  //2014年12月31日
//d2.Display();  //2012年2月29日
//d3.Display();  //2014年12月31日
//d4.Display();  //2012年3月1日

Date d1(2015,12,31);
Date d2(2012, 2, 28);
Date d3 = ++d1;
Date d4 = d2++;
d1.Display();   //2016 1 1
d2.Display();  	//2013 2 29
d3.Display();  	//2016 1 1
d4.Display(); //2012 2 28
}
int main()
{
Test5();
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: