前言
在上一篇文章中,我们学习了类的六种默认成员函数以及运算符重载等知识,在本文中,我们将通过上文知识实现Date类。
文章目录
[+、+=、-、-= 重载](#+、+=、-、-= 重载)
[+= 重载](#+= 重载)
[+ 重载](#+ 重载)
[-= 重载](#-= 重载)
[- 重载](#- 重载)
[附 - 计算两个Date类相差的天数](#附 - 计算两个Date类相差的天数)
[< 以及 == 运算符重载](#< 以及 == 运算符重载)
Date类基本框架
cpp
class Date
{
public:
Date(int year=2000, int month=1, int day=1)
{
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
以上就是Date类的基本框架,那么我们将在此基础上对这些功能进行补充,同时在最后会附上完整的代码。
由于要实现的代码内容较多,我们同样会分为三个文件:Date.h Date.cpp Test.cpp。
我们先给出头文件中的代码主要是函数的声明,后面会依次对这些函数进行实现。
cpp
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& out, const Date& date);
friend istream& operator>>(istream& in, Date& date);
public:
bool Checkdate();
Date(int year = 2000, int month = 1, int day = 1);
void Print();
int Getmonth_day(int year, int month)//默认内联函数,频繁调用的短小函数不会建立栈帧
{
static int days[] = { 31,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 days[month];
}
bool operator<(const Date& date);
bool operator==(const Date& date);
bool operator!=(const Date& date);
bool operator>(const Date& date);
bool operator<=(const Date& date);
bool operator>=(const Date& date);
//计算某一时间过了多少天之后的时间
Date& operator+=(int day);
Date operator+(int day);
//计算某一时间前多少天的时间
Date& operator-=(int day);
Date operator-(int day);
Date operator++(int);//后置++
Date& operator++();//前置++
Date operator--(int);//后置--
Date& operator--();//前置--
//计算两个时间相差多少天
int operator-(Date& date);
//如果我们把<< >>写成类成员函数
/*void operator<<(ostream& out);*/
private:
int _year;
int _month;
int _day;
};
这里要注意,当缺省构造函数的声明与定义分离时,只需要对声明的参数进行缺省就可以了
给个详细的代码展示:
cpp
Date(int year = 2000, int month = 1, int day = 1);
cpp
Date::Date(int year, int month, int day)//分离时,声明给缺省值
{
_day = day;
_month = month;
_year = year;
}
+、+=、-、-= 重载
在写代码之前我们先想清楚这些运算符重载对于Date类的意义。
+= 重载
当我们用d1+=day,得到的应该是d1过了day天之后的时间,返回的应该是被修改之后的d1。我们要完成这个操作之前,我们是不是应该知道某年某月的天数,那样才方便我们进行+=之后的运算,同时后面的运算符重载也会多次调用。
cpp
int Getmonth_day(int year, int month)//默认内联函数,频繁调用的短小函数不会建立栈帧
{
static int days[] = { 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 days[month];
}
我们将每月的天数存储在一个天数的数组中,同时让下标与月数进行对应,只需要注意闰年的2月有29天即可。我们说过类的成员函数默认是内联函数,所以我们为了不必要的资源浪费,我们就把这个函数也写成类的成员函数。
cpp
Date& Date::operator+=(int day)
{
if (day < 0)
{
return *this -= (-day);
}
_day += day;
while (_day > Getmonth_day(_year, _month))
{
_day -= Getmonth_day(_year, _month);
_month++;
if (_month > 12)
{
_month = 1;
_year++;
}
}
return *this;
}
那我们需要注意,如果day<0,那么实现的就是-=的逻辑,这里我们虽然还没有写出来,但是后面会实现的。
+ 重载
+就是返回d1过了day天之后的时间,但是d1不能被修改
cpp
Date Date::operator+(int day)
{
Date tmp = *this;
/*tmp._day += day;
while (tmp._day > Getmonth_day(tmp._year, tmp._month))
{
tmp._day -= Getmonth_day(tmp._year, tmp._month);
tmp._month++;
if (tmp._month > 12)
{
tmp._month = 1;
tmp._year++;
}
}*/
tmp += day;
return tmp;
}
最简单的方法就是创建一个变量来接收d1,然后该变量进行修改,那么就可以复用+=的逻辑了,注意这里是传值返回,因为创建的变量是局部的,函数结束后会销毁,那么引用就是空引用了。虽然传值返回会创建临时拷贝浪费空间时间效率,但是我们首要保证的是正确性。
-= 重载
思路是当天数被减成负值后,向前面的月份进行借位,直到天数成为正值。
cpp
Date& Date::operator-=(int day)
{
if (day < 0)
{
return *this += (-day);
}
_day -= day;
while (_day <= 0)
{
_month--;//我们先对月份进行--,判断是否为0
if (_month == 0)
{
_month = 12;
_year--;
}
_day += Getmonth_day(_year, _month);
}
return *this;
}
- 重载
复用-=逻辑即可
cpp
Date Date::operator-(int day)
{
Date tmp = *this;
tmp -= day;
return tmp;
}
测试:
cpp
//测试+ += -=
void test01()
{
Date d1;
Date d2 = d1 - 100;
d1.Print();
d2.Print();
Date ret=d1 + 10;
d1.Print();
ret.Print();
d1 += 10;
d1.Print();
d1 -= 100;
d1.Print();
}
测试结果正确,代码基本无误。
附 - 计算两个Date类相差的天数
我们只需要让小的一直++,直到两个相等就行了,++的次数就是相差的天数
cpp
int Date::operator-(Date& date)
{
Date min = date;
Date max = *this;
int count_day = 0;
if (*this < date)
{
min = *this;
max = date;
}
while (min != max)
{
++min;
++count_day;
}
return count_day;
}
比较运算符重载
< 以及 == 运算符重载
cpp
bool Date::operator<(const Date& date)//const 保证date不被修改
{
//把所有true的情况列举出来,那么其他的就是false
if (_year < date._year)
{
return true;
}
else if (_year == date._year && _month < date._month)
{
return true;
}
else if (_year == date._year && _month < date._month && _day < date._day)
{
return true;
}
else
return false;
}
bool Date::operator==(const Date& date)
{
return _year == date._year && _month == date._month && _day == date._day;
}
上面的逻辑应该很好理解,< 只需要把所有true的类型列举出来,那么其余的全都是false ;== 就是年月日要全部对应相等。
其余比较运算符重载
事实上,我们并不需要把所有的运算符的逻辑全部写出来,因为运算符是有逻辑关系的,比如
!(<=) 那么就是 > ......,后面的运算符重载全部都可以对前面的代码进行复用。
cpp
bool Date::operator!=(const Date& date)
{
return !(*this == date);
}
bool Date::operator>(const Date& date)
{
return !(*this < date || *this == date);
}
bool Date::operator<=(const Date& date)
{
return !(*this > date);
}
bool Date::operator>=(const Date& date)
{
return !(*this < date);
}
注意:这样的思想可以用到后面的很多类当中,即以后如果我们要实现类中比较运算符的重载,那么只需要写出两种就可以一般写( > 、 = ) 或者( < 、= ),后面的都可以通过复用快速实现。
自增、自减运算符重载
自增运算符重载
在上一篇文章中,我们说到过,后置++重载需要在函数中加int形参
cpp
Date Date::operator++(int)//后置++,返回原对象的拷贝
{
Date tmp = *this;
*this += 1;//原对象天数+=1
return tmp;
}
Date& Date::operator++()//前置++,返回+=之后的原对象
{
*this += 1;
return *this;
}
自减运算符重载
这里我们就只需要把上面的+=换成-=
cpp
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
流操作重载
那如果我们想要自己输入对象的成员变量的值呢?即对于自定义类型的变量怎么进行输入输出?
首先printf scanf 排除,因为这两个只能处理内置类型的输入删除,那我们就需要考虑cin cout,与其相关的是<< >>。我们发现,其实C++中已经实现了 >> <<,对于内置类型的重载,那么我们也可以进行尝试。
cpp
*this<<out
void Date::operator<< (ostream& out)
{
out << _year << "年" << _month << "月" << _day << "日" << endl;
}
可是我们调用是发现,由于this指针的存在,我们只能反着写:
cpp
Date d1;
d1.operator<<(cout);
d1 << cout;
那我们尝试写全局函数重载,但是因为成员变量是私有型,如果我们的全局函数想要调用那么就需要写get函数,这样就比较复杂了,还好C++中有友元函数可以支持调用,这样就可以解决全局函数无法调用类中私有类型的成员变量的问题,我们只需要在类中进行声明,在前面加friend.

cpp
ostream& operator<<(ostream& out, const Date& date)
{
out << date._year << '/' << date._month << '/' << date._day << endl;
return out;
}
istream& operator>>(istream& in, Date& date)
{
in >> date._year >> date._month >> date._day;
return in;
}
完整代码在我的Github中:代码链接