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
- c++实现日期类(class Date) 构造函数 拷贝构造 操作符重载(输入输出 比较操作 算数运算 自增自减)
- String 类的拷贝构造函数, 赋值操作符重载, 算数操作符重载
- C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数)
- 日期类的实现(其中包括构造函数,拷贝构造,赋值运算符重载,输出运算符重载,友元函数等等)
- 构造类的大三原则(读自VCKBase),及拷贝构造函数与=运算符的重载
- C++面试题,自己实现String类的构造函数,拷贝构造函数,赋值操作符,append,replace,+运算符重载等
- 实现MyString类--构造函数、拷贝构造函数、析构函数、赋值函数、操作符重载函数
- c++ 实现动态顺序表(拷贝构造 赋值运算符重载等操作)
- c++ 实现动态顺序表(拷贝构造 赋值运算符重载等操作)
- 拷贝构造,操作符重载
- 实现string的默认构造函数,拷贝函数,c型字符串参数的构造函数,赋值构造函数,析构函数,重载输出操作符,测试
- C++:复数类构造函数、拷贝构造、运算符重载、析构函数
- 【C++】复制构造函数(拷贝构造函数)和=运算符重载使用的区别
- [C++]拷贝构造函数和赋值运算符重载
- 拷贝构造函数中是否可以调用重载后的赋值运算操作符
- 拷贝构造函数/重载赋值运算符
- 拷贝构造函数与重载=操作符
- 拷贝构造函数和重载"="赋值操作符
- HTML 13 表格
- poj 2777 Count Color(位运算+线段树区间更新 可用bitset记录)
- Problem 38 Pandigital multiples (set + vector)
- Swift3.0 popToViewController 的使用
- 2016 NCPC Game Rank(模拟)
- c++实现日期类(class Date) 构造函数 拷贝构造 操作符重载(输入输出 比较操作 算数运算 自增自减)
- Leetcode #343 Integer Break
- 优化php效率,提高php性能的一些方法:
- C# socket学习--IPAddress、IPEndPoint、DNS
- Python 包管理工具解惑
- 页面自动适应屏幕的宽度
- caa二次开发的安装
- phpStorm更新后配置svn无法使用
- JAVA SSH ----Struts数据校验