当前位置: 首页 > news >正文

宜宾建设局网站集团网站品牌建设特点

宜宾建设局网站,集团网站品牌建设特点,wordpress app上传,产品营销方案策划书在上篇博客中,我们已经对于日期类有了较为全面的实现,但是,还有一个问题,比如说,我给一个const修饰的日期类的对象 这个对象是不能调用我们上篇博客写的函数的,因为&d1是const Date*类型的&#xff…

在上篇博客中,我们已经对于日期类有了较为全面的实现,但是,还有一个问题,比如说,我给一个const修饰的日期类的对象
在这里插入图片描述
在这里插入图片描述

这个对象是不能调用我们上篇博客写的函数的,因为&d1是const Date*类型的,而this指针是Date*类型,&d1传给this是一种权限的放大,这是不行的,所以,我们要改造一下相关函数,就是声明和定义都要加上const,那么具体形式如下
在这里插入图片描述
在这里插入图片描述
不只是这一个函数,像比较大小,加减天数等,凡是不改变this指针指向的内容的值的,都要加const,那么<<这个符号加const吗?不用,因为这不是成员函数,没有this指针
关于日期类的所有代码我会放在这篇文章的最后,下面我们来说最后两个类的默认成员函数,就是取地址操作符重载和const修饰的取地址操作符重载
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这个默认成员函数确实没什么实际的作用,就算我不写这个函数,直接取地址也不会有任何问题,唯一的作用就是你可以选择不返回this,而返回空或一个假地址
Date.h文件

#include<iostream>
#include<assert.h>
using namespace std;
class Date {
public:Date(int year = 1, int month = 1, int day = 1);Date* operator&();const Date* operator&()const;void Print()const;int GetMonthDay(int year, int month);bool operator==(const Date& n);bool operator!=(const Date& n);bool operator<(const Date& n);bool operator<=(const Date& n);bool operator>(const Date& n);bool operator>=(const Date& n);Date& operator+=(int day);Date operator+(int day);Date& operator-=(int day);Date operator-(int day);Date& operator++();//前置++Date operator++(int);//后置++Date& operator--();Date operator--(int);int operator-(const Date& d);friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in,  Date& d);private:int _year;int _month;int _day;
};ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in,  Date& d);

Date.cpp文件

#include"Date.h"
Date::Date(int year, int month , int day ) {_year = year;_month = month;_day = day;if (_year < 1 || _month < 1 || _month>12 || _day<1 || _day>GetMonthDay(_year, _month)) {cout << _year << "年" << _month << "月" << _day << "日";cout << "日期非法" << endl;}
}
Date* Date:: operator&() {return this;
}const Date* Date::operator&()const {return this;
}void Date::Print() const{cout << _year << "年" << _month << "月" << _day << "日" << endl;
}int Date :: GetMonthDay(int year, int month) {assert(year >= 1 && month >= 1 && month <= 12);int monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if ((month == 2) && (((year % 400) == 0) || ((year % 4) == 0 && (year % 100) != 0))) {return 29;}return monthArray[month];
}bool Date:: operator==(const Date& n) {return _year == n._year && _month == n._month && _day == n._day;
}bool Date::operator!=(const Date& n) {return !(*this == n);
}bool Date:: operator<(const Date& n) {if (_year < n._year) {return true;}if (_year == n._year && _month < n._month) {return true;}if (_year == n._year && _month == n._month && _day < n._day) {return true;}return false;
}bool Date::operator<=(const Date& n) {return ((*this < n) || (*this == n));
}bool Date::operator>(const Date& n) {return !(*this <= n);
}bool Date:: operator>=(const Date& n) {//return *this > n || *this == n;return !(*this < n);
}Date& Date:: operator+=(int day) {if (day < 0) {return *this -= (-day);}if (day < 0) {return *this -= (-day);}_day += day;while (_day > GetMonthDay(_year, _month)) {_day -= GetMonthDay(_year, _month);++_month;if (_month == 13) {++_year;_month = 1;}}return *this;
}Date Date:: operator+(int day) {Date tmp(*this);tmp += day;return tmp;
}Date& Date:: operator-=(int day) {if (day < 0) {return *this -= (-day);}if (day < 0) {return *this += (-day);}_day -= day;while (_day <= 0) {--_month;if (_month == 0) {--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}Date Date:: operator-(int day) {Date tmp(*this);tmp -= day;return tmp;
}Date& Date::operator++() {*this += 1;return *this;
}Date Date:: operator++(int) {Date tmp(*this);*this += 1;return tmp;
}Date& Date::operator--() {*this -= 1;return *this;
}Date Date:: operator--(int) {Date tmp(*this);*this -= 1;return tmp;
}//int Date::operator-(const Date& d) {
//	int flag = -1;
//	Date min = *this;
//	Date max = d;
//	if (*this > d) {
//		min = d;
//		max = *this;
//		flag = 1;
//	}
//	int n = 0;
//	while (min < max) {
//		++min;
//		++n;
//	}
//	return n*flag;
//}
int Date::operator-(const Date& d) {int flag = 1;Date max = *this;Date min = d;if (*this < d) {max = d;min = *this;flag = -1;}int n = 0;int y = min._year;while (y != max._year) {if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {n += 366;}else {n += 365;}y++;}int m1 = 1;int m2 = 1;while (m1 < max._month) {n += GetMonthDay(max._year, m1);m1++;}while (m2 < min._month) {n -= GetMonthDay(min._year, m2);m2++;}n = n + max._day - min._day;return n;
}
ostream& operator<<(ostream& out, const Date& d) {out << d._year << "-" << d._month << "-" << d._day << endl;return out;
}istream& operator>>(istream& in,  Date& d) {in >> d._year>>d._month >> d._day;return in;
}
http://www.yayakq.cn/news/840911/

相关文章:

  • 郑州seo服务公司抖音seo软件
  • 个人做财经类网站做视频网站什么平台好
  • 备案变更网站做的最好的本地生活网站
  • 二次元动漫网站设计方案苏州注册公司网上核名
  • 用钩针做花网站微信商水县住房城乡建设网站
  • app小程序网站开发企业网站项目流程
  • 荆州哪个公司做网站wordpress 加载图片
  • 企业手机网站源码东莞微信小程序开发公司报价
  • 朝阳市网站建设wordpress前台注册登录弹窗代码
  • 建设网站需要买什么网站设计交流
  • 食品商务网-网站建设龙岩做网站的地方
  • 电子商务网站建设需要学什么怎样开个人网站
  • 佛山专业的免费网站优化汽车零件销售网站开发
  • 重庆网站建设安全淘宝数据分析工具
  • 懒人图库网站源码商标注册网电子证书
  • 属于c2c的网站有哪些做钢材销售客户哪里去开发网站
  • 北京网站排名优化软件建一个产品介绍网站
  • 男女做某事网站网站建设费能抵扣吗
  • 南昌做网站哪家好佛山市锵美装饰有限公司网站建设案例
  • wordpress显示网站运行建筑公司网站常用长尾词
  • 京东这样的网站是怎么做的广东深圳区号
  • 不利于优化网站的因素wordpress 自定义导航
  • 网站相似度检测 站长深圳网站制作费用多少
  • 张槎网站设计网站建设博采
  • 太原网站建设网站网站宽度1200px
  • 番禺网站建设培训班wordpress 强制登录
  • 用微信怎么做商城网站吗企业资质查询
  • 以前做视频的网站吗成都的网站建设
  • 智信建设职业培训学校网站电商的网站
  • 建设网站证书查询网站建设推广工作描述