C++第九弹---类与对象(六)

04-08 阅读 0评论

C++第九弹---类与对象(六)

✨个人主页: 熬夜学编程的小林

💗系列专栏: 【C语言详解】 【数据结构详解】【C++详解】

日期类

1、日期类的分析和设计

1.1、日期类的功能说明

1.2、日期类的分析和设计

1.2.1、数据结构的分析

1.2.2、文件结构设计

2、日期类的结构分析

2.1、创建日期类及声明

2.2、在类内实现获取天数函数

2.3、在类外实现构造函数

 2.4、在类外实现拷贝构造函数

2.5、在类外实现赋值运算符重载

2.6、在类外实现日期的比较

2.7、在类外实现日期+-天数

2.8、在类外实现日期-日期

3、日期类分文件的代码实现

3.1、test.cpp

3.2、Date.cpp

3.3、Date.h

总结


1、日期类的分析和设计

1.1、日期类的功能说明

创建一个日期类,通过运算符重载实现对日期的相关计算以及比较功能。包含如下:

1、重载日期的大小比较(>  ==  >=  运算符重载 bool operator>(const Date& d) const; // ==运算符重载 bool operator==(const Date& d) const; // >=运算符重载 bool operator >= (const Date& d) const; // 运算符重载 bool Date::operator>(const Date& d) const { if (_year > d._year) return true; else if (_year == d._year) { if (_month > d._month) return true; else if (_month == d._month) { if (_day > d._day) return true; } } 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; } // = d); } // GetMonthDay(_year, _month)) { _day -= GetMonthDay(_year, _month); ++_month; if (_month == 13) { ++_year; _month = 1; } } return *this; } // 日期+天数 ---使用前面实现的+=运算符重载实现 //Date Date::operator+(int day) const //{ // Date temp(*this); // temp += day; // return temp; //} // 日期+天数 ---直接实现 Date Date::operator+(int day) const { Date temp(*this); temp._day += day; while (temp._day > GetMonthDay(_year, _month)) { temp._day -= GetMonthDay(temp._year, temp._month); ++temp._month; if (temp._month == 13) { ++temp._year; temp._month = 1; } } return temp; } // 日期-=天数 Date& Date::operator-=(int day) { _day -= day; while (_day d._year) return true; else if (_year == d._year) { if (_month > d._month) return true; else if (_month == d._month) { if (_day > d._day) return true; } } 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; } // = d); } // GetMonthDay(_year, _month)) { _day -= GetMonthDay(_year, _month); ++_month; if (_month == 13) { ++_year; _month = 1; } } return *this; } // 日期+天数 ---使用前面实现的+=运算符重载实现 //Date Date::operator+(int day) const //{ // Date temp(*this); // temp += day; // return temp; //} // 日期+天数 ---直接实现 Date Date::operator+(int day) const { Date temp(*this); temp._day += day; while (temp._day > GetMonthDay(_year, _month)) { temp._day -= GetMonthDay(temp._year, temp._month); ++temp._month; if (temp._month == 13) { ++temp._year; temp._month = 1; } } return temp; } // 日期-=天数 Date& Date::operator-=(int day) { _day -= day; while (_day d2.operator=(&d2, d3) // >运算符重载 bool operator>(const Date& d) const; // ==运算符重载 bool operator==(const Date& d) const; // >=运算符重载 bool operator >= (const Date& d) const; //


免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,人围观)

还没有评论,来说两句吧...

目录[+]