🔥个人主页:guoguoqiang. 🔥专栏:我与C++的爱恋
朋友们大家好啊,在我们学习了默认成员函数后,我们通过上述内容,来实现一个简易的日期计算器。
头文件的声明
#pragma once
#include <iostream>
using namespace std;
#include <assert.h>
class Date {
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
Date(int year = 1900, int month = 1, int day = 1);
void Print() const;
//直接定义类里面,它默认是inline
//频繁调用
int GetMonthDay(int year,int month) {
assert(month > 0 && month < 13);
static int monthDayArray[13] = {
-1,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;
}
else {
return monthDayArray[month];
}
}
bool CheckDate();
bool operator<(const Date& d)const;
bool operator<=(const Date& d)const;
bool operator>(const Date& d)const;
bool operator>=(const Date& d)const;
bool operator==(const Date& d)const;
bool operator!=(const Date& d)