类和对象—日期类
2016-01-21 14:49
387 查看
C++是基于面向过程与面向对象的语言,类和对象是其中比较重要的部分,也是较为难理解的部分,这一部分需要大量的代码练习。下面为类和对象—日期类。主要功能相当于一个日期计算器,可以实现日期的推后、提前、日期差等功能。
#define _CRT_SECURE_NO_WARNINGS 1 //日期类 #include <iostream> #include <stdlib.h> using namespace std; class Date { public: Date(int year, int month, int day) //构造函数 { _year = year; _month = month; _day = day; } Date(const Date & d) //拷贝构造函数 { _year = d._year; _month = d._month; _day = d._day; } void Display() //打印 { cout << _year << "-" << _month << "-" << _day << endl; } ~Date() //析构函数 { } bool operator==(const Date & d) //相等 { return (_year == d._year)&& (_month == d._month)&& (_day == d._day); } bool operator<(const Date & d) //小于 { return (_year < d._year)|| (_year == d._year && _month < d._month) || (_year == d._year && _month == d._month && _day < d._day); } bool operator<=(const Date & d) //小于等于 { return *this < d || *this == d; } int _GetMonthDay(int year, int month) //判断每月的天数 { int day; if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) { if (month == 2) { day = 29; } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { day = 31; } else { day = 30; } } else { if (month == 2) { day = 28; } if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { day = 31; } else { day = 30; } } return day; } Date operator+(int day) //推后day { if (day < 0) { return *this - (-day); } Date tmp(*this); tmp._day = tmp._day + day; while (tmp._day > _GetMonthDay(tmp._year, tmp._month)) { tmp._day -= _GetMonthDay(tmp._year, tmp._month); if (tmp._month == 12) { tmp._year++; tmp._month = 1; } else { tmp._month++; } } return tmp; } Date & operator+=(int day) //推后多少天 { *this = *this + day; return *this; } Date & operator++() //加1天,返回增加后的日期 { *this += 1; return *this; } Date operator++(int) //加一天,返回增加前的日期 { Date tmp(*this); *this += 1; return tmp; } Date operator-(int day) //提前day天 { if (day < 0) { return *this + (-day); } Date tmp(*this); tmp._day -= day; while (tmp._day <= 0) { if (tmp._month == 1) { tmp._month = 12; tmp._year--; } else { tmp._month--; } tmp._day -= _GetMonthDay(tmp._year, tmp._month); } return tmp; } Date & operator-=(int day) //减等于 { *this = *this - day; return *this; } Date & operator--() //减减、后置 { *this -= 1; return *this; } Date operator--(int) //减减、前置 { Date tmp(*this); *this -= 1; return tmp; } bool operator!=(const Date & d) //不等于 { return (_year != d._year)|| (_year == d._year && _month != d._month) || (_year == d._year && _month == d._month && _day != d._day); } int operator-(const Date & d) //两个日期的差 { int flag = 1; Date max = *this; Date min = d; if (max < min) { swap(max._year, min._year); swap(max._month, min._month); swap(max._day, min._day); flag = -1; } int day = 0; while (max != min) { ++min; ++day; } return day*flag; } private: int _year; int _month; int _day; }; //测试用例 void Test1() { Date d1(2015, 1, 1); d1 += 35; d1.Display(); d1 += 10; d1.Display(); } void Test2() { Date d2(2016, 1, 15); d2 -= 30; d2.Display(); d2 -= 10; d2.Display(); } void Test3() { Date d3(2016, 2, 9); d3--; d3.Display(); d3++; d3.Display(); } //主函数 int main() { //Test1(); Test2(); //Test3(); system("pause"); return 0; }
相关文章推荐
- oracle sql日期比较
- DB2 日期和时间的函数应用说明
- Delphi实现读取系统时间与日期完整实例
- Country使 MS-DOS 子系统能使用国际时间、日期、货币、大小写转换和小数分隔符
- vbs下通过日期查找文件夹的代码
- 用批处理实现的创建带日期的文件夹的代码
- asp格式化日期时间格式的代码
- sql server动态存储过程按日期保存数据示例
- SqlServer中的日期与时间函数
- Oracle 日期的一些简单使用
- C#验证给定字符串形式日期是否合法的方法
- ASP 日期的加减运算实现代码
- PHP计算指定日期所在周的开始和结束日期的方法
- php计算到指定日期还有多少天的方法
- php准确计算复活节日期的方法
- C#基于基姆拉尔森算法计算指定日期是星期几的方法
- Ext JS框架中日期函数的用法及日期选择控件的实现
- asp取日期1月25日为0125的最简单方法
- JavaScript中日期的相关操作方法总结
- 一个写得较好的JavaScript日期挑选控件