#pragma once
#include
using namespace std;
class Date
{
//重载输入输出流
//直接在类里实现的话,this会抢占第一个参数的位置,
//导致调用时d1<>(istream& in, Date& d);
public:
//构造函数
Date(int year = 2000, int month = 1, int day = 1);
//拷贝构造
Date(const Date& d);
//打印
void print();
//当月天数
int GetMonthDay(int year, int month);
//比较 都可以用const修饰*this
bool operator==(const Date& d);
bool operator!=(const Date& d);
bool operator>=(const Date& d);
bool operator<=(const Date& d);
bool operator>(const Date& d);
bool operator<(const Date& d);
//日期+-,不改变自身可以用const修饰,例如+ -
Date operator+=(int day);
Date operator+(int day);
Date operator-=(int day);
Date operator-(int day);
Date& operator++();
Date& operator++(int);
Date& operator--();
Date& operator--(int);
int operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
//ostream& operator<<(ostream& out, const Date& d);
//istream& operator>>(ostream& in, Date& d);
#pragma once
#include"Date.h"
Date::Date(int year , int month , int day )
{
if (month > 0 && month < 13
&& (day > 0 && day <= GetMonthDay(year, month)))
{
_year = year;
_month = month;
_day = day;
}
else
cout << "日期非法" << endl;
}
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
void Date::print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
int Date::GetMonthDay(int year, int month)
{
static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = days[month];
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
day += 1;
}
return day;
}
bool Date::operator==(const Date& d)
{
return (_year == d._year) && (_month==d._month) & (_day==d._day);
}
bool Date::operator>(const Date& d )
{
return _year > d._year ||
(_year == d._year && _month > d._month) ||
(_year == d._year && _month == d._month && _day > d._day);
}
bool Date::operator>=(const Date& d)
{
return *this == d || *this > d;
}
bool Date::operator<(const Date& d)
{
return!(*this >= d);
}
bool Date::operator<=(const Date& d)
{
return!(*this > d);
}
bool Date::operator!=(const Date& d)
{
return!(*this == d);
}
Date Date::operator+=(int day)
{
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
Date Date::operator+(int day)
{
Date temp = *this;
temp += day;
return temp;
}
Date Date::operator-=(int day)
{
_day -= day;
while (_day < 0)
{
--_month;
if (_month == 0)
{
_year--;
_month = 12;
}
_day += GetMonthDay(_year,_month);
}
return *this;
}
Date Date::operator-(int day)
{
Date temp = *this;
temp -= day;
return temp;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date& Date::operator++(int)
{
Date temp = *this;
temp += 1;
return temp;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date& Date::operator--(int)
{
Date temp = *this;
temp -= 1;
return temp;
}
int Date::operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
{
flag = -1;
max = d;
min = *this;
}
int n = 0;
while (min != max)
{
++min;
n++;
}
return flag*n;
}
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;
}
将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
#include
using namespace std;
class Date
{
public:
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
void Print()
{
cout << "Print()" << endl;
cout << "year:" << _year << endl;
cout << "month:" << _month << endl;
cout << "day:" << _day << endl << endl;
}
void Print() const
{
cout << "Print()const" << endl;
cout << "year:" << _year << endl;
cout << "month:" << _month << endl;
cout << "day:" << _day << endl << endl;
}
private:
int _year=2000;
int _month=1;
int _day=1;
};
int main()
{
Date d1(2022, 1, 13);
d1.Print();
const Date d2(2022, 1, 13);
d2.Print();
return 0;
}
这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
class Date
{
public :
Date* operator&()
{
return this ;
}
const Date* operator&()const
{
return this ;
}
private :
int _year ; // 年
int _month ; // 月
int _day ; // 日
};
这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如想让别人获取到指定的内容