日期类的实现
Data.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month) //由于这个函数会要频繁的调用,所以将这个函数写在类里面,本质为类函数
{
assert(month > 0 && month < 13);
static int Monthday[] = { 0,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;
}
return Monthday[month];
}
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
// 拷贝构造函数
// d2(d1)
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
// 析构函数
~Date()
{
_year = 0;
_month = 0;
_day = 0;
}
// 日期+=天数
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);
void print()
{
cout << _year << "/" << _month << "/" << _day <<endl;
}
private:
int _year;
int _month;
int _day;
};
Data.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"
Date& Date::operator+=(int day)
{
_day += day;
while(_day> GetMonthDay(_year,_month))
{
_day = _day - GetMonthDay(_year, _month);
_month = _month + 1;
if (_month==13)
{
_year++;
_month = 1;
}
}
return *this;
}
Date Date::operator+(int day)
{
Date tem = *this;
tem += day;
return tem;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day = _day + GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day)
{
Date tem = *this;
tem += day;
return tem;
}
Date& Date::operator++()
{
return *this += 1;
}
// 后置++
Date Date::operator++(int)
{
Date tem = *this;
*this += 1;
return tem;
}
// 后置--
Date Date::operator--(int)
{
Date tem = *this;
*this -= 1;
return *this;
}
// 前置--
Date& Date::operator--()
{
return *this -= 1;
};
bool Date::operator>(const Date& d)
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year && _month > d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day > d._day)
return true;
else
return false;
}
bool Date::operator==(const Date& d)
{
if (_year == d._year && _month == d._month && _day == d._day)
{
return true;
}
else
return false;
}
bool Date::operator >= (const Date& d)
{
if (*this > d || *this == d)
{
return true;
}
return false;
}
bool Date::operator != (const Date& d)
{
return !(*this == d);
}
2.日期类的补充
用上面的日期类测试下面的代码
const Date d1(2024, 3, 24);
d1.print();
会发现printf会打印不了
原因:因为加上const以后d1不可以修改了,而传到print()函数以后由&d1 const Data变为了this Date,printf()没有const的权限,权限扩大所以会报错
解决的办法是将printf()函数也加上const,这样它们的权限就是一样的了
void print() const
{
cout << _year << "/" << _month << "/" << _day <<endl;
}
权限的扩大是不可以的,但是权限的缩小是可以的,像下面就是权限的缩小
void print() const
{
cout << _year << "/" << _month << "/" << _day <<endl;
}
int main()
{
Date d1(2024, 3, 24);
d1.print();
}