C++实现简单的日期类
2016-12-24 22:58
330 查看
date.h
/******************************************************************************************* *Author:MaJing *Date:2016_12_24 *Word: if you want,you can do it *******************************************************************************************/ #pragma once #include <iostream> #include <assert.h> using namespace std; class Date { public: Date(int y = 1900, int m = 1, int d = 1); Date(const Date &d); //复制构造函数看成构造函数的重载, 只有参数不同 // 比较运算符重载 bool operator>(const Date& d); //前面的日期在后面的日期之后 敲的不多吧,感觉像在默写 bool operator<(const Date& d); bool operator>=(const Date& d); bool operator<=(const Date& d); bool operator==(const Date& d); //类中被调用函数在调用函数之后可以找到这个函数吗? bool operator!=(const Date& d); //赋值运算符重载 Date &operator=(const Date & d); //返回该类型的引用是为了支持连等 Date & operator++();//前置++ Date operator++(int); //后置++ Date & operator--(); // 前置-- Date operator--(int); //后置-- int operator-(const Date &d);//计算两个日期之间差几天 Date operator-(int day); //计算几天之前的日期 Date operator+(int day); //计算几天之后的日期 Date operator+=(int day); //计算几天之后的日期 int operator-(const Date &d); //计算两个日期之间差几天 Date &operator-=(int day);//计算几天之前的日期 private: //内部函数 bool IsVaild(); //检查传入参数的合法性 bool IsLeapYear(); int GetMonthDay(); // private: int _year; int _month; int _day; };
date.cpp
#include "date.h" Date::Date(int y = 1900, int m = 1, int d = 1) { _year = y; _month = m; _day = d; assert(IsVaild()); } Date::Date(const Date &d) //复制构造函数看成构造函数的重载, 只有参数不同 { _year = d._year; _month = d._month; _day = d._day; } // 比较运算符重载 bool Date::operator>(const Date& d) //前面的日期在后面的日期之后 敲的不多吧,感觉像在默写 { if (_year > d._year) return true; else if (_year < d._year) return false; else { if (_month > d._month) return true; else if (_month < d._month) return false; else return _day > d._day; } } bool Date::operator<(const Date& d) { return !(*this > d || *this == d);//比较的是*this } bool Date::operator>=(const Date& d) { return ((*this > d) || (*this == d)); //调用的时候是date对象 编译器处理成this指针 return 后面到底用不用加() } bool Date::operator<=(const Date& d) { return ((*this < d) || (*this == d)); } bool Date::operator==(const Date& d) //类中被调用函数在调用函数之后可以找到这个函数吗? { return (_year == d._year && _month == d._month && _day == d._day); } bool Date::operator!=(const Date& d) //类中被调用函数在调用函数之后可以找到这个函数吗? { return !(_year == d._year && _month == d._month && _day == d._day); } //赋值运算符重载 Date &Date::operator=(const Date & d) //返回该类型的引用是为了支持连等 { if (this != &d) // 此处为比较地址 { _year = d._year; _month = d._month; _day = d._day; } return *this; } Date & Date::operator++()//前置++ { _day += 1; if (_day > GetMonthDay()) { if (_month == 12) { _month = 1; _year += 1; } else { _month++; } _day = 1; } return *this; } Date Date::operator++(int) //后置++ { Date tmp(*this); //Date 类型 operator++(); return tmp; } Date & Date::operator--() // 前置-- { _day -= 1; if (_day < 1) { --_month; if (_month < 1) { --_year; _month = 12; } _day = GetMonthDay(); } return *this; //为什么要返回 是要支持连加 } Date Date::operator--(int) //后置-- { Date tmp(*this); operator--(); return tmp; } Date Date::operator-(int day) //计算几天之前的日期 { Date tmp(*this); tmp -= day; return tmp; } Date Date::operator+(int day) //计算几天之后的日期 { Date tmp(*this); tmp += day; return tmp; } Date &Date::operator-=(int day) //计算几天之前的日期 { _day -= day; while (_day < 1) { --_month; if (_month < 1) { --_year; _month = 12; } _day += GetMonthDay(); } return *this; } Date Date::operator+=(int day) //计算几天之后的日期 { _day += day; while (_day > GetMonthDay()) { _day -= GetMonthDay(); _month++; if (_month > 12) { _month = 1; _year += 1; } } return *this; } int Date::operator-(const Date &d)//计算两个日期之间差几天 { Date later = (*this) > d ? (*this) : d; Date formar = (*this) < d ? (*this) : d; int count = 0; while (formar != later) { ++formar; ++count; } return count; } //内部函数 bool Date::IsVaild() //检查传入参数的合法性 { if (_year > 0 && _month > 0 && _month <13 && _day > 0 && _day <= GetMonthDay()) return true; else return false; } bool Date::IsLeapYear() { if ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0) return true; else return false; } int Date::GetMonthDay() // { static const int dayTable[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (_month == 2 && IsLeapYear()) { return dayTable[2] + 1; } else return dayTable[_month]; }
test.c
#include "date.h"
int main()
{
Date d1(2016, 12,1);
//d1-=10; //2015 12 27
Date d2(2016, 12, 23);
//cout <<(d1.operator>=(d2)) << endl; // d1.operator>(d2) d1>d2; operator>(d1,d2)由于隐式传参不行
//d2 += 10; // 2017 1 2 想好你要的结果,就能快一些
int distance = d2 - d1; // 22
return 0;
}
相关文章推荐
- C++实现的简单日期类
- 用c++实现的简单的日期类
- 利用C++日期类实现简单的日期计算器
- 利用C++日期类实现简单的日期计算器
- 标准纯C++实现简单的词法分析器(一)
- 标准纯C++实现简单的词法分析器(二)
- C++中简单实现foreach循环
- Ping的简单实现 CODE: C /C++
- 堆栈的简单C++实现
- 策略模式与简单工厂的C++实现
- 用vbs实现的简单的服务器文件备份办法压缩文件名自动按日期命名
- 用C++实现简单的文件I/O操作
- 一个简单的多叉树C++实现
- 标准纯C++实现简单的词法分析器(三)
- 标准纯C++实现简单的词法分析器(三)
- 实例学MFC(3)--超简单C++实现网络下载器
- 用C++实现简单的文件I/O操作
- 用C++实现简单的文件I/O操作(ifstream,ofstream)
- 用C++实现简单的文件I/O操作(ifstream,ofstream)
- 大话设计模式之C++实现---简单工厂模式