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

珠海网站建设创意宁波超值关键词优化

珠海网站建设创意,宁波超值关键词优化,厦门网站建设慕枫,wordpress 内页打不开学习目标: 加深对四个默认构造函数的理解: 1.构造函数 2.析构函数 3.拷贝构造 4.运算符重载 实现功能 1.比较日期的大小 2.日期-天数 3.前/后置,-- 这里基本会使用运算符重载 定义一个日期类 class Date { public://1.全缺省参数的构造函数Da…

学习目标:

加深对四个默认构造函数的理解:

1.构造函数

2.析构函数

3.拷贝构造

4.运算符重载

实现功能

1.比较日期的大小

2.日期+-天数

3.前/后置++,--

这里基本会使用运算符重载

定义一个日期类

class Date
{
public://1.全缺省参数的构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_day = day;_month = month;}//2.构造拷贝函数Date(const Date& x){_year = x._year;_month = x._month;_day = x._day;}//3.析构函数(这里可不构造)~Date() {}//4.运算符重载bool operator==(const Date& x);private:int _year;int _month;int _day;
};

1.比较日期的大小

比较日期大小,只用写大于,等于或者小于,等于,其它功能去复用就行(关系互斥)

--功能声明:

--等于实现:

//等于(d1 == d2;)
bool Date::operator==(const Date& x) 
{return this->_year == x._year&& this->_month == x._month&& this->_day == x._day;
}

--大于实现:

//大于(d1>d2)
bool Date::operator>(const Date& x) 
{if     ((_year > x._year)|| ((_year == x._year) && (_month > x._month))|| ((_year == x._year) && (_month == x._month) && (_day > x._day)))return true;elsereturn false;
}

--小于等于

//小于等于(d1<=d2)
bool Date::operator<=(const Date& x) 
{return !(*this > x);
}

--大于等于

//大于等于
bool Date::operator>=(const Date& x) 
{return (*this > x) || (*this == x);
}

--小于:

//小于(d1<d2)
bool Date::operator<(const Date& x) 
{return !(*this >= x);
}

--不等于:

//不等于
bool Date::operator!=(const Date& x) 
{return !(*this == x);
}

效果:

2.日期+-天数

准备工作:日期+-天数,要考虑到进位,借位,以及闰年,非闰年的月份天数问题:

给出函数:

--是否为闰年:

	//是否为闰年int is_leapyear(int year){if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){return 1;}return 0;}

--获取月分的天数:
 

	//获取每一个月份的天数int GetMonthDay(int year, int month){int month_day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//存放每个月的月份int is_leap = is_leapyear(year);if (is_leap && month == 2){return month_day[month] + 1;//是闰年,2月有29天}else{return month_day[month];//不是闰年且不是2月}}

功能声明:

这里同样可以实现复用:

写好+=后可以复用+,写好-=可以复用-

为什么不是+复用+=呢?

--+天数不改变原来日期,+=会改变,直接用+=复用的时候,拷贝一份,传+=后的即可

实现:

日期+=天数

//日期+=天数(d1 += 100)
Date Date::operator+=(int day) 
{	if (day < 0) {return *this -= -day;}//1.直接将天数加到该日期的月数中去_day += day;//2.判断是否超出当前月数的天数while (_day > GetMonthDay(_year,_month)) {//3.超出就进位,重复_day -= GetMonthDay(_year,_month);_month++;//处理年份if (_month == 13) {_month = 1;_year++;}}return *this;
}

日期+天数

//日期+天数(d1+100)
Date Date::operator+(int day) 
{//+,不改变date,拷贝一份Date ret(*this);ret += day;return ret;
}

日期-=天数:

//日期 -= 天数(	d1 - 100)
Date Date::operator-=(int day)
{if (day < 0) {return *this += -day;}//1.天减去天_day -= day;//2.判断是否违法,违法向前面借while (_day <= 0){_month--;//借月数if(_month <= 0){_year--;_month = 12;}//借天数_day += GetMonthDay(_year, _month);}return *this;
}

日期-天数:

//日期 - 天数(	d1 - 100)
Date Date::operator-(int day) 
{	//不改d1,拷贝一下Date tmp(*this);tmp -= day;return tmp;
}

日期-日期:

//日期-日期  -
int Date::operator-(const Date& d) 
{Date max = *this;Date min = d;int flag = 1;//1.找出较小的日期if (*this < min) {max = d;min = *this;flag = -1;}int n = 0;//2.让较小的日期一直+,直到等于较大的日期(加了多少次,它们就差了多少天)while (min != max) {++min;++n;}//flag用来处理大年减小年或小年减大年的情况return n*flag;}

赋值运算符重载(=)

//赋值
void Date::operator=(const Date& x) 
{_year = x._year;_month = x._month;_day = x._day;
}

效果:

3.前/后置++,--

前/后置++,--会有歧义:

如++运算符重载后的到底是前置++,还是后置++呢?

这里使用函数重载解决,改变形参来区分

这里说明一下为什么前置可以使用引用返回,后置却不行:

--前置会先改变d1,再++,直接把d1传回去就行了

--后置会先使用原来的,再++,所有要拷贝一份,传回去的是拷贝的tmp,出栈就销毁了,所有不能传引用

++:

//前置++
Date& Date::operator++() 
{*this += 1;return *this;
}//后置++
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp; 
}

--:

1

//前置--
Date& Date::operator--() 
{*this -= 1;return *this;
}//后置--
Date Date::operator--(int) 
{Date tmp(*this);*this -= 1;return tmp;
}

效果:

http://www.mmbaike.com/news/70057.html

相关文章:

  • 网站搜索引擎优化诊断网站搜索引擎优化的方法
  • 宜昌网站建设厂家如何做好搜索引擎优化工作
  • 四川遂宁做网站的公司pc端百度
  • 学做网站 为了熊掌号运营培训班
  • 微网站平台成人营销管理培训班
  • wordpress主题the 7特点站长seo查询工具
  • 公众号做视频网站会封吗seo专员工资待遇
  • 客户关系管理系统简称搜索引擎seo关键词优化
  • 阳泉做网站公司女生学市场营销好吗
  • 旅游网站首页设计模板seo排名怎么样
  • 黄冈网站开发如何自己创建网址
  • 网站没有收录百度官方网站
  • 英文网站优化网络设计
  • 公司设计网站推荐免费广告发布平台
  • 自己做网站需要什么材料广告开户南京seo
  • 高端网站制作软件中山百度推广公司
  • 上海市建设交通工会网站产品推广平台
  • 网站建设管理西安做网站哪家好
  • 做网站的公司现在还 赚钱吗现在搜索引擎哪个比百度好用
  • 沧州百度建设网站微信公众号怎么推广
  • 网站做的支付宝接口吗百度有刷排名软件
  • 怎么做服装外贸网站推广平台的方式有哪些
  • 备案的博客网站可以做别的吗seo优化几个关键词
  • 广州网站制作公司优化微信广告推广平台
  • wordpress安装vps郭生b如何优化网站
  • 企业网站推广成功案例谷歌seo和百度seo
  • 外贸公司网站建设费用 如何申请百度快速提交入口
  • 政府网站需求分析如何宣传推广自己的店铺
  • 做网站需要学习哪些如何做企业产品推广
  • 网站建设与维护教学计划搜索引擎营销的模式有哪些