目录
一:日期类实现
//头文件
#include <iostream>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& out, const Date d);
friend istream& operator>>(istream& in, const Date d);
public:
Date(int year = 1900, int month = 1, int day = 1);
void Print()
{
cout << "年 " << _year << "月 " << _month<<"日 " << _day << endl;
}
//得到对应年月的天数
int GetMonthDay(int year, int month)
{
static int day[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if(month == 2 &&((year%100 != 0&& year%4==0) || (year % 400 == 0)))
return day[month]+1;
else
return day[month];
}
bool CheckDay();
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)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>>(istream& in, const Date d);
//实现
#include "Date.h"
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
bool Date::CheckDay()
{
if (_month > 12 || _month < 0 || _day <0 || _day > GetMonthDay(_year, _month))
return false;
else
return true;
}
bool Date::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;
}
bool Date::operator<=(const Date d)const
{
return ((*this) < d) || ((*this) == d);
}
bool Date::operator==(const Date d)const
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
bool Date::operator>=(const Date d)const
{
return ((*this) > d) || ((*this) == d);
}
bool Date::operator>(const Date d)const
{
return !((*this) <= d);
}
bool Date::operator!=(const Date d)const
{
return !((*this) == d);
}
Date& Date::operator+=(int day)
{
if (day < 0)
return (*this) -= -day;
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month > 12)
{
_year++;
_month = 1;
}
}
return (*this);
}
Date Date::operator+(int day)
{
Date tmp = *this;
tmp += day;
return tmp;
}
Date& Date::operator-=(int day)
{
if (day < 0)
return (*this) += -day;
_day -= day;
while (_day <= 0)
{
if (_month-- < 1)
{
_month = 12;
_year--;
}
_day += GetMonthDay(_year, _month);
}
return (*this);
}
Date Date::operator-(int day)
{
Date tmp = *this;
tmp -= day;
return tmp;
}
Date Date::operator++()
{
return (*this) += 1;
}
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
Date Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
int Date::operator-(const Date d)
{
int flag = 1;
int n = 0;
//假设d1 大, d2小
Date d1 = *this;
Date d2 = d;
if (d1 < d2)
{
flag = -1;
d1 = d;
d2 = *this;
}
while (d1 != d2)
{
n++;
d2++;
}
return n * flag;
}
ostream& operator<<(ostream& out, const Date d)
{
out << "年 " << d._year << "月 " << d._month << "日 " << d._day << endl;
return out;
}
istream& operator>>(istream& in, const Date d)
{
cout << "请输入年月日->";
in >> d._year >> d._month >> d._day;
return in;
}
二:const成员
将 const 修饰的 " 成员函数 " 称之为 const 成员函数 , const 修饰类成员函数,实际修饰该成员函数
隐含的 this 指针 ,表明在该成员函数中 不能对类的任何成员进行修改。
class Date
{
public:
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
//void Print(Date* const this) 本身指向的对象是不能修改的,但是指向的内容可以改
void Print()
{
cout << "年" << _year << endl;
cout << "月" << _month << endl;
cout << "日" << _day << endl;
}
//void Print(const Date* const this) 本身指向的对象 和 指向的对象的内容 都不可以修改
void Print()const
{
cout << "年" << _year << endl;
cout << "月" << _month << endl;
cout << "日" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2024, 4, 28);
d1.Print();
Date d2(2024, 4, 27);
d2.Print();
return 0;
}
三:取地址及const取地址操作符重载
这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
class Date
{
public:
Date* operator&()
{
return this;
}
const Date* operator&()const
{
return this;
}
private:
int _year;
int _month;
int _day;
};
这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需
要重载,比如 想让别人获取到指定的内容!