类和对象—日期类
来源:互联网 发布:懒人做饭知乎 编辑:程序博客网 时间:2024/05/21 01:57
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;}
本文出自 “无心的执着” 博客,转载请与作者联系!
0 0
- 类和对象—日期类
- 日期和时间对象
- Python 日期对象和日期字符串
- 日期和时间类
- C++类与对象——日期类
- 第一个面向对象程序——日期类
- 字符串和日期类:日期类
- 定义一个日期类Date,该类对象存放一个日期
- 类与对象3(日期类)
- JavaScript 常用对象[日期和时间对象]
- JavaScript语法入门系列(七) 类和对象(日期Date)
- JAVASE--JAR包、基本数据类型对象包装类、日期和时间
- 常见对象_日期工具类的编写和测试案例
- System 类和日期类
- 包装类和日期操作
- 字符串和日期:字符串类
- 日期和星期工具类
- Java常用类:Data()的用法,获得当前日期和时间对象date 定义对象Date date1 = new Date(36000000);获得包含的日期和时间 比较date和date1是否相等
- 判断数字1在计算机中的存储方式
- 连接函数strcat与strncat
- 单链表
- 数据结构——栈与队列
- 面试题——查找单链表的中间节点
- 类和对象—日期类
- DFS JAVA 模板
- 类和对象—复数类
- 面试题—链表操作
- C语言编写—通讯录(小项目)
- 浅谈为什么Java接口中不允许定义变量------转帖
- android混淆后webview显示问题
- 面试题—链表操作2
- 顺序表(C++实现)