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

在微信怎样搞做微视频网站h5设计平台

在微信怎样搞做微视频网站,h5设计平台,免费网站模板源码,阳泉营销型网站建设费用日期类的实现 一,声明二,函数成员定义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…

日期类的实现

  • 一,声明
  • 二,函数成员定义
    • 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 || _month>12 || _day<1 || _day>GetMonthDay(_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 !(*this>=y);
}
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 (_day<=0){_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.yayakq.cn/news/509130/

相关文章:

  • 重庆技术网站建设做的比较好的车载嗨曲网站
  • 谷歌官方网站登录入口做IT的会做网站吗
  • wordpress如何添加关键词seo实战密码第三版pdf
  • 做百度移动端网站优化河南省建筑工程信息网
  • 我的世界做指令的网站微网站开发报价单
  • 网站建设要学哪些wordpress微信登录申请
  • 黑群晖wordpress建站WordPress主题预览封面
  • 沧州南皮网站建设网站后台更新了 前台不现实
  • 网站开发计划书网站技术解决方案怎么在网上推广产品
  • 网站开发服务 税厦门seo培训
  • 菏泽网站建设兼职wordpress网站排名
  • 个人网站建设法律规定wordpress样式多的编辑器
  • 福建建设工程有限公司网站企业邮箱网易登录入口
  • 机械网站源码 php深圳燃气公司电话号码
  • 霸县网站建设小型网站
  • 互联网网站建设哪家好河南平台网站建设设计
  • asp业务网站做网站建设与推广企业
  • 什么是网站建设方案书造价工程建设协会网站
  • 贵阳网站搜索优化宿州保洁公司哪家好
  • 网站布局怎么用dw做专业网站建设分为8步
  • 建站工具箱 discuz9uu域名更新自动转跳
  • app制作和网站一样吗wordpress怎么给会员加上期限
  • 南通网站推广优化费用ui界面设计思路
  • 制作公司宣传片天津优化网站
  • 网站设计书的结构网站的域名
  • 怎样弄免费网站公司网站建设管理意见
  • 深圳金鼎网站建设想学编程做网站
  • 新类型网站外贸网站建设内容
  • 专业公司网站建设网站设计最新风格
  • 网站大全软件手机ui设计网站