用c++实现日期类函数的重载
来源:互联网 发布:好帮手软件简介 编辑:程序博客网 时间:2024/06/08 18:39
- #include<iostream>
- #include<windows.h>
- using namespace std;
- class Date
- {
- public:
- Date(int year, int month, int day) //构造函数
- :_year(year)
- ,_month(month)
- ,_day(day)
- {}
- Date(Date & d) //拷贝构造
- :_year(d._year)
- , _month(d._month)
- , _day(d._day)
- {}
- Date & operator = (const Date &d) //赋值运算符的重载
- {//将实例d的所有成员变量值全部赋值给this,这里不存在指针问题,就不用考虑内存问题
- _year = d._year;
- _month = d._month;
- _day = d._day;
- return *this;
- }
- bool operator == (const Date& d)//重载 ==
- {
- return this->_year == d._year
- && this->_month == d._month
- && this->_day == d._day;
- }
- bool operator <(const Date& d) //重载 <
- {
- if (_year < d._year)//判断年
- {
- return true;
- }
- else if (_year == d._year)//年相等,就判断月
- {
- if (_month<d._month)
- {
- return true;
- }
- else if (_month == d._month)//月相等,判断天
- {
- if (_day<d._day)
- {
- return true;
- }
- }
- }
- return false;
- }
- bool operator <=(const Date& d)//重载<=(复用函数<和==)
- {
- return (*this<d) || (*this == d);//当<或==满足一个的时候为真
- }
- bool operator >(const Date& d)//重载<=(复用函数<=)
- {
- return !(*this <= d);
- }
- bool operator >=(const Date& d)
- {
- return !(*this < d);
- }
- Date operator+ (int day)//重载+
- {
- int ret = GetMonthDay(_year, _month);//调用函数获取当年当月天数
- Date tmp = *this;//用this创建一个临时对象
- while ((_day + day) > ret)//循环条件:当总天数大于一个月的天数时
- {
- if ((tmp._month + 1) > 12)//当月份大于12时
- {
- tmp._year++;//年份加一
- tmp._month = 0;//月份置零
- }
- else//否则,月份加一
- {
- tmp._month++;
- }
- day -= ret;//总天数减去当月的天数
- ret = GetMonthDay(_year, tmp._month);//获取下一月的天数
- }
- tmp._day += day;
- return tmp;
- }
- Date& operator+= (int day)//重载+=(复用+)
- {
- *this = *this + day;
- return *this;
- }
- Date operator- (int day)//重载-
- {
- int ret = GetMonthDay(_year, _month);
- Date tmp = *this;
- while (day > tmp._day)//当需要减的天>当月的天数时进行循环
- {
- while (day > ret)
- {
- if (tmp._month > 1)
- {
- tmp._month--;
- }
- else
- {
- tmp._year--;
- tmp._month = 11;
- }
- day -= ret;
- ret = GetMonthDay(tmp._year, tmp._month);
- }
- day -= ret;
- }
- tmp._day -= day;
- return tmp;
- }
- Date& operator-= (int day)//重载-=(复用-)
- {
- *this = *this - day;
- return *this;
- }
- Date operator++()
- {
- return *this += 1;
- }
- Date operator++(int)
- {
- return *this + 1;
- }
- Date operator--()
- {
- return *this -= 1;
- }
- Date operator--(int)
- {
- return *this - 1;
- }
- int operator-(const Date& d)//日期间日期
- {
- Date tmp = *this;
- int day = 0;
- int ret = 0;
- if (tmp > d)
- {
- while (tmp._year > d._year)
- {
- if (tmp._month > 2)
- {
- if (IsLeapYear(tmp._year))
- {
- day += 366;
- tmp._year--;
- }
- }
- else
- {
- day += 365;
- tmp._year--;
- }
- }
- while (tmp._month > d._month)
- {
- ret = GetMonthDay(tmp._year, tmp._month);
- day += ret;
- }
- if (tmp._day > d._day)
- {
- day += tmp._day - d._day;
- }
- else
- {
- day -= d._day - tmp._day;
- }
- }
- return day;
- }
- void Display()
- {
- cout << _year << "\t" << _month << "\t" << _day << endl;
- }
- protected:
- bool IsLeapYear(int year)//判断是否为闰年
- {
- if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
- {
- return true;
- }
- return false;
- }
- int GetMonthDay(int year, int month)//获得当年当月天数
- {
- int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- int day = monthArray[month];
- if (month == 2 && IsLeapYear(year))
- {
- day += 1;
- }
- return day;
- }
- private:
- int _year;
- int _month;
- int _day;
- };
- #include<iostream>
- #include<windows.h>
- using namespace std;
- class Date
- {
- public:
- Date(int year, int month, int day) //构造函数
- :_year(year)
- ,_month(month)
- ,_day(day)
- {}
- Date(Date & d) //拷贝构造
- :_year(d._year)
- , _month(d._month)
- , _day(d._day)
- {}
- Date & operator = (const Date &d) //赋值运算符的重载
- {//将实例d的所有成员变量值全部赋值给this,这里不存在指针问题,就不用考虑内存问题
- _year = d._year;
- _month = d._month;
- _day = d._day;
- return *this;
- }
- bool operator == (const Date& d)//重载 ==
- {
- return this->_year == d._year
- && this->_month == d._month
- && this->_day == d._day;
- }
- bool operator <(const Date& d) //重载 <
- {
- if (_year < d._year)//判断年
- {
- return true;
- }
- else if (_year == d._year)//年相等,就判断月
- {
- if (_month<d._month)
- {
- return true;
- }
- else if (_month == d._month)//月相等,判断天
- {
- if (_day<d._day)
- {
- return true;
- }
- }
- }
- return false;
- }
- bool operator <=(const Date& d)//重载<=(复用函数<和==)
- {
- return (*this<d) || (*this == d);//当<或==满足一个的时候为真
- }
- bool operator >(const Date& d)//重载<=(复用函数<=)
- {
- return !(*this <= d);
- }
- bool operator >=(const Date& d)
- {
- return !(*this < d);
- }
- Date operator+ (int day)//重载+
- {
- int ret = GetMonthDay(_year, _month);//调用函数获取当年当月天数
- Date tmp = *this;//用this创建一个临时对象
- while ((_day + day) > ret)//循环条件:当总天数大于一个月的天数时
- {
- if ((tmp._month + 1) > 12)//当月份大于12时
- {
- tmp._year++;//年份加一
- tmp._month = 0;//月份置零
- }
- else//否则,月份加一
- {
- tmp._month++;
- }
- day -= ret;//总天数减去当月的天数
- ret = GetMonthDay(_year, tmp._month);//获取下一月的天数
- }
- tmp._day += day;
- return tmp;
- }
- Date& operator+= (int day)//重载+=(复用+)
- {
- *this = *this + day;
- return *this;
- }
- Date operator- (int day)//重载-
- {
- int ret = GetMonthDay(_year, _month);
- Date tmp = *this;
- while (day > tmp._day)//当需要减的天>当月的天数时进行循环
- {
- while (day > ret)
- {
- if (tmp._month > 1)
- {
- tmp._month--;
- }
- else
- {
- tmp._year--;
- tmp._month = 11;
- }
- day -= ret;
- ret = GetMonthDay(tmp._year, tmp._month);
- }
- day -= ret;
- }
- tmp._day -= day;
- return tmp;
- }
- Date& operator-= (int day)//重载-=(复用-)
- {
- *this = *this - day;
- return *this;
- }
- Date operator++()
- {
- return *this += 1;
- }
- Date operator++(int)
- {
- return *this + 1;
- }
- Date operator--()
- {
- return *this -= 1;
- }
- Date operator--(int)
- {
- return *this - 1;
- }
- int operator-(const Date& d)//日期间日期
- {
- Date tmp = *this;
- int day = 0;
- int ret = 0;
- if (tmp > d)
- {
- while (tmp._year > d._year)
- {
- if (tmp._month > 2)
- {
- if (IsLeapYear(tmp._year))
- {
- day += 366;
- tmp._year--;
- }
- }
- else
- {
- day += 365;
- tmp._year--;
- }
- }
- while (tmp._month > d._month)
- {
- ret = GetMonthDay(tmp._year, tmp._month);
- day += ret;
- }
- if (tmp._day > d._day)
- {
- day += tmp._day - d._day;
- }
- else
- {
- day -= d._day - tmp._day;
- }
- }
- return day;
- }
- void Display()
- {
- cout << _year << "\t" << _month << "\t" << _day << endl;
- }
- protected:
- bool IsLeapYear(int year)//判断是否为闰年
- {
- if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
- {
- return true;
- }
- return false;
- }
- int GetMonthDay(int year, int month)//获得当年当月天数
- {
- int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- int day = monthArray[month];
- if (month == 2 && IsLeapYear(year))
- {
- day += 1;
- }
- return day;
- }
- private:
- int _year;
- int _month;
- int _day;
- };
阅读全文
1 0
- 用c++实现日期类函数的重载
- c函数重载的实现
- 如何实现 C 的函数重载
- C语言是实现函数的重载
- C函数的模拟重载实现说明
- 用C语言实现 函数重载
- 日期减少一天的C函数实现
- 用C/C++实现一个日期类,重载运算符=,==,+,-,++,--,>,>=,<,<=等
- 【C++】函数的重载
- 【c++】函数的重载
- 【c++】实现一个函数重载
- C语言实现函数重载
- 日期类的实现(其中包括构造函数,拷贝构造,赋值运算符重载,输出运算符重载,友元函数等等)
- C++:日期类的实现
- 【c++】日期类的实现
- c++日期类的实现级运算符的重载
- C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数)
- 【C++】编写一个简单的函数实现重载。
- poj3580:SuperMemo(块状链表/Splay)
- 号码加密
- synchronized关键字修饰代码块
- Mondriaan's Dream POJ
- 什么叫做第三方支付?
- 用c++实现日期类函数的重载
- ContentObserver内容监听者(内容观察者)
- 模块
- 关于css的选择器
- Android ContentProvider
- 服务状态的工具类
- Tomcat系列—JDK安装(tar.gz)
- JS 中关于document和window对象的一些值: scrollHeight,scrollLeft,scrollTop,scrollWidth等。。
- dubbox编译