C++9--前置++和后置++重载,const,日期类的实现(对前几篇知识点的应用)
目录
1.前置++和后置++重载
2.const成员
3.日期类的实现
1.前置++和后置++重载
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 2024, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//前置++:返回+1之后的结果
//注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率
Date& operator++()
{
_day += 1;
return *this;
}
//后置++:
//前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确的重载
//C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传递
//注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在是实现时需要先将this保存一份
//,然后给this+1
//而temp是临时对象,因此只能以值的方式返回,不能返回引用
Date operator++(int)
{
Date temp = *this;
_day += 1;
return temp;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d;
Date d1(2022, 1, 13);
d = d1++;//d:2022,1,13 d1:2022,1,14
d = ++d1;//d:2022,1,15 d1:2022,1,15
return 0;
}
2.const成员
将const修饰的”成员函数“称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
在日期类的实现里面具体应用
3.日期类的实现
//Date.h
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{ //友元
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
//全缺省构造函数
Date(int year = 1, int month = 1, int day = 1);
void Print()const
{
cout << _year << "-" << _month << "-" << _day << endl;
}
//拷贝构造函数
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//运算符重载
bool operator<(const Date& x)const;
bool operator==(const Date& x)const;
bool operator<=(const Date& x)const;
bool operator>(const Date& x)const;
bool operator>=(const Date& x)const;
bool operator!=(const Date& x)const;
//获取某年某月的天数
int GetMonthDay(int year, int month);
//日期+=天数
Date& operator+=(int day);
//日期+天数
Date operator+(int day)const;
//日期-=天数
Date& operator-=(int day);
//日期-天数
Date operator-(int day)const;
//前置++
Date& operator++();
//后置++
Date operator++(int);
//前置--
Date& operator--();
//后置--
Date operator--(int);
//日期-日期 返回天数
int operator-(const Date& d)const;
//流插入不能写成成员函数?
//因为Date对象默认占用第一个参数,就是做了左操作数
//写出来就一定是下面这个样子,不符合使用习惯
//d1 << cout; //d1.operator<<(cout);
//void operator<<(ostream& out);
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
//Date.cpp
#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;
assert(false);
}
}
bool Date::operator<(const Date& x)const
{
if (_year < x._year)
{
return true;
}
else if (_year == x._year && _month < x._month)
{
return true;
}
else if (_year == x._year && _month == x._month && _day < x._day)
{
return true;
}
return false;
}
bool Date::operator==(const Date& x)const
{
return _year == x._year
&& _month == x._month
&& _day == x._day;
}
bool Date::operator<=(const Date& x)const
{
return *this < x || *this == x;
}
bool Date::operator>(const Date& x)const
{
return !(*this <= x);
}
bool Date::operator>=(const Date& x)const
{
return !(*this < x);
}
bool Date::operator!=(const Date& x)const
{
return !(*this == x);
}
int Date::GetMonthDay(int year, int month)
{
static int daysArr[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;
}
else
{
return daysArr[month];
}
}
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 == 13)
{
_month = 1;
++_year;
}
}
return *this;
}
Date Date::operator+(int day)const
{
Date tmp(*this);
tmp += day;
return tmp;
}
Date& Date::operator-=(int day)
{
if (day < 0)
{
return *this += _day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
_month = 12;
--_year;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day)const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
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)const
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
int year, month, day;
in >> year >> month >> day;
if (month > 0 && month < 13
&& day>0 && day <= d.GetMonthDay(year, month))
{
d._year = year;
d._month = month;
d._day = day;
}
else
{
cout << "非法日期" << endl;
assert(false);
}
return in;
}