类和对象(3)日期类的实现

日期类的实现

  • 一,声明
  • 二,函数成员定义
    • 2.1构造函数
    • 2.2获取月份天数
    • 2.3比较运算符
      • 2.3.1等于和大于
      • 2.3.2其他
    • 2.4计算运算符
      • 2.4.1 +=&&+
      • 2.4.2-=&&-
    • 2.5日期-日期

一,声明

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>=(const Date& y);bool operator<=(const Date& y);//计算运算符int operator-(const Date& d);Date& operator+=(int day);Date operator+(int day);Date& operator-=(int day);Date operator-(int day);Date& operator++();Date operator++(int);Date& operator--();Date operator--(int);
private:int _year;int _month;int _day;
};

二,函数成员定义

2.1构造函数

Date::Date(int year,int month,int day)
{_year = year;_month = month;_day = day;if (_year < 1 || _month < 1 || _month>12 || _day<1 || _day>GetMonthDay(_year, _month)){Print();cout << "日期非法" << endl;}
}

这里要注意,构造函数的声明定义分离,给缺省值的时候,只在声明的地方给,不然会出错。

2.2获取月份天数

//获取月份天数
int Date::GetMonthDay(int year, int month)
{assert(year >= 1 && month >= 1 && month <= 12);int monthArray[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;return monthArray[month];
}

2.3比较运算符

2.3.1等于和大于

bool Date::operator==(const Date& y)
{return _year == y._year && _month == y._month&& _day == y._day;
}
bool Date::operator>(const Date& y)
{if (_year > y._year){return true;}else if (_year == y._year){if (_month > y._month){return true;}else if (_month == y._month){if (_day > y._day){return true;}}}return false;
}

写完了这两个那么其他的运算符我们都可以复用来简化代码。

2.3.2其他

bool Date::operator!=(const Date& y)
{return !(*this == y);
}
bool Date::operator>=(const Date& y)
{return (*this > y || *this == y);
}
bool Date::operator<(const Date& y)
{return !(*this>=y);
}
bool Date::operator<=(const Date& y)
{return!(*this > y);
}

2.4计算运算符

2.4.1 +=&&+

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){_month = 1;_year++;}}return *this;
}

+可以复用+=

Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}

补充:这里除了+去复用+=,可以反过来吗?
这里我们就要从效率的角度去看待这个问题。在这里插入图片描述
我们分别对比,他们的拷贝构造,可以看出用+=去复用+资源更浪费。

2.4.2-=&&-

Date& Date::operator-=(int day)
{if (day < 0){return *this += (-day);}_day -= day;while (_day<=0){_month--;if (_month < 1){_month = 12;_year--;}_day += GetMonthDay(_year, _month);}return *this;
}

一样的复用

Date Date::operator-(int day)
{Date tmp(*this);tmp -= day;return tmp;
}

2.4.3(前置++&&后置++)&&(前置–&&后置–)

Date& Date::operator++()
{*this += 1;return *this;
}
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;
}

为例区分前置和后置,我们会在后置的参数部分加一个参数类型。

2.5日期-日期

int Date::operator-(const Date& d)
{int flag = 1;Date Max = *this;Date Min = d;if (*this < d){Max = d;Min = *this;flag = -1;}int n = 0;while (Max != Min){++Min;++n;}return n * flag;
}

找出两个天数中大的那个,然后让小的天数一直++,直到相等。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/216561.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[BJDCTF 2020]easy_md5

md5(string,raw) 所以首先我们要找到一个字符串&#xff0c;这个字符串经过md5得到的16位原始二进制的字符串能帮我们实现sql注入。 我们的目标就是要找一个字符串取32位16进制的md5值里带有276f7227这个字段的&#xff0c;接着就是要看关键的数字部分了&#xff0c;在276f72…

自动语音识别 支持86种语言 Dragon Professional 16 Crack

从个体从业者到全球组织&#xff0c;文档密集型行业的专业人士长期以来一直依靠 Dragon 语音识别来更快、更高效地创建高质量文档&#xff0c;减少管理开销&#xff0c;以便他们能够专注于客户。了解 Dragon Professional v16 如何通过单一解决方案提高标准&#xff0c;为各个业…

Clion在Windows下build时出现undefined reference,即使cmake已经正确链接第三方库(如protobuf)?

你是否正在使用clion自带的vcpkg来安装了protobuf&#xff1f; 或者你是否自己使用visual studio自己编译了libprotobuf.lib&#xff1f; 你是否已经正确在CmakeLists.txt中添加了以下命令&#xff1a; find_package(Protobuf CONFIG REQUIRED) include_directories(${Protobu…

c++数学表达式解析求值库推荐

让程序支持自定义的数学计算器是常用功能&#xff0c;找了几个用来数学表达式解析求值的库&#xff0c;有几个看起来很不错。 各个数学表达式求值的综合对比 对比结果如下&#xff0c;ExprTK得分最高&#xff0c; TinyExpr得分最低&#xff0c; GitHub - ArashPartow/math-par…

新版画中画documentPictureInPicture API使用

关于该API&#xff0c;chrome dev有一篇比较好入门的文章&#xff0c;如果你没看过强烈推荐你先看这篇基础用法&#xff0c;该文章只针对API的特性和chrome dev文章进行扩展性说明。 提前说明&#xff0c;目前该API是非w3c草案功能&#xff0c;从chrome 116开始已经强推到stabl…

ASO优化之如何测试应用的屏幕截图

截取屏幕截图并上传到应用商店后&#xff0c;我们需要对其进行测试和优化&#xff0c;从而来获得更高的转化率&#xff0c;精美的图片有助于提高应用在商店的安装率。 1、定义目标受众。 战略性地决定测试哪些目标受众&#xff0c;可以通过年龄、性别、地点、兴趣等来定义我们…

超声波雪深传感器冬季里的科技魔法

在冬季的某个清晨&#xff0c;当你打开大门&#xff0c;被厚厚的积雪覆盖的大地映入眼帘&#xff0c;你是否曾想过&#xff0c;这片雪地的深度是多少&#xff1f;它又如何影响着我们的生活和环境&#xff1f;今天&#xff0c;我们将为你揭开这个谜团&#xff0c;介绍一款神秘的…

AHSATA模块之AHCI HBA卡开发,结合SPEC文档和项目实际底层FW开发总结(一)

目录 一、简介二、总体架构和常用术语总结2.1 总结介绍2.2 常用术语解析 三、详细流程3.1 总结3.2 物理层详解3.3 链路层、传输层详解3.4 命令层详解 四、FW开发4.1 pcie header配置4.2 PMCAP和MSICAP配置4.3 pcie capbility配置4.4 Generic Host Control配置4.5 Port Register…

EEG 脑电信号处理合集(2): 信号预处理

脑电信号在采集完以后&#xff0c;需要进行一系列的预处理操作&#xff0c;然后才能用于后续的科学研究和计算。预处理是脑电信号分析最基本且重要的一步。基于python环境MNE库。 1 使用带通滤波器&#xff0c;信号滤波&#xff0c;去噪&#xff0c;去工频干扰 data_path sam…

【计网 可靠数据传输RDT】 中科大笔记 (十 一)

目录 0 引言1 RDT的原理RDT的原理&#xff1a; 2 RDT的机制与作用2.1 重要协议停等协议&#xff08;Stop-and-Wait&#xff09;:连续ARQ协议: 2.2 机制与作用实现机制&#xff1a;RDT的作用&#xff1a; &#x1f64b;‍♂️ 作者&#xff1a;海码007&#x1f4dc; 专栏&#x…

【自主探索】基于 rrt_exploration 的单个机器人自主探索建图

文章目录 一、rrt_exploration 介绍1、原理2、主要思想3、拟解决的问题4、优缺点 二、安装环境三、安装与运行1、安装2、运行 四、配置自己的机器人1、Robots Network2、Robots frame names in tf3、Robots node and topic names4、Setting up the navigation stack on the rob…

CTA-GAN:基于生成对抗性网络的主动脉和颈动脉非集中CT血管造影 CT到增强CT的合成技术

Generative Adversarial Network–based Noncontrast CT Angiography for Aorta and Carotid Arteries 基于生成对抗性网络的主动脉和颈动脉非集中CT血管造影背景贡献实验方法损失函数Thinking 基于生成对抗性网络的主动脉和颈动脉非集中CT血管造影 https://github.com/ying-f…