文章目录
- 前言
- Date类的构思
- Date类的相关实现
- 基本框架(默认成员函数)
- 计算n天前\后的日期
- 补充:前置++、后置++说明
- 判断两个日期的关系(大于,小于等);
- 可以计算两个日期之间相差多少天
- 补充:流输入以及流提取操作符实现:
- 总结
- C++语言系列学习目录
前言
在上一章节中,我们学习了类和对象的一些内容,包括:类的相关特征、类的默认成员函数、以及操作符重载(重点)。本节就综合前面的相关内容,实现一个Date类。
Date类的构思
我们设想的Date类包括以下操作:
- 可以计算n天前\后的日期(+、-、+=、-=等);
- 判断两个日期的关系(大于,小于等);
- 可以计算两个日期之间相差多少天;
Date类的相关实现
基本框架(默认成员函数)
因为Date类型比较简单,属性只有内置类型,所以大部分默认成员函数就可以满足需求。
class Date
{
public:
//构造函数:全缺省的默认构造函数,但需要判断合法性,所以需要自己定义
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);
}
}
//拷贝构造函数:Date类属性全是内置类型,可以不写拷贝构造
//赋值运算符重载:Date类属性全是内置类型,赋值运算符重载也可以不用写
//析构函数:Date类属性全是内置类型,赋值运算符重载也不用写
private:
int _year;
int _month;
int _day;
};
计算n天前\后的日期
计算n天后基本思路:
日期加上天数,天数相加,判断是否超出当月的日期天数(循环判断),若超出,则月数进1,同时判断月数是否等于13,若等于则再向年进1,月变回1月;
设计流程:
- GetMonthDay()获得当月的天数;
- Date Date::operator # (int day) const (#代表多种运算符重载);
1.GetMonthDay()
功能:传入年和月,得出该月的天数。
参数:(int year, int month)
返回:int (天数)
注意:方法一般设置在头文件之外,头文件(Date.h)声明函数方法,(Date.cpp)实现方法,因此方法前需要加入Date::类域
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 (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) && month == 2)
//不太好,应当容易简单判断的条件放在&&前,优化
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
return 29;
}
else
{
return daysArr[month];
}
}
- Date Date::operator+=(int day) (+=运算符重载);
功能:实现Date类和int整形相加,并且直接修改该对象;
参数: (int day)
返回: Date&
Date& Date::operator+=(int day)
{
if (day < 0) //考虑我们输入的day是负数
{
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类和int整形相加,返回最终日期。
参数: (int day)
返回: Date (最终日期)
Date Date::operator+(int day) const
{
//直接复用+=号
Date tmp(*this);
tmp += day;
return tmp;
}
计算n天前基本思路:
日期减去天数,天数相减,判断天数是否为负数或者零(循环判断),若为负数,则月数减1,同时判断月数是否等于0,若等于则再向年借1,月变回12月;
- Date& Date::operator-=(int day) (-=运算符重载)
功能:实现Date类和int整形相减,且直接修改该对象。
参数: (int day)
返回: Date&
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类和int整形相减,返回最终日期。
参数: (int day)
返回: Date
Date Date::operator-(int day) const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
补充:前置++、后置++说明
前置++,返回++后的值,实现时返回原本对象即可,所以可以引用返回。
后置++,返回++前的值,需要创建临时变量储存,临时变量出函数自动销毁,所以只能传值返回;
但我们运算符重载的时候发现,他们都以Date& operator++()
为声明,这会导致编译器无法区分,从而报错。从而为了区别,C++这样规定:
前置++声明:Date& operator++();
后置++声明:Date operator++(int);
为区分他们,在后置++的传参部分加入(int)int占位,以示区分,并没有其他作用。
代码实现很简单,但必须认识到这点(减减一并实现):
// 前置++
Date& Date::operator++()
{
*this += 1;
return *this;
}
// 后置++
// 增加这个int参数不是为了接收具体的值,仅仅是占位,跟前置++构成重载
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;
}
判断两个日期的关系(大于,小于等);
我们需要先判断两个日期的大小关系,才可以实现两个日期的相减;
大小关系操作数我们通常只需要实现两个最基本的,其他复用就可以了:大于或者小于,以及等于。
bool operator<(const Date& x) const
功能:判断两个日期的大小,该对象是否小于x对象;
参数:(const Date& x)
返回值: bool 布尔类型
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;
}
// 复用
// d1 <= d2
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::operator-(const Date& d) const
功能:计算两个日期之间相差多少天
参数:(const Date& d)
返回值: int (相差天数)
int Date::operator-(const Date& d) const
{
//假设法:任意假设大小;
Date max = *this;
Date min = d;
int flag = 1;//巧妙运用1和-1
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}
补充:流输入以及流提取操作符实现:
这里我们只是简单认识其中的一些问题即可,内容实现需要i/o相关库的基础,不容易懂。
- <<(>>) 流输入(流提取)他们无法在类域中实现,因为我们在使用cout和cin的过程中,第一参数并不是我们的Date(或其他类对象)而是cout和cin,所以我们需要定义为类外的函数方法;
- 我们要实现连续输入或输出,所以返回参数应当是ostream&或istream&;
- 外部定义实现的函数无法访问类中private的属性,因此需要类有相关方法或者设置该外部定义为友元函数;
class Date
{
// 友元函数声明
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
...其他代码
}
代码实现:
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;
}
完整代码如下:
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;
}
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);
// d1 + 100
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;
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;
}
// 复用
// d1 <= d2
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 (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) && month == 2)
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)
{
++_year;
_month = 1;
}
}
return *this;
}
// d1 + 100
Date Date::operator+(int day) const
{
Date tmp(*this);
tmp += day;
return tmp;
/*tmp._day += day;
while (tmp._day > GetMonthDay(tmp._year, tmp._month))
{
tmp._day -= GetMonthDay(tmp._year, tmp._month);
++tmp._month;
if (tmp._month == 13)
{
++tmp._year;
tmp._month = 1;
}
}
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;
}
// 后置++
// 增加这个int参数不是为了接收具体的值,仅仅是占位,跟前置++构成重载
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)
{
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;
}
总结
本节重点是实践各种操作符的重载,以简单Date类做案例。
C++语言系列学习目录
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加,添加超链接