【C++】:拷贝构造函数与赋值运算符重载的实例应用之日期类的实现
温馨提示:这篇文章已超过401天没有更新,请注意相关的内容是否还可用!
C++实现日期类 ├─属性: │ ├─年份 │ ├─月份 │ └─日期 ├─方法: │ ├─构造函数 │ ├─拷贝构造函数 │ ├─析构函数 │ ├─设置年份 │ ├─设置月份 │ ├─设置日期 │ ├─获取年份 │ ├─获取月份 │ ├─获取日期 │ ├─判断是否为闰年 │ ├─计算该日期是该年的第几天 │ ├─计算该日期是星期几 │ └─重载运算符(+、-、==、!=、、=)
一、📚头文件的声明(Date.h)
🔑日期类的实现,将按以下声明依次进行,其中因为Print函数比较短,直接放到类里面让其变成内联函数
#pragma once
#include
#include
using namespace std;
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1);
void Print();
int GetMonthDay(int year, int month);
bool operator==(const Date& y);
bool operator!=(const Date& y);
bool operator>(const Date& y);
bool operator=(const Date& y);
bool operator
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month)
{
static int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31};
int day = days[month];
if (month == 2
&&((year % 4 == 0 && year % 100 != 0) || (year%400 == 0)))
{
day += 1;
}
return day;
}
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);
// 拷贝构造函数
// d2(d1)
Date(const Date& d);
// 赋值运算符重载
// d2 = d3 - d2.operator=(&d2, d3)
Date& operator=(const Date& d);
// 析构函数
~Date();
// 日期+=天数
Date& operator+=(int day);
// 日期+天数
Date operator+(int day);
// 日期-天数
Date operator-(int day);
// 日期-=天数
Date& operator-=(int day);
// 前置++
Date& operator++();
// 后置++
Date operator++(int);
// 后置--
Date operator--(int);
// 前置--
Date& operator--();
// >运算符重载
bool operator>(const Date& d);
// ==运算符重载
bool operator==(const Date& d);
// >=运算符重载
bool operator >= (const Date& d);
//
assert(year = 1 && month = 1 && month 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 monthArray[month];
}
_year = year;
_month = month;
_day = day;
if (_year 的比较
bool Date::operator>(const Date& y)
{
if (_year > y._year)
{
return true;
}
else if (_year == y._year && _month > y._month)
{
return true;
}
else if (_year == y._year && _month == y._month && _day > y._day)
{
return true;
}
return false;
}
六、📚==运算符重载
bool Date::operator==(const Date& y)
{
return _year == y._year
&& _month == y._month
&& _day == y._day;
}
七、📚>=
// d1 != d2
bool Date::operator!=(const Date& y)
{
return !(*this == y);
}
bool Date::operator>(const Date& y)
{
if (_year > y._year)
{
return true;
}
else if (_year == y._year && _month > y._month)
{
return true;
}
else if (_year == y._year && _month == y._month && _day > y._day)
{
return true;
}
return false;
}
bool Date::operator>=(const Date& y)
{
return *this > y || *this == y;
}
bool Date::operator
return !(*this = y);
}
bool Date::operator
return !(*this y);
}
八、📚日期类的计算
🔑日期类的连续赋值
在内置类型的适合我们经常有连续赋值的习惯,类似a1=a2=a3这种,而日期类也支持连续赋值的操作对此我们返回值不能写void 而应该返回引用,我们可以减少拷贝,从而提高效率 这是一名C/C++程序员的基本素养
Date& Date::operator=(const Date& d)
{
this->_year = d._year;
this->_month = d._month;
this->_day = d._day;
return *this;
}
🔑日期类的加法
+=运算符重载和+对+=的复用
🔑+=实现的思路就是,实现一个循环,直到天数回到该月的正常天数为止,在循环内部要做的就是进月和进年,让天数不断减去本月天数,直到恢复本月正常天数时,循环结束,返回对象本身即可
// d1 += 100
Date& Date::operator+=(int day)
{
if (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;
return tmp;
}
🔑-=实现的思路就是,实现一个循环,直到天数变为正数为止,在循环内部要做的就是借月和借年,让天数不断加上上一个月份的天数,直到恢复正数为止,循环结束,返回对象本身
Date& Date::operator-=(int day)
{
if (day GetMonthDay(_year, _month))
{
//assert(false);
Print();
cout
cout
return _year == y._year
&& _month == y._month
&& _day == y._day;
}
// d1 != d2
bool Date::operator!=(const Date& y)
{
return !(*this == y);
}
bool Date::operator(const Date& y)
{
if (_year y._year)
{
return true;
}
else if (_year == y._year && _month y._month)
{
return true;
}
else if (_year == y._year && _month == y._month && _day y._day)
{
return true;
}
return false;
}
bool Date::operator=(const Date& y)
{
return *this y || *this == y;
}
bool Date::operator
return !(*this = y);
}
bool Date::operator
return !(*this y);
}
int Date::GetMonthDay(int year, int month)
{
assert(year = 1 && month = 1 && month 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 monthArray[month];
}
// d1 += 100
Date& Date::operator+=(int day)
{
if (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
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

