博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实现Data类
阅读量:2242 次
发布时间:2019-05-09

本文共 3124 字,大约阅读时间需要 10 分钟。

实现Data类

运算符重载 + - = > <

主要功能

实现日期加日期 
实现日期间日起 
判断N天后是几月几号 
判断N天前是几月几号 
实现复制运算符重载

代码实现

class Date {
public: Date(int year = 1900, int month = 1, int day = 0) /*构造函数*/ :_year(year) ,_month(month) ,_day(day) {
assert(JudgeDay()); cout<<"Date()"<
_year >= date._year) {
int addYear = this->_year - date._year; if (this->_month >= date._month) {
Date newDate(date); int sum = 0; while (newDate._month != this->_month) {
sum += newDate.GetDay(newDate._year, newDate._month); newDate._month++; } return ( sum += addYear * 365 + this->_day - date._day ); } else if (this->_month < date._month) {
Date newDate(*this); int sum = 0; while (newDate._month != date._month) {
sum += newDate.GetDay(newDate._year, newDate._month); newDate._month++; } return ( sum = addYear*365 - sum + this->_day - date._day ); } } } bool operator<(const Date& date)/*
<运算符重载* { if (this->
_year < date._year || (this->_year == date._year && this->_month < date._month) || (this->_month == date._month && this->_day < date._day) ) {
return true; } return false; } bool operator <=(const Date& date)/*'<'运算符重载*/ {
if ( *this < date || (this->_year == date._year && this->_month == date._month&& this->_day == date._day) ) {
return true; } return false; } bool operator>(const Date& date)/*'>'运算符重载*/ {
if ( this->_year > date._year|| (this->_year == date._year && this->_month > date._month) || (this->_month == date._month && this->_day > date._day) ) {
return true; } return false; } bool operator >=(const Date& date) /*'>='运算符重载*/ {
if (*this > date || (this->_year == date._year && this->_month == date._month && this->_day == date._day) ) {
return true; } return false; } bool operator==(const Date& date) {
if (this->_year == date._year && this->_month == date._month && this->_day == date._day) {
return true; } return false; } bool operator!=(const Date& date) {
if ( this->_year != date._year || (this->_year == date._year && this->_month != date._month )|| (this->_month == date._month && this->_day != date._day) ) {
return true; } return false; } ~Date() {
cout<<" ~Date()"<

若有疑问和建议请发邮件到blbagony@163.com欢迎指出问题

转载地址:http://gvgbb.baihongyu.com/

你可能感兴趣的文章
SpringMVC学习笔记2
查看>>
Oracle知识点连载(一)
查看>>
Oracle知识点连载(二)
查看>>
Oracle知识点连载(三)
查看>>
Oracle知识点连载(五)
查看>>
关于三元运算符的类型转换问题
查看>>
笔记本怎么设置WIfi热点
查看>>
如何实现字符串的反转及替换?
查看>>
Java面试题全集(上)
查看>>
Java面试题全集(中)
查看>>
值传递和引用传递
查看>>
什么情况下用+运算符进行字符串连接比调用StringBuilder对象的append方法连接字符串性能更好?
查看>>
怎么根据Comparable方法中的compareTo方法的返回值的正负 判断升序 还是 降序?
查看>>
理解事务的4种隔离级别
查看>>
常用正则匹配符号
查看>>
建议42: 让工具类不可实例化
查看>>
Java 异步机制与同步机制的区别
查看>>
hibernate的对象三种状态说明
查看>>
什么是N+1查询?
查看>>
Spring 接管 Hibernate 配置 延迟加载
查看>>