c++实现日期类(class Date) 构造函数 拷贝构造 操作符重载(输入输出 比较操作 算数运算 自增自减)

来源:互联网 发布:nginx docker 负载均衡 编辑:程序博客网 时间:2024/06/02 04:11


注释比较详细,可以直接跑起来,直接上代码(vs2012 win7)


一、头文件


/**************Date.h***************/#pragma once#include <iostream>using namespace std;class Date{private:int my_iYear;int my_iMonth;int my_iDay;int GetOneMonthDays(int year, int month)const;//获取某个月的天数public:Date():my_iYear(1900),my_iMonth(1),my_iDay(1){}Date(int year, int month, int day);// 构造函数Date(const Date& d);       // 拷贝构造函数~Date(){}       // 析构函数int GetYear()const;// 返回年份int GetMonth()const;// 返回月份int GetDay()const;// 返回天数void Print()const;        // 输出日期bool IsLeapYear()const;// 判断当前对象年是否是闰年bool IsLeapYear(const int y)const;  // 判断指定年份是否是闰年// 操作符重载部分// 天数 + 日期friend Date operator+(const  int d, const Date date);// 日期 +  天数friend Date operator + (const Date date, const   int d);// 前置 ++ friend Date& operator ++ (Date& date);// 后置 ++   多一个int参数与前置区别friend Date operator ++ (Date& date, int);// 重载 +=friend Date operator +=(Date& date, const int d);// 日期 - 天数friend Date operator - (const Date date, const int d);// 天数 - 日期friend Date operator - (const int d, const Date date);// 前置 --friend Date& operator -- (Date& date);// 后置 -- friend Date operator -- (Date& date, int);// 重载 -=friend Date operator -=(Date& date, const int d);// 日期 - 日期friend int operator - (const Date a, const Date b);// 重载比较操作符friend bool operator< (const Date a, const Date b);friend bool operator<= (const Date a, const Date b);friend bool operator> (const Date a, const Date b);friend bool operator>= (const Date a, const Date b);friend bool operator== (const Date a, const Date b);friend bool operator!= (const Date a, const Date b);// 重载输出运算符 <<friend ostream& operator <<(ostream& _out, const Date& date);// 重载输入运算符 >>friend istream& operator >> (istream& _out,  Date& date);};



二、实现详情Date.cpp


#include "Date.h"//构造函数Date::Date(int year = 1900,int month = 1, int day = 1):my_iYear(year),my_iMonth(month),my_iDay(day){//检测年份月份天数的合法性 if (  year<= 0  ||      (month<=0 || month>12) ||      (day <= 0 || day>GetOneMonthDays(year, month)) ) {my_iYear = 1900;my_iMonth = 1;my_iDay = 1;}}//拷贝构造函数Date::Date(const Date& d){my_iYear = d.my_iYear;my_iMonth = d.my_iMonth;my_iDay = d.my_iDay;}//获取某月中的天数int Date::GetOneMonthDays(int year, int month)const{// a[1] - a[12] 表示非闰年每月天数int a[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};// 二月份天数做特殊处理 平年28 闰年29天if (month == 2){a[2] += IsLeapYear(year);}return a[month];}// 返回年份int Date::GetYear()const{return my_iYear;}// 返回月份int  Date::GetMonth()const{return my_iMonth;}// 返回天数int  Date::GetDay()const{return my_iDay;}// 输出日期void  Date::Print()const{cout << my_iYear << " 年 " << my_iMonth << " 月 " << my_iDay << " 日 " <<endl;}// 判断当前年是否是闰年bool  Date::IsLeapYear()const{if (my_iYear%400 == 0 || (my_iYear%4==0 && my_iYear%100!=0)){return true;}return false;}// 判断指定年份是否是闰年bool  Date::IsLeapYear(const int year)const{if (year%400 == 0 || (year%4==0 && year%100!=0)){return true;}return false;}// 操作符重载部分// 天数 + 日期Date operator+(const  int d, const Date date){Date tmpDate = date;if (0 == d)  return date;   // 加的天数为0 返回本身的值// 获取当前对象对应月份的天数int days; if (d > 0){tmpDate.my_iDay += d;       while (tmpDate.my_iDay > (days = tmpDate.GetOneMonthDays(tmpDate.my_iYear, tmpDate.my_iMonth)))   //加天数后对不合理值处理{//2010.3.12  + 30   2012.3.42 > 31  2012.4.42 - 31//减去该月的天数tmpDate.my_iDay -= days;tmpDate.my_iMonth ++; //  月份增加一个月if (tmpDate.my_iMonth > 12) //处理月份不合理{tmpDate.my_iYear ++; tmpDate.my_iMonth = 1;   //超过12后年份++ 月份置1}}}else{return date - (0 - d);}return tmpDate;}// 日期 +  天数Date operator + (const Date date, const  int d){return d + date;}// 前置 ++ Date& operator ++ (Date& date){date = date  + 1;return date;}// 后置 ++   多一个int参数与前置区别Date operator ++ (Date& date, int){Date temp = date;date =  date + 1;return temp;}// 重载 +=Date operator +=(Date& date, const int d){date = date + d;return date;}// 日期 - 天数Date operator - (const Date date, const int d){Date tempDate = date;if (0 == d) return date;if ( d>0 ){tempDate.my_iDay -= d;while (tempDate.my_iDay <= 0){--tempDate.my_iMonth;if (tempDate.my_iMonth == 0){--tempDate.my_iYear;tempDate.my_iMonth = 12;}tempDate.my_iDay += tempDate.GetOneMonthDays(tempDate.my_iYear, tempDate.my_iMonth);}}else{return date + ( - d);}return tempDate;}// 天数 - 日期Date operator - (const int d, const Date date){return date - d;}// 前置 --Date& operator -- (Date& date){date = date - 1;return date;}// 后置 --Date operator -- (Date& date, int){Date temp = date;date = date - 1;return temp;}// 重载 -=Date operator -=(Date& date, const int d){date = date - d;return date;}// 日期 - 日期intoperator - (const Date a, const Date b){int sumDay = 0;if (a==b){return 0;}else if(a>b){Date temp = b;while (temp!=a){temp++;sumDay++;}}else // a<b{Date temp = a;while (temp!=b){temp++;sumDay++;}}return sumDay;// 2016.1.1 - 2015.12.20}//重载比较操作符部分// 判相等bool operator== (const Date a, const Date b){// 同年同月同日return (a.my_iYear == b.my_iYear && a.my_iMonth == b.my_iMonth && a.my_iDay == b.my_iDay);}// 判不等bool operator!= (const Date a, const Date b){// 对判相等取反即可 a和b相等时 取反 不相等判断为假return !(a==b);}// 判小于bool operator< (const Date a, const Date b){//先处理不小于 2016 10 29 2016 10 29   同年看月 同年同月看日if (a.my_iYear > b.my_iYear||(a.my_iYear == b.my_iYear && a.my_iMonth > b.my_iMonth)||(a.my_iYear == b.my_iYear && a.my_iMonth == b.my_iMonth && a.my_iDay >= b.my_iDay)){return false;}return true;}// 判小于等于bool operator<= (const Date a, const Date b){if (a<b ||a==b){return true;}return false;}// 判大于bool operator> (const Date a, const Date b){return !(a<=b);}// 判大于等于bool operator>= (const Date a, const Date b){if (a>b || a==b){return true;}return false;}// 重载输出运算符 <<std::ostream& operator<<(ostream& _out, const Date& date){_out << date.GetYear() << " 年 " << date.GetMonth() << " 月 " << date.GetDay() << " 日 " <<endl;return _out;}// 重载输入运算符 <<std::istream& operator >> (istream& _in, Date& date){int year = 0,month = 0,day = 0;cin >> year >> month >> day;Date temp(year,month,day);date = temp;return _in;}



三、主函数,做了一些基本测试(脸滚键盘)Test.cpp


#include "Date.h"int main(){Date d1;d1.Print();cout << "----------------------1------------------"<< endl;Date d2(2016, 10 , 1);d2.Print();++d2;d2.Print();--d2;d2.Print();d2--;d2.Print();d2++;d2.Print();cout << "---------------------2------------------"<< endl;Date d3(2016,1,1);d3.Print();d3 += 10;d3.Print();d3 -= 10;d3.Print();d3 = d3 - 3;d3.Print();cout << "----------------------3------------------"<< endl;bool state;Date d4(2016,10,9);Date d5(2016,10,10);state = d4<d5;cout << state << endl;cout << "----------------------4------------------"<< endl;Date d6(2008,4,1);Date d7(2016,1,1);cout << d6 - d7<<endl;//2831cout << d7 - d6 << endl;cout <<" ----------------------5------------------" <<  endl;Date d8(1996,10,1);cout<<d8<< endl;;cin >>d8;cout << d8<<endl;cout << "----------------------6------------------"<< endl;return 0;}





0 0
原创粉丝点击