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

一般网站的宽度河源网站制作

一般网站的宽度,河源网站制作,地方网站自助建站,公司网站上传图片日期类的实现 一,声明二,函数成员定义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/621814/

相关文章:

  • 做网站最好要买什么东西在线无限观看次数破解版
  • 做民俗酒店到哪些网站推荐专业做旅游网站
  • 珠海市建设工程造价协会网站响应式设计的网站
  • 网站做服务端浙江坤宇建设有限公司 网站
  • 手机网站建设设计深圳的企业排名
  • 360网站认证域名跳转短链接生成
  • 生产企业网站欣赏海外模板网站有哪些
  • 住建部网站村镇建设管理平台策划方案范文
  • 上海专业网站建设公司有哪些学游戏设计去哪个学校
  • 网站建设的经验总结微商城系统销售
  • 怎么做跳转网站首页房屋设计图制作软件
  • 网站设计软件培训怎么样网页设计培训高清图集
  • 轮播网站大男人直播视频
  • 广州网站建设系统开发wordpress 个人简历
  • 广西住房和城乡建设厅网站首页wordpress仿互站
  • 阿里云服务器责任怎么做网站最好国内免费网站空间
  • 牛商网做的网站有哪些营销成功的案例
  • 共享备案网站海葵音乐制作公司
  • 网站建设搭建公司网站的推广和宣传方式
  • 网站调用优酷视频去广告php做网站需要的软件
  • 北京建站程序凯里市企业建站公司
  • 中国建设银行预约网站新乐网站建设
  • 学习网站建设的步骤网站开发的分工和流程
  • 北京网站建设学习网站建设的合同 体会
  • 南京网站设南京网站设计计wordpress网站打包app
  • 网站做视频链接网站运营与管理期末考试
  • 怎样用云服务器做网站福建seo网络
  • 石家庄品牌网站建设设计师个人网页设计
  • 消费者联盟网站怎么做手机微网站
  • 分享类网站源码在哪里学广告设计培训