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

佛山专业的做网站的哈尔滨建设工程网

佛山专业的做网站的,哈尔滨建设工程网,问答社区网站建设,外包网站平台日期类的实现 一#xff0c;声明二#xff0c;函数成员定义2.1构造函数2.2获取月份天数2.3比较运算符2.3.1等于和大于2.3.2其他 2.4计算运算符2.4.1 2.4.2-- 2.5日期-日期 一#xff0c;声明 class Date { public:Date(int year 1, int month 1, int… 日期类的实现 一声明二函数成员定义2.1构造函数2.2获取月份天数2.3比较运算符2.3.1等于和大于2.3.2其他 2.4计算运算符2.4.1 2.4.2-- 2.5日期-日期 一声明 class Date { public:Date(int year 1, int month 1, int day 1);//打印void Print();//获取月份天数int GetMonthDay(int year, int month);//比较运算符bool operator(const Date y);bool operator!(const Date y);bool operator(const Date y);bool operator(const Date y);bool operator(const Date y);bool operator(const Date y);//计算运算符int operator-(const Date d);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); private:int _year;int _month;int _day; };二函数成员定义 2.1构造函数 Date::Date(int year,int month,int day) {_year year;_month month;_day day;if (_year 1 || _month 1 || _month12 || _day1 || _dayGetMonthDay(_year, _month)){Print();cout 日期非法 endl;} }这里要注意构造函数的声明定义分离给缺省值的时候只在声明的地方给不然会出错。 2.2获取月份天数 //获取月份天数 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 % 4 0 year % 100 ! 0) || (year % 400 0)))return 29;return monthArray[month]; }2.3比较运算符 2.3.1等于和大于 bool Date::operator(const Date y) {return _year y._year _month y._month _day y._day; }bool Date::operator(const Date y) {if (_year y._year){return true;}else if (_year y._year){if (_month y._month){return true;}else if (_month y._month){if (_day y._day){return true;}}}return false; }写完了这两个那么其他的运算符我们都可以复用来简化代码。 2.3.2其他 bool Date::operator!(const Date y) {return !(*this y); } bool Date::operator(const Date y) {return (*this y || *this y); } bool Date::operator(const Date y) {return !(*thisy); } bool Date::operator(const Date y) {return!(*this y); }2.4计算运算符 2.4.1 Date Date::operator(int day) {if (day 0){return *this - (-day);}_day day;while (_day GetMonthDay(_year, _month)){_day - GetMonthDay(_year, _month);_month;if (_month 13){_month 1;_year;}}return *this; }可以复用 Date Date::operator(int day) {Date tmp(*this);tmp day;return tmp; }补充这里除了去复用可以反过来吗 这里我们就要从效率的角度去看待这个问题。 我们分别对比他们的拷贝构造可以看出用去复用资源更浪费。 2.4.2-- Date Date::operator-(int day) {if (day 0){return *this (-day);}_day - day;while (_day0){_month--;if (_month 1){_month 12;_year--;}_day GetMonthDay(_year, _month);}return *this; }一样的复用 Date Date::operator-(int day) {Date tmp(*this);tmp - day;return tmp; }2.4.3前置后置前置–后置– 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; }为例区分前置和后置我们会在后置的参数部分加一个参数类型。 2.5日期-日期 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;while (Max ! Min){Min;n;}return n * flag; }找出两个天数中大的那个然后让小的天数一直直到相等。
http://www.hkea.cn/news/14492553/

相关文章:

  • 网站防止盗图邯郸网络企业版
  • 昆明品牌网站建设广州网站推广平台
  • 湘潭做网站推荐磐石网络旅游网站排名榜
  • 中文的网站做不成二维码html中文网站模板下载
  • 电脑做网站用word衡水网站建设制作
  • 建设银行东航龙卡登录东航网站网络营销工具及其特点
  • 那些网站可以做淘宝店铺推广高端网站建设如何收费
  • 个人网站的首页中山哪里有好网站建设公司
  • 宁波网站建设设计报告网易企业邮箱域名是什么
  • 百度云虚拟主机做网站wordpress能承载多少数据库
  • 狮山网站建设app开发网站建设哪家好
  • 十大搜索引擎网站郑州便宜网站建设费用
  • 中国建设工程安全管理协会网站免费文档模板素材网站
  • 创口贴网站模板网站建设捌金手指花总十三
  • 网站备案 网站建设方案书提高网页加载速度的方式
  • 简单响应式网站设计代码如何进行营销型企业网站的优化
  • 深圳网站建设最好网页制作公司南昌
  • 建设创意网站seo推广培训班
  • cms建站系统免费wordpress 去除底部
  • 建立个人网站服务器襄阳网站seo
  • 一个网站大概多少页面网络服务提供者收集和使用个人信息应当符合的条件有
  • 梅州网站建设公司视频教学互动网站建设
  • 桥东网站建设建设银行网站背景
  • 怎么做网站自动响应外包公司与劳务派遣区别
  • 受欢迎的网站建设教程房地网站制作
  • 网站如何做微信支付宝支付宝支付宝goule wordpress
  • 警告欺骗网站模板nodejs做后端的网站
  • 网站推广方式主要通过怎么将自己的视频推广出去
  • 网站后台传不上图片腾讯云网站搭建流程
  • 简单建网站公司注册代理机构