一、基础函数与类
我们实现一个类,最基本的成员变量和构造函数和析构函数是必须存在的。
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month);
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);
// 拷贝构造函数
// d2(d1)
Date(const Date& d);
//打印当前日期的函数
void print()
{
printf("%d年%d月%d日\n", _year, _month, _day);
}
private:
int _year;
int _month;
int _day;
};
上面的形式是在对象内部声明,我们可以在对象外实现内部成员函数
由于日期类的成员都是简单的内置类型,我们可以不用写析构函数,这个时候编译器会自动生成一个简单的析构函数,可以将其处理,需要注意的是,如果成员变量有数组之类或者复杂的自定义类型时候,我们需要手动写一个析构函数进行处理
// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
int m[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 m[month];
}
}
// 全缺省的构造函数
Date::Date(int year , int month, int day)
{
_year = year;
_month = month;
_day = day;
}
// 拷贝构造函数
// d2(d1)
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
我们运行一下
二、运算符重载概念
如果我们想对日期进行加减之类的操作,这个时候就需要我们使用运算符重载,在operator后面加上重载的运算符就可以自定义实现对运算符的重载
Date& operator+=(int day);
我们在实现部分运算符需要注意返回值,因为我们经常会有a=b=c=0,这样的操作,为什么我们先实现+=呢,因为+不改变自身,我们实现+的时候可以复用+=
// 日期+=天数
Date& Date::operator+=(int day)
{
_day += day;//我们先直接把天加上
while (_day > GetMonthDay(_year, _month))//判断天数是否超过当前月份的最大天数
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
那么operator内部是怎么实现的呢
#include"Date.h"
void test1()
{
Date d1(2023, 5, 2);
d1 += 10;//实际上我们在类后面使用运算符时候,由于我们重载了该运算符所以就会调用下面这行代码
d1.operator+=(10);//上一行代码和这一行代码是等价的
d1.print();
}
三、实现运算符重载
我们在实现运算符重载的时候可以多复用,比如实现>可以写成!(<=)来提高效率。下面就是完整的代码了
#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"
// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
int m[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 m[month];
}
}
// 全缺省的构造函数
Date::Date(int year , int month, int day)
{
_year = year;
_month = month;
_day = day;
}
// 拷贝构造函数
// d2(d1)
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
// 日期+=天数
Date& Date::operator+=(int 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)
{
Date tmp(*this);
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)
{
Date tmp(*this);
tmp -= day;
return tmp;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
_day -= day;
while (_day < 1)
{
if (_month == 1)
{
_year--;
_month = 12;
_day += 31;
}
else
{
_day += GetMonthDay(_year, --_month);
}
}
return *this;
}
// 前置++
Date& Date::operator++()
{
*this += 1;
return *this;
}
// 后置++
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
//在这里要注意c++编译器为了区分前置和后置运算符的区别,规定后置运算符声明中要有一个int形参,用此来区别
// 后置--
Date Date::operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
// 前置--
Date& Date::operator--()
{
*this -= 1;
return *this;
}
// >运算符重载
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;
}
else
{
return false;
}
}
// ==运算符重载
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) || (*this == d);
}
// <运算符重载
bool Date:: operator < (const Date& d)
{
return !(*this >= d);
}
// <=运算符重载
bool Date::operator <= (const Date& d)
{
return !(*this > d);
}
// !=运算符重载
bool Date::operator != (const Date& d)
{
return !(*this == d);
}
// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
int flag = 1;
int count = 0;
Date big(*this);
Date min(d);
if (*this < d)
{
big = d;
min = *this;
flag = -1;
}
while (big != min)
{
min++;
count++;
}
return count * flag;
}