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

贵州建设职业技术学院官方网站/墨子学院seo

贵州建设职业技术学院官方网站,墨子学院seo,微信如何开小程序,网站建设 长春今天我们实现一个日期类,对前面所学知识进行小的应用总结。 首先我们先写日期类Date最基本的东西,私有成员,构造函数,拷贝构造函数,析构函数(可以不写,编译器默认生成),赋…

今天我们实现一个日期类,对前面所学知识进行小的应用总结。

首先我们先写日期类Date最基本的东西,私有成员,构造函数,拷贝构造函数,析构函数(可以不写,编译器默认生成),赋值运算符重载。

私有成员变量:
private:int _year=1900;int _month=1;int _day=1;
构造函数:Date(int year,int month,int day):_year(year),_month(month),_day(day){ }
拷贝构造函数:
Date(const Date& d)
{_year=d._year;_month=d._month;_day=d._day;
} 
赋值运算符重载:检测是否自己给自己赋值
// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}

我们还要考虑,我们创建的日期是否合法,比如每个月的天数是否符合月数。那我们就要写一个检查日期合法的函数,检查日期又要获得当年当月的天数,又要再写一个获得月份天数的函数。

获得当年月份天数的函数:

	// 获取某年某月的天数//频繁调用,放类中定义作inlineint GetMonthDay(int year, int month){static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//使用static,静态区,在程序结束前只用开辟一次空间if (month == 2 &&(year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)){return 29;}else{return days[month];}}

 检查日期:

	bool CheckDate(){if (_year >= 1 && _month > 0 && _month < 13 && _day>0 && _day <= GetMonthDay(_year, _month)){return true;}else{return false;}}

Date.h头文件

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:// 获取某年某月的天数//频繁调用,放类中定义作inlineint GetMonthDay(int year, int month){static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };//使用static,静态区,在程序结束前只用开辟一次空间if (month == 2 &&(year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)){return 29;}else{return days[month];}}bool CheckDate(){if (_year >= 1 && _month > 0 && _month < 13 && _day>0 && _day <= GetMonthDay(_year, _month)){return true;}else{return false;}}// 全缺省的构造函数Date(int year , int month , int day):_year(year),_month(month),_day(day){}// 拷贝构造函数// d2(d1)Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}// 赋值运算符重载// d2 = d3 -> d2.operator=(&d2, d3)Date& operator=(const Date& d);// 日期+=天数Date& operator+=(int day);// 日期+天数Date operator+(int day) const;// 日期-天数Date operator-(int day) const;// 日期-=天数Date& operator-=(int day);// 前置++Date& operator++();// 后置++Date operator++(int);// 后置--Date operator--(int);// 前置--Date& operator--();// >运算符重载bool operator>(const Date& d) const;// ==运算符重载bool operator==(const Date& d) const;// >=运算符重载bool operator >= (const Date& d) const;// <运算符重载bool operator < (const Date& d) const;// <=运算符重载bool operator <= (const Date& d) const;// !=运算符重载bool operator != (const Date& d) const;// 日期-日期 返回天数int operator-(const Date& d) const;private:int _year=1900;int _month=1;int _day=1;
};

下面我们来写Date.cpp文件,定义各运算符重载 

任何一个类只需要写一个> ==或< ==重载,剩下比较运算符重载复用即可。

// >运算符重载
bool Date::operator>(const Date& d) const
{if ((_year > d._year)|| (_year == d._year && _month > d._month)|| (_year == d._year && _month == d._month && _day > d._day)){return true;}else{return false;}
}
// ==运算符重载
bool Date::operator==(const Date& d) const
{return _year == d._year&& _month == d._month&& _day == d._day;
}
// >=运算符重载
bool Date::operator>=(const Date& d) const
{return (*this == d) || (*this > d);
}// <运算符重载
bool Date::operator < (const Date& d) const
{return !(*this >= d);
}// <=运算符重载
bool Date::operator <= (const Date& d) const
{return !(*this > d);
}// !=运算符重载
bool Date::operator != (const Date& d) const
{return !(*this == d);
}

实现日期+=天数,日期+天数,日期-=天数,日期-天数。

同样的我们可以,写其中一种另一种进行复用。

这里我们采用写日期+=天数,日期-=天数。

// 日期+=天数 d2+=d1+=100
//自身要改变,并且可以连续操作使用引用返回。
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){++_year;_month = 1;}}return *this;
}// 日期+天数 d2+100
Date Date::operator+(int day) const
{Date ret = *this;ret += day;return ret;
}// 日期-天数
Date Date::operator-(int day) const
{Date ret(*this);ret -= day;return ret;
}// 日期-=天数
Date& Date::operator-=(int day)
{if (_day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (_month == 0){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}return *this;
}

 【问题】

1.这里为什么采用写+=和-=,而不是写+和-?

答:+=重载和-=重载中,并没有使用拷贝构造函数,如果使用+和-,则会使用拷贝构造函数。

Date Date::operator+(int day)
{Date ret = *this;// ...ret._day += day;while (ret._day > GetMonthDay(ret._year, ret._month)){//...}return ret;
}// d1 += 100
Date& Date::operator+=(int day)
{*this = *this + day;return *this;
}

写+的话,内部创建ret拷贝构造,值返回拷贝构造,2次拷贝构造。+=复用中,需要调用+,2次拷贝构造。

写+=的话,内部没有拷贝构造,复用+=,2个拷贝构造,所以综上还是写+=调用拷贝构造的次数少,效率好。

2.日期+天数和日期-天数,为什么是const成员函数?

答:因为日期+天数和日期-天数都不需要改变原来的日期,即this指针指向的内容,所以我们用const修饰,const Date* const this ,使指向内容不变。

 前置++,后置++,前置--,后置--

// 前置++:返回+1之后的结果// 注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率
Date& Date::operator++()
{return *this += 1;
}// 后置++// 前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载// C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传
递// 注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存一份,
然后给this+1// 而temp是临时对象,因此只能以值的方式返回,不能返回引用Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}// 后置--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}// 前置--
Date& Date::operator--()
{return *this -= 1;
}

 日期-日期 返回天数

// 日期-日期 返回天数
int Date::operator-(const Date& d) const
{//思路,用小的一天天加上去//假定前一个是大日期,后一个小日期Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++n;++min;}return n * flag;
}

Date.cpp文件内容

#include"Date.h"// >运算符重载
bool Date::operator>(const Date& d) const
{if ((_year > d._year)|| (_year == d._year && _month > d._month)|| (_year == d._year && _month == d._month && _day > d._day)){return true;}else{return false;}
}// ==运算符重载
bool Date::operator==(const Date& d) const
{return _year == d._year&& _month == d._month&& _day == d._day;
}// >=运算符重载
bool Date::operator>=(const Date& d) const
{return (*this == d) || (*this > d);
}// <运算符重载
bool Date::operator < (const Date& d) const
{return !(*this >= d);
}// <=运算符重载
bool Date::operator <= (const Date& d) const
{return !(*this > d);
}// !=运算符重载
bool Date::operator != (const Date& d) const
{return !(*this == d);
}// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}// 日期+=天数 d2+=d1+=100
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){++_year;_month = 1;}}return *this;
}// 日期+天数 d2+100
Date Date::operator+(int day) const
{Date ret = *this;ret += day;return ret;
}// 日期-天数
Date Date::operator-(int day) const
{Date ret(*this);ret -= day;return ret;
}// 日期-=天数
Date& Date::operator-=(int day)
{if (_day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (_month == 0){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}return *this;
}// 前置++
// 前置++:返回+1之后的结果// 注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率
Date& Date::operator++()
{return *this += 1;
}// 后置++// 前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载// C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传
递
// 注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存一份,
然后给this + 1
// 而temp是临时对象,因此只能以值的方式返回,不能返回引用
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}// 后置--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}// 前置--
Date& Date::operator--()
{return *this -= 1;
}// 日期-日期 返回天数
int Date::operator-(const Date& d) const
{//思路,用小的一天天加上去//假定前一个是大日期,后一个小日期Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++n;++min;}return n * flag;
}

以上就是整个日期类的实现,我们通过实现日期类可以对前面所学默认成员函数进行巩固加强。

http://www.jmfq.cn/news/5288581.html

相关文章:

  • 宜宾做直销会员网站/温州seo外包公司
  • php用什么做网站服务器吗/网络营销服务商有哪些
  • 广州企业网站设计方案/十大教育培训机构排名
  • 上海网站建设团队/百度竞价登陆
  • 网站子页面怎么做/结构优化设计
  • 网站建设名字/哈尔滨seo和网络推广
  • 做册子模板素材有哪些网站/全球最牛的搜索引擎
  • 网站如何建设移动端/大概需要多少钱
  • 学校门户网站建设的意义/全网搜索
  • 婚恋交友网站制作/推广普通话的重要意义
  • 网站建设需要会什么软件/关键词优化是什么工作
  • 东台建网站/老铁外链工具
  • 太原做企业网站的/网络销售怎么干
  • 网站建设具体实施方案/中国seo高手排行榜
  • 新沂网站建设/杭州线上推广
  • 做网站的公司简介/凡科建站的优势
  • 阿里巴巴网站导航栏怎么做/成都百度提升优化
  • mysql做wp网站/线上推广平台报价
  • 百度对网站建设公司/公司网站如何制作设计
  • 学校网站的作用/软件推广是什么工作
  • seo上海推广公司/外贸网站如何推广优化
  • 网站建设技术合同/seo营销优化软件
  • 网站改版需求分析/百度知道登录入口
  • 男女生做恶心的网站/app推广方法及技巧
  • 企业网站 个人备案/手机app软件开发
  • 创建吃的网站怎么做/百度手机助手官网
  • 青海网站建设有哪些/学seo需要学什么专业
  • 普陀专业做网站/不属于网络推广方法
  • wordpress源码 优惠券/网站seo置顶
  • 淮北市城乡建设委员会的网站/2022十大网络营销案例