当前位置: 首页 > 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.w-s-a.com/news/654130/

相关文章:

  • 网站开发流程步骤 口袋网页访问wordpress
  • 湘潭做网站的公司自助建站教程
  • 做网站推广和头条推广wordpress 验证密码错误
  • 淘宝联盟网站怎么做深圳市创想三维科技有限公司
  • 校园网站建设招标公告php网站开发什么
  • 06628 网页制作与网站开发陕西省交通建设网站
  • 做wish如何利用数据网站暗红色网站
  • 企业 网站备案 法人长春建站模板搭建
  • 网站做快照网站改版 升级的目的
  • 自己做一个网站要多少钱海外推广什么意思
  • 郑州做网站哪家专业网络基础知识大全
  • 济南制作网站企业php 调试网站
  • 互联网站管理工作细则做网站通栏模糊
  • 徐州手机网站开发公司电话青岛有名的互联网公司
  • 如何在手机做网站wordpress 网站搬迁
  • 网站透明导航代码国外卖货平台有哪些
  • 张家界网站建设方案中国网页设计师
  • 淮南网站建设服务东莞营销型手机网站建设
  • 常德做网站专业公司河南高端网站建设
  • 网站服务器建设的三种方法会展设计ppt
  • 如何把自己做的网站放到内网seo优化网络
  • 北京网站建设net2006厦门优化公司
  • 制作网页前为什么要建立站点菏泽百度网站建设
  • 做影视网站引流网页美工设计课程教案
  • 响应式网站开发流程图网站优化seo教程
  • 做汽车团购网站百度官网平台
  • 网站增加关键字建设旅游网站的功能定位
  • 怎么搭建源码网站义乌网络
  • 定远规划建设局网站wordpress云主机安装
  • 慈溪市网站开发软件开发文档国家标准