在学了c++类和对象基本知识以及六个默认成员函数后,我们可以上手实现一个date类出来,检验学习的效果。
接口展示:
class date{ //输出操作符重载friend ostream& operator<<(ostream& _cout, const date& d);//输出操作符重载friend istream& operator>>(istream& _cin, date& d);public: // 获取某年某月的天数 int getmonthday(int year, int month); // 全缺省的构造函数 date(int year=1988, int month=1, int day=1); // 拷贝构造函数 date(const date& d); // 赋值运算符重载 date& 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--(int); // 前置-- date& operator--(); // >运算符重载 bool operator>(const date& d); // ==运算符重载 bool operator==(const date& d); // >=运算符重载 bool operator>=(const date& d); // <运算符重载 bool operator<(const date& d); // <=运算符重载 bool operator<=(const date& d); // !=运算符重载 bool operator!=(const date& d); // 日期-日期 返回两个日期之间相隔的具体天数 int operator-(const date& d); //日期展示 void print() { cout << _year << " " << _month << " " << _day << endl; }private: int _year; int _month; int _day;};
注意:
因为对于定义在类里面的函数会自动设成内联函数,而只有一些简单的函数才建议设成内联函数,所以实现函数时我们是声明和定义分离(在类里面声明,类外定义)
在类外实现函数接口需要加上类域名称
注意:
闰年二月与平年二月的天数不同
实现代码:
//获取月份天数int date::getmonthday(int year, int month){//设置平年月天数数组static int monthdays[] = { 0,31,28,31,30,31,30,31,31,30,31,b20峰会30,31 };//设置成静态避免重复创建int day = monthdays[month];//对闰年二月的处理if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)){day = 29;}return day;}
注:打印函数比较简单,设成内联函数很适合,可以直接在类里定义
实现代码:
void date::print(){cout << _year << "年" << _month << "月" << _day << "日" << endl;}
注意:
对于构造函数建议写成全缺省函数(便于无参数初始化),但是只能定义和声明其中一个写缺省
考虑初始化的日期是否合理
实现代码:
//构造函数//类里声明date(int year = 0, int month = 1, int day = 1);//定义date::date(int year, int month, int day){// 检查日期的合法性if (year >= 0&& month > 0 && month < 13&& day > 0 && day <= getmonthday(year, month)){_year = year;_month = month;_day = day;}el{// 严格来说抛异常更好cout << "非法日期" << endl;cout << year << "年" << month << "月" << day << "日" << endl;exit(-1);}}
注:对于像date一样的类来说,析构函数(没有需要清理的空间资源),拷贝函数和赋值重载函数(能够完成成员变量浅拷贝)都不用自己写,编译器默认生成的已经足够使用
实现代码:
//析构函数date::~date(){_year = 1;_month = 0;_day = 0;}
实现代码:
//拷贝构造date::date(const date& d){_year = d._year;_month = d._month;_day= d._day;}
注意:
对于赋值操作符来说,是需要能支持连续赋值的操作,这里我们返回date本身来进行接下来的继续赋值
实现代码:
//赋值运算符重载date& date::operator=(const date& d){_year = d._year;_month = d._month;_day = d._day;return *this;}
效果图:
注意:
+=表示会修改date本身的数据处理传入负数天数处理好天数进位,月份进位实现代码:
//日期+=天数date& date::operator+=(int day){if (day < 0)//处理特殊情况{*this -= -day;//复用date-=天数}el{_day += day;whilsos团e (_day &g闺蜜生日送什么礼物好t; getmonthday(_year, _month))//处理数据合理性{_day -= getmonthday(_year, _month);_month++;if (_month > 12){ _year++;_month = 1;}}}return *this;//返回引用,即对象本身}
注意:
+天数表示不会修改date本身的数据(使用const修饰,避免修改)
逻辑与date+=天数基本一致,可以进行复用
实现代码:
date date::operator+(int day) const{date tmp = *this;//赋值重载tmp += day;//复用+=重载return tmp;//返回值(拷贝构造)}
注意:
+=表示会修改date本身的数据处理传入负数天数考虑日期的借位,月份的借位实现代码:
//日期-=天数date& date::operator-=(int day){if (day < 0){*this += -day;//复用date+=天数}el{_day -= day;while (_day <= 0)//处理数据合理性{_month--;if (_month <= 0){_year--;_month = 12;}_day += getmonthday(_year, _month);}}return *this;}
注意:
-天数不会修改date本身的数据(使用const修饰,避免修改)逻辑与date-=天数基本一致,可以进行复用实现代码:
date date::operator-(int day) const{date tmp = *this;tmp -= day;return tmp;}
注意:
前置++表示,date先增后使用
实现代码:
//++datedate& date::operator++(){*this += 1;//复用date+=天数return *this;}
注意:
语法规定,因为与前置命名相同的缘故,这里的后置函数多一个参数来与前置函数形成重载
后置++表示先使用后自增
实现代码:
//date++date date::operator++(int){date tmp = *this;//保存一份日期*this += 1;//自增当前日期return tmp;//返回自增前的日期}
实现代码:
//--datedate& date::operator--(){*this -= 1;return *this;}
实现代码:
//date--date date::operator--(int){date tmp = *this;*this -= 1;return tmp;}
注:可以多次复用
实现代码:
//日期比较bool date::operator>(const date& d) const{if (_year > d._year){return true;}el if(_year == d._year){if (_month > d._month){return true;}el if(_month == d._month){if (_day > d._day){return true;}}}> return fal;}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);}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);}
实现代码:
//日期减日期 int date::operator-(const date& d) const { //确定日期的大小 date max = *this; date min = d; if (*this < d)//复用日期比较 { max = d; min = *this; } int day = 0; while (max != min) { ++min; ++day; } return day; }
注意:
对于输入操作符,我们习惯是cin>>date,而这样的用法表示做操作数是cin,右操作数为日期对象,但是对于类成员函数来说,存在着隐含参数this指针(占据和第一个参数位置,即日期对象是左操作数)虽然定义成类外函数能修改参数位置,但是无法访问类里的私有成员变量,这里我们使用友元函数来解决,即在类里声明函数前加上friend,便可以访问成员实现代码:
//输出操作符重载ostream& operator<<(ostream& _cout, const date& d){_2021最火生日短句cout << d._year << "年" << d._month << "月" << d._day << "日" ;return _cout;}//输出操作符重载istream& operator>>(istream& _cin, date& d)> {_cin >> d._year >> d._month >> d._day;return _cin;}
效果图:
date,而这样的用法表示做操作数是cin,右操作数为日期对象,但是对于类成员函数来说,存在着隐含参数this指针(占据和第一个参数位置,即日期对象是左操作数)虽然定义成类外函数能修改参数位置,但是无法访问类里的私有成员变量,这里我们使用友元函数来解决,即在类里声明函数前加上friend,便可以访问成员实现代码:
//输出操作符重载ostream& operator<<(ostream& _cout, const date& d){_cout << d._year << "年" << d._month << "月" << d._day << "日" ;return _cout;}//输出操作符重载istream& operator>>(istream& _cin, date& d){_cin >> d._year >> d._month >> d._day;return _cin;}
效果图:
到此这篇关于c++类和对象实战之date类实现的文章就介绍到这了,更多相关c++date类的实现内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 02:31:07,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/4bc59ae27d70a17eb845c2f199be0d5e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C++类和对象实战之Date类的实现方法.doc
本文 PDF 下载地址:C++类和对象实战之Date类的实现方法.pdf
留言与评论(共有 0 条评论) |