【C++学习笔记】----日期类实现(运算符重载,天数计算等)

1.题目

实现日期类的运算符 +, -, +=, -=, ==, !=, >=, <= ,>, <, >> ,<<, 日期-日期等。

2.代码展示

Date.h

#include<iostream>
using namespace std;
class Date
{
public:
	//获取某月的天数
	friend int Getmonthday(int year, int month);
	//构造函数全缺省
	Date(int year, int month, int day);
		
	//析构函数
	 ~Date();
	//重载>
	 friend  bool operator>(const Date& d1, const Date& d2);
	//运算符==重载
	 friend bool operator==(const Date& d1, const Date& d2);
	//运算符!=重载
	 friend bool operator!=(const Date& d1, const Date& d2);
	//运算符>=重载
	 friend bool operator>=(const Date& d1, const Date& d2);
	//运算<重载
	 friend bool operator<(const Date& d1, const Date& d2);
	//运算<=重载
	 friend bool operator<=(const Date& d1, const Date& d2);
	//赋值运算符号 = 
	 //friend Date& operator=(Date& d1, const Date& d2);
	//日期-=天数
	 friend Date& operator-=(Date& d1, int day);
	//日期-天数
	 friend Date& operator-(Date& d1, int day);
	//日期+天数
	 friend  Date& operator+(Date& d1, int day);
	//日期+=天数
	 friend Date& operator+=(Date& d1, int day);
	//前置++
	 friend Date& operator++(Date& d1);
	//后置++
	 friend Date operator++(Date& d1, int);
	//前置--
	 friend Date& operator--(Date& d1);
	//后置--
	 friend Date operator--(Date& d1, int);
	//日期-日期
	 friend int operator-(const Date& d1, const Date&  d2);
	//<<
	friend ostream& operator<<(ostream& _out, const Date& d);
	//>>
	friend istream& operator>>(istream& _in, Date& d);
private:
	int _year;
	int _month;
	int _day;
};

Date.cpp

#include"Date.h"

	//获取某月的天数
	int Getmonthday(int year, int month){
		int day[13] = { 0, 31,28,31,30,31,30,31,30,31,30,31,30 };
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
			day[2] = 29;
		}
		return day[month];
	}
	//构造函数全缺省
	Date::Date(int year, int month, int day)
		:_year(year)
		, _month(month)
		,_day(day)
	{
		if (year >= 0 && month > 0 && month < 13 && day>0 && day <= Getmonthday(year, month))
		{
			_year = year;
			_month = month;
			_day = day;
		}else{
			cout << "非法日期" << endl;
		}
	}
	//析构函数
	Date::~Date(){}
	//运算符>重载
	 bool operator>(const Date& d1,const Date& d2){
		if (d1._year > d2._year){
			return true;
		}
		else if (d1._year == d2._year&&d1._month > d2._month){
			return true;
		}
		else if (d1._year == d2._year&&d1._month == d2._month&&d1._day > d2._day){
			return true;
		}
		return false;
	}
	//运算符==重载
	bool operator==(const Date& d1, const Date& d2) {
		if (d1._year == d2._year && d1._month == d2._month&&d1._day == d2._day){
			return true;
		}
		return false;
	}
	//运算符!=重载
	bool operator!=(const Date& d1, const Date& d2) {
		return !(d1 == d2);
	}
	//运算符>=重载
	bool operator>=(const Date& d1, const Date& d2){
		if (d1 > d2){
			return true;
		}
		else if (d1 == d2){
			return true;
		}
		return false;
	}

	//运算<重载
	bool operator<(const Date& d1, const Date& d2) {
		if (d1>= d2){
			return false;
		}
		return true;
	}

	//运算<=重载
	bool operator<=(const Date& d1, const Date& d2) {
		if (d1 > d2){
			return false;
		}
		return true;
	}
	赋值运算符号 = 
	/* Date& operator=( Date& d1, const Date& d2) {
		if (&d1 != &d2){
			d1._year = d2._year;
			d1._month = d2._month;
			d1._day = d2._day;
		}
		return d1;
	}*/
	//日期-天数
	Date& operator-=(Date& d1,int day){
		if (day < 0){
			return d1 += -day;
		}
		d1._day -= day;
		while (d1._day <= 0){
			--d1._month;
			if (d1._month == 0){
				d1._month = 12;
				--d1._year;
			}
			d1._day += Getmonthday(d1._year, d1._month);
		}
		return d1;
	}
	Date& operator-( Date& d1,int day){
		d1 -= day;
		return d1;
	}
	//日期+=天数
	Date& operator+=(Date& d1, int day) {
		if (day < 0) {
			return d1 -= -day;
		}
		d1._day += day;
		while (d1._day > Getmonthday(d1._year, d1._month)) {
			++d1._month;
			if (d1._month > 12) {
				++d1._year;
				d1._month = 1;
			}
			d1._day -= Getmonthday(d1._year, d1._month);
		}
		return d1;
	}
	//日期+天数
	Date& operator+(Date& d1, int day) {
		d1 += day;
		return d1;
	}
	
	//前置++
	Date& operator++(Date& d1){
		d1 += 1;
		return d1;
	}
	//后置++
	Date operator++(Date& d1,int) {
		Date d(d1);
		d1 += 1;
		return d;
	}
	//前置--
	Date& operator--(Date& d1) {
		d1 -= 1;
		return d1;
	}
	//后置--
	Date operator--(Date& d1, int){
		Date d(d1);
		d1 -= 1;
		return d;
	}
	//日期-日期
	int operator-(const Date& d1,const Date&  d2){
		Date max = d1;
		Date min = d2;
		int flag = 1;
		int num = 0;
		if (d1 < d2){
			max = d2;
			min = d1;
			flag = -1;
		}
		while (min != max){
			++num;
			++min;
		}
		return flag == 1 ? num : -num;
	}

//重载<<
ostream& operator<<(ostream& _out, const Date& d) {
	_out << d._year << "-" << d._month << "-" << d._day << endl;
	return _out;
}
//重载>>
istream& operator>>(istream& _in, Date& d) {
	_in >> d._year >> d._month >> d._day;
	return _in;
}
void test()
{
	Date d1(1900,1,1);
	Date d2(2020,1,1);
	//d1 = d2;
	//cout << (d1 == d2) << endl;
	//cout << (d1 != d2) << endl;
	//cout << (d1 >= d2) << endl;
	//cout << (d1 <= d2) << endl;
	//cout << (d1 > d2) << endl;
	//cout << (d1 < d2) << endl;
	//d1 + 1;
	//d1 + -1;
	//d1 - 1;
	//d1 += -1;
	//d1 += 1;
	//d1 -1;
	//d1 -= 1;
	//d1 -= -1;
	//cout<<d1++;
	//cout <<++d1;
	//cout<<d1--;
	//cout <<--d1;
	//cin >> d1;
	//cout << d1;
	//cout << (d2 - d1) << endl;
	//cout << (d1 - d2) << endl;
	
}


test.cpp

#include"Date.h"
void test();
int main(){
	test();
	system("pause");
	return 0;
}

3.心得体会

通过日期类的实现,我熟悉了运算符重载,友元类,构造函数等知识点,对类的封装性也有了深刻的理解。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来为您编写一个日期类的示例代码,满足您的要求: ```c++ #include <iostream> using namespace std; class Date { private: int year, month, day; public: Date(int y = 1970, int m = 1, int d = 1): year(y), month(m), day(d) { } bool 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) { return day < d.day; } } return false; } Date& operator ++ () { day++; if (day > getDaysInMonth()) { day = 1; month++; if (month > 12) { month = 1; year++; } } return *this; } friend bool operator == (const Date& d1, const Date& d2) { return d1.year == d2.year && d1.month == d2.month && d1.day == d2.day; } friend ostream& operator << (ostream& os, const Date& d) { os << d.year << "年" << d.month << "月" << d.day << "日"; return os; } private: int getDaysInMonth() const { int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 && isLeapYear()) { return 29; } return daysInMonth[month-1]; } bool isLeapYear() const { return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); } }; int main() { Date d1(2021, 7, 1); Date d2(2022, 1, 1); cout << (d1 < d2) << endl; // 输出 1 Date d3(2021, 7, 1); Date d4(2021, 7, 1); cout << (d3 == d4) << endl; // 输出 1 Date d5(2021, 6, 30); ++d5; cout << d5 << endl; // 输出 2021年7月1日 return 0; } ``` 其中,重载运算符 `<` 和 `++` 是成员函数,重载运算符 `=` 是友元函数。此外,我们还重载了 `<<` 运算符,使其能够方便地输出日期对象的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值