高邮建设银行网站,百度怎么直接访问网址,建设一个网站平台的费用吗,网站建设需要什么东西#x1f525;博客主页#xff1a;小王又困了
#x1f4da;系列专栏#xff1a;C
#x1f31f;人之为学#xff0c;不日近则日退
❤️感谢大家点赞#x1f44d;收藏⭐评论✍️ 目录
一、日期类的实现
#x1f4d2;1.1日期类功能
#x1f4d2;1.2拷贝日期
#… 博客主页小王又困了
系列专栏C
人之为学不日近则日退
❤️感谢大家点赞收藏⭐评论✍️ 目录
一、日期类的实现
1.1日期类功能
1.2拷贝日期
1.3重载关系运算符
1.4重载、
1.5重载 -、-
1.6重载、-- 一、日期类的实现 通过前面的知识我们要实现一个日期类巩固前面学习的类和对象。这里我们也要使用多文件来完成我们的日期类。
1.1日期类功能 头文件中是我们要实现日期类功能的函数声明。这里我们要注意拷贝函数只能在函数声明时写缺省值防止我们在声明和定义是给的缺省值不一样。
#include iostream
#include assert.h
using namespace std;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;
};
1.2拷贝日期 有时输入的日期可能是非法的例如月份大于12日期大于31还有闰2月天数等。所以我们要对输入的日期进行判断因为每个月的天数不同所以要用到GetMonthDay函数。
Date::Date(int year, int month, int day)
{_year year;_month month;_day day;if (_year 1 || _month 1 || _month12 || _day 1 || _day GetMonthDay(_year, _month)){//assert(false);Print();cout 日期非法 endl;}
}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];
}
小Tips我们先判断是否是二月在判断是否是闰年可以提高效率。
1.3重载关系运算符 关系运算符有、、、、、!我们在写实现这些功能的代码时会发现它们逻辑相同会复制粘贴但这样的代码看着十分冗余我们可以通过复用来实现让代码更简单。
重载
bool Date::operator(const Date y)
{return _year y._year _month y._month _day y._day;
}重载!
bool Date::operator!(const Date y)
{return !(*this y);
}
重载
bool Date::operator(const Date d)
{if (_year d._year){return true;}else if (_year d._year _month d._month){return true;}else if (_year d._year _month d._month _day d._day){return true;}else{return false;}
}
重载
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);
}
1.4重载、 我们想知道50天之后的日期就可以通过重载、来实现。在加天数的时候由于每个月的天数不一样所以进位就不同我们要得到每个月份的天数。 获得月份的天数
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];
} 小Tips把month 2放在前面判断可以提高效率如果不是二月就不需要判断是否是闰年如果是二月在判断。 重载
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;
} 重载
Date Date::operator(int day)
{if(x 0){return *this - (-day);}Date tmp(*this);tmp._day _day x;while (tmp._day GetDay(tmp._year, tmp._month)){tmp._day tmp._day - GetDay(tmp._year, tmp._month);tmp._month;if (tmp._month 13){tmp._year;tmp._month 1;}}return tmp;
}小Tips重载是原来的日期不会改变所以我们拷贝构造了一个和*this相同的对象我们对tmp对象修改不会改变*this。重载是在原来的日期上直接修改所以我们直接对*this指向的日期进行修改然后返回就可以。
为什么重载用引用返回 在重载中*this就是d1它的作用域是函数结束后才销毁由于传值返回会拷贝一份返回值所以为了减少返回时的拷贝所以使用引用返回。在重载中tmp出了operator函数就被销毁所以只能使用传值返回。
和之间的复用
复用 Date Date::operator(int day)
{Date tmp(*this);tmp day;return tmp;
}
1.5重载 -、- 有时我们想知道以前的日期日期-天数可以知道多少天以前的日期日期-日期可以知道两个日期直接隔了多少天。两个operator-函数构成了函数重载。
重载-
Date operator-(int x)
{//天数天数小于0复用if (x 0){return *this -x;}_day - x;while (_day 0){_month--;if (_month 0){_month 12;_year--;}_day GetDay(_year, _month);}return *this;
}重载日期-天数
Date Date::operator-(int x)
{Date tmp(*this);return tmp - x;
}重载日期-日期 日期-日期计算的结果是两个日期之间的天数所以返回值是int要知道两个日期之间相隔的天数可以设置一个计数器让小日期一直加到大日期就可以知道两个日期之间相隔的天数。
int operator-(const Date d)
{Date max *this;//存放大日期Date min d;//存放小日期int flag 1;if (*this d){max d;min *this;flag -1;}int n 0;while (max ! min){--max;n;}return n * flag;
}1.6重载、--
重载前置 前置要返回之后的值
Date Date::operator()
{*this 1;//复用return *this;
}重载前置 后置要返回之前的值
Date Date::operator(int)//编译器会把有int的视为后置
{Date tmp(*this);*this 1;//复用return tmp;
}小Tips这两个operator函数构成了函数重载那调用的时候怎么区分前置和后置呢后置重载的时候多增加一个int类型的参数使用后置时调用运算符重载函数时不用传递参数编译器自动传递。
重载前置 -- 前置--要返回--之后的值
Date operator--()
{*this - 1;//复用-return *this
}重载后置 -- 后置--要返回--之前的值
Date operator--(int)
{Date tmp(*this);*this - 1;//复用了-return tmp;
}结语 本次的内容到这里就结束啦。希望大家阅读完可以有所收获同时也感谢各位读者三连支持。文章有问题可以在评论区留言博主一定认真认真修改以后写出更好的文章。你们的支持就是博主最大的动力。