代码
#include<iostream>
using namespace std;
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month)
{
int Day[13] = { 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 Day[month];
}
void Print() const
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
if (month > 12 || month < 1
|| day < 1 || day > 31 || day > GetMonthDay(_year, _month))
{
cout << "非法日期" << endl;
}
}
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d)
{
if (this != &d)
{
_day = d._day;
_month = d._month;
_year = d._year;
}
return *this;
}
// 日期+=天数
Date& operator+=(int day)
{
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
++_year;
_month = 1;
}
}
return *this;
}
// 日期+天数
Date operator+(int day)
{
Date tmp(*this);
tmp += day;
return tmp;
}
// 日期-天数
Date operator-(int day)
{
Date tmp(*this);
tmp -= day;
return tmp;
}
// 日期-=天数
Date& operator-=(int day)
{
_day = day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
// 前置++
Date& operator++()
{
*this += 1;
return *this;
}
// 后置++
Date operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
// 后置--
Date operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
// 前置--
Date& operator--()
{
*this -= 1;
return *this;
}
// >运算符重载
bool operator>(const Date& d)
{
return !(*this <= d);
}
// ==运算符重载
bool operator==(const Date& d)
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
// >=运算符重载
bool operator >= (const Date& d)
{
return !(*this < d);
}
// <运算符重载
bool 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 operator <= (const Date& d)
{
return *this < d || *this == d;
}
// !=运算符重载
bool operator != (const Date& d)
{
return !( * this == d);
}
// 日期-日期 返回天数
int operator-(const Date& d)
{
Date max = *this;
Date min = d;
int flag = 1;
if (max < min)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n*flag;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2023, 8, 6);
// 拷贝构造,一个已经存在的对象去初始化另一个要创建对象
Date d2(d1);
d1.Print();
d2.Print();
Date d3(2023, 8, 13);
// 赋值,两个已经存在对象进行拷贝
//d1 = d3; // d1.operator=(d3)
d1 = d2 = d3;
d1.Print();
d2.Print();
d3.Print();
d1 = d1;
int i, j;
i = j = 10;
cout << (j = 10) << endl;
return 0;
}
知识总结
1.前置++与后置++重载
前置++:返回+1之后的结果
Date& operator++()
{
*this += 1;
return *this;
}
用引用方式返回是因为this在所指向的对象函数结束后不会销毁,要用引用返回提高效率。
后置++:
Date operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
C++规定:后置++重载时比前置++多一个int类型的参数,但是传递时该参数不用传递,编译器会自动传递。
后置++是先使用后加1,所以要返回+1之前的旧值,所以在实现时要将this保存一份,再将this+1.这里不用引用返回是因为temp是临时对象,不能引用返回,采用值返回
总结:
①后置++比前置++参数多个int
②前置++是引用返回,后置++是值返回
2.const成员
2.1const成员函数
显示年月日:
void Print() const
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
为什么这里const加在函数名后面呢?
将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
如图:
2.2const修饰变量
例:
// !=运算符重载
bool operator != (const Date& d)
{
return !( * this == d);
}
const 是用来表明你不打算在该函数中修改 d,同时确保传递对象时效率更高。
2.3关于const的一些小知识
- const对象可以调用非const成员函数吗?
- 非const对象可以调用const成员函数吗?
- const成员函数内可以调用其它的非const成员函数吗?
- 非const成员函数内可以调用其它的const成员函数吗?
本文到此结束~