c++实现日期类(class Date) 构造函数 拷贝构造 操作符重载(输入输出 比较操作 算数运算 自增自减)
2016-10-30 00:22
771 查看
注释比较详细,可以直接跑起来,直接上代码(vs2012 win7)
一、头文件
二、实现详情Date.cpp
三、主函数,做了一些基本测试(脸滚键盘)Test.cpp
一、头文件
/************** 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; } // 日期 - 日期 int operator - (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;//2831 cout << d7 - d6 << endl; cout <<" ----------------------5------------------" << endl; Date d8(1996,10,1); cout<<d8<< endl;; cin >>d8; cout << d8<<endl; cout << "----------------------6------------------"<< endl; return 0; }
相关文章推荐
- c++ 实现动态顺序表(拷贝构造 赋值运算符重载等操作)
- 请用c++ 实现stl中的string类,实现构造,拷贝构造,析构,赋值,比较,字符串相加,获取长度及子串等功能。
- C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数)
- 日期类的实现(其中包括构造函数,拷贝构造,赋值运算符重载,输出运算符重载,友元函数等等)
- 号)、sex(性别)、birthday(出生日期)、id(身份证号)等等。其中“出生日期”定义为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函数、拷贝构造函
- C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数)
- 请用c++ 实现stl中的string类,实现构造,拷贝构造,析构,赋值,比较,字符串相加,获取长度及子串等功能
- c++ 实现动态顺序表(拷贝构造 赋值运算符重载等操作)
- c++ 实现动态顺序表(拷贝构造 赋值运算符重载等操作)
- 拷贝构造,深度拷贝,关于delete和default相关的操作,explicit,类赋初值,构造函数和析构函数,成员函数和内联函数,关于内存存储,默认参数,静态函数和普通函数,const函数,友元
- [C++]异常处理中的拷贝构造操作(copy constructor)
- C++ 类的三种构造函数(默认构造,拷贝构造,赋值构造),在什么情况下会被调用
- java比较日期大小及日期与字符串的转换【SimpleDateFormat操作实例】
- 实现 日期 的运算---C++
- 用char*实现的一个完整的类,包含类的基本操作:一般构造、拷贝构造、赋值转换、重载 > >,< <
- 用汇编的眼光看C++(之class构造、析构) ,(之拷贝、赋值函数)
- C++【拷贝构造】和【拷贝赋值】(实现自定义的string类)
- java实现计算两个日期相差多少月、比较两个日期大小 等常用日期操作
- 使用datepicker插件实现日期选择的基本操作 8-5
- c++一个实现分数大小比较和各种运算的类