C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4728字,纯文字阅读大概需要7分钟。
内容图文
#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> #include<stdlib.h> using namespace std; class Date { public: Date(int year=0, int month=0, int day=0) :_year(year) , _month(month) , _day(day) { cout << "构造函数" << endl; } Date( const Date& d) { cout << "拷贝构造函数" << endl; _year = d._year; _month = d._month; _day = d._day; } /*Date& operater = (const Date& d) { cout << "运算符重载" <<endl; }*/ bool operator == (const Date&d) { return this->_year == d._year && this->_month == d._month &&this->_day = d._month; } //大于 bool operator > (const Date& d) { if (this->_year > d._year) { return true; } else { if (this->_year == d._year) { if (this->_month > d._month) { return true; } else { if (this->_month == d._month) { if (this->_day > d._day) { return true; } else { return false; } } else { return false; } } } else { return false; } } } //小于 bool operator < (const Date& d) { if (this->_year < d._year) { return true; } else { if (this->_year == d._year) { if (this->_month < d._month) { return true; } else { if (this->_month == d._month) { if (this->_day < d._day) { return true; } else { return false; } } else { return false; } } } else { return false; } } } //大于等于 bool operator >= (const Date& d) { if (this->_year >= d._year) { return true; } else { return false; } } //小于等于 bool operator <= (const Date& d) { if (this->_year <= d._year) { return true; } else { return false; } } //Date operator+ (int day); Date operator+ (int day) { } void Display() { cout << _year << "-" << _month << "-" << _day<<endl; } private: //判断平年闰年 bool IsLeapYear(int year) { if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) { return true; } else { return false; } } //判断天数 int GetMonthDay(int year, int month) { if (year && month) { int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int day = monthArray[month]; if (month == 2 && IsLeapYear(year)) { day += 1; } return day; } } //加天数,Date operator+ (int day) Date operator+ (int day) { int rest = 0; if (day >= 0) { this->_day += day; while (this->_day > GetMonthDay(2016, 5)) { rest = this->_day - GetMonthDay(2016, 2); if (this->_month != 12) { this->_month++; } else { this->_year++; this->_month = 1; } this->_day = rest; } } else { Date operator - 50; } return *this; } //加天数,Date operator+= (int day); Date operator+= (int day ) { Date tmp(*this); tmp._day += day; int rest = 0; if (day > 0) { while (tmp._day > GetMonthDay(2016, 5)) { rest = tmp._day - GetMonthDay(2016, 2); if (tmp._month != 12) { tmp._month++; } else { tmp._year++; tmp._month = 1; } tmp._day = rest; } } else { Date operator-= 50; } return tmp; } //减天数,Date operator- (int day); Date operator - (int day) { int rest = 0; if (day <= 0) { this->_day -= day; while (this->_day > GetMonthDay(2016, 5)) { rest = this->_day - GetMonthDay(2016, 2); if (this->_month != 1) { this->_month--; } else { this->_year--; this->_month = 12; } this->_day = rest; } } else { Date operator + 50; } return *this; } //减天数,Date operator-= (int day); Date operator-= (int day) { int rest = 0; Date tmp(*this); if (day <= 0) { tmp._day -= day; while (tmp._day > GetMonthDay(2016, 5)) { rest = tmp._day - GetMonthDay(2016, 2); if (tmp._month != 1) { tmp._month--; } else { tmp._year--; tmp._month = 12; } tmp._day = rest; } } else { Date operator + 50; } return tmp; } //前置++ Date operator++() { if (day > GetMonthDay(int year, int month)) { if (month != 12) { ++this->day; } else { ++year; month = 1; day = 1; } } else { ++day; } return *this; } //后置++ Date operator++(int) { Date tmp(*this); tmp._day; if (day > GetMonthDay(int year, int month)) { if (month != 12) { ++tmp.day; } else { ++tmp.year; tmp.month = 1; tmp.day = 1; } } else { ++tmp.day; } return tmp; } //前置-- Date operator++() { if (day > GetMonthDay(int year, int month)) { if (month != 1) { ++this->day; } else { --year; month = 12; day = 1; } } else { --day; } return *this; } //后置-- Date operator--(int) { Date tmp(*this); tmp._day; if (day > GetMonthDay(int year, int month)) { if (month != 1) { --tmp.day; } else { --tmp.year; tmp.month = 12; tmp.day = 1; } } else { --tmp.day; } return tmp; } ~Date() { cout << "析构函数" << endl; } private: int _year; int _month; int _day; }; void Test1() { Date d; d.Display(); Date ret = operator + (50); } int main() { Test1(); system("pause"); return 0; }
原文:http://10740184.blog.51cto.com/10730184/1736131
内容总结
以上是互联网集市为您收集整理的C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数)全部内容,希望文章能够帮你解决C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。