前言
这篇文章是对前面知识的综合应用,加深了类的各种知识点的理解。其中会涉及:构造函数、析构函数和拷贝构造函数、操作符重载、友元、const成员和this指针等等知识点,如果有不清楚的小伙伴欢迎浏览之前的文章,希望可以给你带来帮助。下面废话不多说,直接附源码。
头文件
#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 = 2023, int month = 7, int day = 22);
//拷贝构造
Date(const Date& d);
//析构构造
~Date();
void Print();
int GetMonthDay(int year, int day);
int GetYearDay(int year);
bool operator==(const Date& d);
bool operator!=(const Date& d);
bool operator>(const Date& d);
bool operator>=(const Date& d);
bool operator<(const Date& d);
bool operator<=(const Date& d);
//还需要实现的是类+-天数 返回一个 类
//类 += 天数,返回一个类。
//类 -= 天数,返回一个类
//类 = 类,返回类
//类前置++ 和 ++类 返回类
//类 - 类 返回天数
Date operator+(int day); //d1 + d2 -> d1.operator+(d2)
Date& operator+=(int day);
Date operator-(int day);
Date& operator-=(int day);
int operator-(Date& d);
Date& operator++();
Date operator++(int);
Date& operator--();
Date operator--(int);
private:
int _year;
int _month;
int _day;
};
//cout << d --> cout.operator(d)
//运算符重载中
//重载运算符函数的参数数量与该运算符作用的运算对象数量一样多。
//一元运算符有一个参数,二元运算符有两个。
//对于二元运算符来说,左侧运算对象传递给第一个参数
//而右侧运算对象传递给第二个参数。
//可以用内联函数,提高效率
inline ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
inline istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}
源文件
#include "Date.h"
//构造函数
Date::Date(int year, int month, int day)
{
if (year < 0 || (month > 12 || month < 0) || day > GetMonthDay(year, month))
{
cout << "输入不符合规范" << endl;
exit(-1);
}
else
{
_year = year;
_month = month;
_day = day;
}
}
//拷贝构造
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//析构函数
Date::~Date()
{
_year = 0;
_month = 0;
_day = 0;
}
//打印
void Date::Print()
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
int Date::GetMonthDay(int year, int month)
{
int day[] = { 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 day[month];
}
}
int Date::GetYearDay(int year)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return 366;
else
return 365;
}
bool Date::operator==(const Date& d)
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
bool Date::operator!=(const Date& d)
{
return !(*this == d);
}
bool Date::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;
return false;
}
bool Date::operator>=(const Date& d)
{
return (*this == d) || (*this > d);
}
bool Date::operator<(const Date& d)
{
return !(*this >= d);
}
bool Date::operator<=(const Date& d)
{
return !(*this > d);
}
//类 += 天数,返回一个类。
//2023 7 27
// 80
//2023 7 107
Date& Date::operator+=(int day)
{
if (day < 0)
{
*this -= -day;
return *this;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month > 12)
{
_month = 1;
_year++;
}
}
return *this;
}
Date Date::operator+(int day)
{
Date tmp = *this;
tmp += day;
return tmp;
}
Date& Date::operator-=(int day)
{
if (day < 0)
{
*this += -day;
return *this;
}
_day -= day;
while (_day <= 0)
{
_month--;
if (_month <= 0)
{
_month = 12;
_year--;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day)
{
Date tmp = *this;
tmp -= day;
return tmp;
}
int Date::operator-(Date& d)
{
Date max = *this;
Date min = d;
int flag = 1;
if(d < *this) // ---> d.operator<(*this);
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}
//++d -> ++前置
//++d -> d1.operator()
Date& Date::operator++()
{
*this += 1;
return *this;
}
//d++ -> 后置++
//d++ -> d1.operator(int)
//参数没有实际意义,仅仅是占位,只是为了构成重载
Date Date::operator++(int)
{
Date tmp = *this;
*this += 1;
return tmp;
}
//--d
Date& Date::operator--()
{
*this -= 1;
return *this;
}
//d-- 后置--
Date Date::operator--(int)
{
Date tmp;
*this -= 1;
return tmp;
}