前面我们讲过了类和对象,现在我们来建立一个日期类来实践一下吧。
一、日期类的函数声明
首先 我们和之前一样先写头文件
这个类它包括了日期的构造函数、日期的拷贝、日期的打印、日期的运算、包括两个日期的比较、相减、日期加减天数等运算。下面是日期类的函数声明
#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
class Date
{
// 友元函数声明
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
Date(int year = 1900, int month = 1, int day = 1);
void Print() const;
int GetMonthDay(int year, int month)
{
assert(month > 0 && month < 13);
static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// 365天 5h +
if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return 29;
}
else
{
return monthDayArray[month];
}
}
bool CheckDate();
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;
// d1 += 100
Date& operator+=(int day);
Date operator+(int day) const;
// d1 -= 100
Date& operator-=(int day);
// d1 - 100;
Date operator-(int day) const;
// d1 - d2
int operator-(const Date& d) const;
Date& operator++();
Date operator++(int);
Date& operator--();
Date operator--(int);
private:
int _year;
int _month;
int _day;
};
// 重载
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
// 直接定义类里面,他默认是inline
// 频繁调用
int GetMonthDay(int year, int month)
{
assert(month > 0 && month < 13);
static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// 365天 5h +
if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return 29;
}
else
{
return monthDayArray[month];
}
}
二、日期类的实现
1.日期类的构造函数
是日期类的构造函数,这里是函数的实现,由于是分文件,所以不能在这里写缺省参数,应该将缺省参数放在函数声明中。同时我们为了方便最好写一个获取某月的天数的函数,其中的天数的数组我们可以将他放到静态区。因为我们会频繁的调用它。
bool Date::CheckDate()
{
if (_month < 1 || _month > 12
|| _day < 1 || _day > GetMonthDay(_year, _month))
{
return false;
}
else
{
return true;
}
}
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!CheckDate())
{
cout << "日期非法" << endl;
}
}
2.日期类的析构和拷贝构造函数
之前的学习,我们可以知道函数事实上是不需要我们去写的,因为成员变量都是内置类型所以不需要写,使用编译器自己生成的就可以了。
3.日期类的比较之d1<d2
这个运算符重载的功能是为了比较两个日期的大小,如果d1小于d2那么返回真,否则返回假。其中x是形参,他是d2的别名。且功能中并未涉及到修改d2,所以最好加上const,权限缩小。
// d1 < d2
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;
}
这里d1<d2会被编译器认为是d1.operator<(d2)
4.日期类的其他比较
// d1 <= d2
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);
}
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);
}
5.日期类的+=和-=
首先我们需要判断的是day是否小于0,如果小于0的话,那么其实就可以相互复用。如果是大于0的话,就需要注意算法。先将day给加上去,然后如果day大于当月天数,那么就可以进位了。-=也是类似的思路。这里我们可以返回引用,因为我们返回的是原本的日期类,出了作用域,这个日期类还在
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)
{
++_year;
_month = 1;
}
}
return *this;
}
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;
}
6.日期类加减具体天数
事实上在这里我们可以直接复用+=和-=完成,但是由于加并不会改变原本的类,所以我们先使用拷贝构造一个,然后再返回即可,但是这里需要注意,我们必须得返回值而不是引用,因为出了作用域tmp就不在了(写这两个主要是进行对比,了解效率)
Date Date::operator+(int day) const
{
Date tmp = *this;
tmp += day;
return tmp;
}
Date Date::operator-(int day) const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
这里我们可能会注意到一个问题,是自己写一个+=,然后写+的时候直接复用+=好,还是自己先写一个+,然后写+=的时候直接复用+好?
这里我们就得思考一下+和+=的效率问题了, 直接使用+这个我们是不可避免的肯定会调用两次拷贝构造函数。而先写+=却不需要调用拷贝构造,如果后写+=的话,调用+的时候就会调用两次拷贝构造了。所以先写+=再让+复用+=效率要高一点。
7.前置++和后置++
这个比较容易实现,但是需要注意的是,如何区分前置++和后置++,在c++中,我们如果不带任何形参的话默认是前置++,如果带了一个形参的话,那就认为他是后置++。
这里的实现我们可以直接复用+=
//++d1
Date& Date::operator++()
{
*this += 1;
return *this;
}
// d1++
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
8.前置–和后置–
这个思路和前面基本一致
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
9.d1-d2
这是运算符重载,但是和日期减某天这两个函数名相同,但参数类型不同,于是又构成了函数重载。这里我们采用循环计数的方法思路。
// d1 - d2
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;
}
10.流插入
注意,这个函数不可以写成成员函数。因为我们的Date对象默认占用第一个操作数。我们必须要更改操作数的位置,所以只能定义再全局中。但是这样我们就不可以使用私有的成员了,我们就有两种方案来处理,一种是在写一些获取每个成员值的函数,另外一种就是使用友元函数,你是我的朋友,所以可以来访问我的成员变量。
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
流插入不能写成成员函数,因为Date做了左操作数,写出来就是下面的样子,不符合我们的使用习惯。
例:
void Date::operator<<(ostream& out)
{
out << _year << "年" << _month << "月" << _day << "日" << endl;
}
调用时:
d1<<cout;
注意:cout的类型是ostream,而且他不可以加上const,因为我们是在cout里面进行写入的。我们可以直接传别名过去使得效率更高。流插入运算符在库函数里事实上也是经历了大量的重载,才使得我们的cout可以自动识别类型。最后为了可以实现连续流插入,我们需要返回out。
11.流提取
这个运算符与cout类似,我们也是必须使用友元函数进行声明。注意的是,这里的in和d都不可以加上const,因为他们都会被修改。最后为了连续的流提取,我们需要返回in
istream& operator>>(istream& in, Date& d)
{
cout << "请依次输入年月日:>";
in >> d._year >> d._month >> d._day;
if (!d.CheckDate())
{
cout << "日期非法" << endl;
}
return in;
}
三、完整代码
Date.h
#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
class Date
{
// 友元函数声明
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
Date(int year = 1900, int month = 1, int day = 1);
void Print() const;
int GetMonthDay(int year, int month)
{
assert(month > 0 && month < 13);
static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// 365天 5h +
if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return 29;
}
else
{
return monthDayArray[month];
}
}
bool CheckDate();
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;
// d1 += 100
Date& operator+=(int day);
Date operator+(int day) const;
// d1 -= 100
Date& operator-=(int day);
// d1 - 100;
Date operator-(int day) const;
// d1 - d2
int operator-(const Date& d) const;
Date& operator++();
Date operator++(int);
Date& operator--();
Date operator--(int);
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"
bool Date::CheckDate()
{
if (_month < 1 || _month > 12
|| _day < 1 || _day > GetMonthDay(_year, _month))
{
return false;
}
else
{
return true;
}
}
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!CheckDate())
{
cout << "日期非法" << endl;
}
}
void Date::Print() const
{
cout << _year << "-" << _month << "-" << _day << endl;
}
// d1 < d2
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;
}
// d1 += 50
// d1 += -50
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)
{
++_year;
_month = 1;
}
}
return *this;
}
Date Date::operator+(int day) const
{
Date tmp = *this;
tmp += day;
return tmp;
}
// d1 -= 100
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;
}
//++d1
Date& Date::operator++()
{
*this += 1;
return *this;
}
// d1++
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;
}
// d1 - d2
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)
{
cout << "请依次输入年月日:>";
in >> d._year >> d._month >> d._day;
if (!d.CheckDate())
{
cout << "日期非法" << endl;
}
return in;
}
test.c
#include"Date.h"
void TestDate1()
{
Date d1;
Date d2(2023, 5, 13);
d1.Print();
d2.Print();
cout << (d1 < d2) << endl;
cout << (d1 == d2) << endl;
cout << (d1 > d2) << endl;
cout << (d1 <= d2) << endl;
d2 += 200;
d2.Print();
Date d3 = d2;
d3 -= 200;
d3.Print();
d1 = d3 + 200;
d1.Print();
}
void TestDate2()
{
Date d1(2023, 5, 13);
d1 -= -100;
d1.Print();
d1 += -100;
d1.Print();
}
void TestDate3()
{
Date d1(2023, 5, 13);
Date ret1 = d1--;
ret1.Print();
Date ret2 = --d1;
ret2.Print();
}
void TestDate4()
{
Date d1(2023, 5, 13);
Date d2(2049, 6, 13);
cout << (d2 - d1) << endl;
cout << d1 << d2;
cin >> d2 >> d1;
cout << d2 << d1;
}
void TestDate5()
{
Date d1;
cin >> d1;
}
int main()
{
TestDate5();
return 0;
}