C++ Date类型定义 (类的简单实践案例)

//头文件#include<iostream>
#include<assert.h>
using namespace std;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);// 赋值运算符重载// 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);// <运算符重载bool operator < (const Date& d);// <=运算符重载bool operator <= (const Date& d);// !=运算符重载bool operator != (const Date& d);// 日期-日期 返回天数int operator-(const Date& d);void Print();
private:int _year;int _month;int _day;};

cpp文件

#include"date.h"// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{assert(month > 0 && month < 13);static int monthDays[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 monthDays[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(){}// 日期+=天数
Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month > 12){_month -= 12;_year++;}}return *this;
}// 日期+天数
Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}// 日期-天数
Date Date::operator-(int day)
{Date tmp(*this);tmp -= (day);return tmp;
}// 日期-=天数
Date& Date::operator-=(int day)
{_day -= day;while (_day < 1){_month--;if (_month < 1){_month += 12;_year--;}_day += GetMonthDay(_year, _month);}return *this;
}// 前置++
Date& Date::operator++()
{*this += 1;return *this;
}// 后置++
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}// 后置--
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;elsereturn false;
}// ==运算符重载
bool Date::operator==(const Date& d)
{if (_year == d._year && _month == d._month && _day == d._day)return true;
}// >=运算符重载
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;Date max = *this;Date min = d;if (*this < d){int flag = -1;max = d;min = *this;}int n = 0;while (min != max){++min;++n;}return n * flag;
}void Date::Print()
{cout << _year << ' ' << _month << ' ' << _day << endl;
}

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

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

相关文章

LeetCode-02

225. 用队列实现栈 用两个队列实现栈的功能&#xff0c;思路如下&#xff1a; 往空队列中放新元素把非空队列中的元素依次放入刚才添加了新元素的队列&#xff0c;直到非空队列变为空队列 class MyStack(object):def __init__(self):self.queue1 []self.queue2 []def push(…

【教程】Kotlin语言学习笔记(四)——方法(持续更新)

写在前面&#xff1a; 如果文章对你有帮助&#xff0c;记得点赞关注加收藏一波&#xff0c;利于以后需要的时候复习&#xff0c;多谢支持&#xff01; 【Kotlin语言学习】系列文章 第一章 《认识Kotlin》 第二章 《数据类型》 第三章 《数据容器》 第四章 《方法》 文章目录 【…

突发,Anthropic推出突破性Claude 3系列模型,性能超越GPT-4

&#x1f989; AI新闻 &#x1f680; 突发&#xff0c;Anthropic推出突破性Claude 3系列模型 摘要&#xff1a;人工智能创业公司Anthropic宣布推出其Claude 3系列大型语言模型&#xff0c;该系列包括Claude 3 Haiku、Claude 3 Sonnet和Claude 3 Opus三个子模型&#xff0c;旨…

Cesium 自定义Primitive-绘制圆

一、创作来源 1、cesium的entity绘制圆 2、不使用entity的情况下&#xff0c;使用自定义的primitive来动态绘制圆 3、结合上一篇文章的圆&#xff0c;执行动态圆的更新 二、编写步骤 1、创建绘制线的类 包括构造函数、绘图函数以及销毁函数 import { Viewer, ScreenSpaceEven…

docker-compose启动postgres数据库,实现主从备份

文章目录 1. 主库2. 从库3. 测试 1. 主库 创建pg-m 目录&#xff0c;并进入该目录创建docker-compose.yml文件&#xff0c;内容如下&#xff1a; version: "3.1" services:pg_master:image: postgres:15.3container_name: pg_masterenvironment:POSTGRES_PASSWORD:…

Domain Adaptation Vs. Prompt-Tuning:能否用域自适应解决大模型提示学习问题?

点击蓝字 关注我们 AI TIME欢迎每一位AI爱好者的加入&#xff01; 作者简介 李江梦&#xff0c;中国科学院软件研究所天基综合信息系统全国重点实验室助理研究员 论文简介 今天介绍的是被机器学习领域顶级学术会议ICLR 2024接收的论文&#xff1a;BayesPrompt: Prompting Large…

广东Lenovo SR588服务器维修升级硬盘内存

本案例描述了对联想SR588服务器进行硬件升级的过程&#xff0c;包括更换固态硬盘作为系统盘&#xff0c;以及增加内存容量至128GB。升级后&#xff0c;服务器性能得到显著提升&#xff0c;同时通过重新配置RAID阵列和操作系统的重新安装&#xff0c;确保了系统的稳定性和数据的…

STM32 NAND FLASH知识点

1.NAND FLASH的简介 NAND FLASH 的概念是由东芝公司在 1989 年率先提出&#xff0c;它内部采用非线性宏单元模式&#xff0c;为固态大容量内存的实现提供了廉价有效的解决方案。 NAND FLASH 存储器具有容量较大&#xff0c;改写速度快等优点&#xff0c;适用于大量数据的存储&…

MySQL EXPLAIN 性能分析工具详解

EXPLAIN 是 MySQL 中一个非常重要的性能分析工具&#xff0c;它用于显示 MySQL 如何执行 SQL 查询。通过 EXPLAIN&#xff0c;你可以查看查询的执行计划&#xff0c;从而理解查询是如何被优化和执行的&#xff0c;从而找出可能的性能瓶颈。 如何使用 EXPLAIN 使用 EXPLAIN 非…

CC++语言强制类型转换的类型

介绍 在C语言中&#xff0c;可以使用强制类型转换&#xff08;也称为显式类型转换&#xff09;来改变一个变量的类型。这通常在你需要将一个类型的值转换为不兼容的另一种类型时非常有用。 强制类型转换的语法如下&#xff1a; (type_name) expression其中&#xff0c;type_…

Unix Network Programming Episode 88

‘inetd’ Daemon On a typical Unix system, there could be many servers in existence, just waiting for a client request to arrive. Examples are FTP, Telnet, Rlogin, TFTP, and so on. With systems before 4.3BSD, each of these services had a process associate…

如何利用Flutter来写后端 服务端应用

前言 Flutter是谷歌推出的一款跨平台开发框架&#xff0c;现在属于此领域star最多的框架&#xff0c;其被广泛应用于构建前台界面&#xff0c;但或许很少人知道&#xff0c;他也可以写后端应用。 本文主角 flutter非常著名的getx库推出的get server jonataslaw/get_server:…

实验01-STP+链路聚合+VRRP实验

1.实验拓扑 2 实验需求 根据拓扑图配置IP地址。交换机之间通过STP防环为了防止SW2-SW3之间聚合的高效链路被STP 阻塞&#xff0c;请配置SW2 为网络中的主根&#xff0c;SW3为网络中的备份根桥。通过VRRP实现网关冗余&#xff0c;网关在SW2和SW3上&#xff0c;其中VLAN10的网关…

【3GPP】【核心网】【5G】5G核心网协议解析(一)(超详细)

1. 5G核心网概念 5G核心网是支撑5G移动通信系统的关键组成部分&#xff0c;是实现5G移动通信的重要基础设施&#xff0c;它负责管理和控制移动网络中的各种功能和服务。它提供了丰富的功能和服务&#xff0c;支持高速、低时延、高可靠性的通信体验&#xff0c;并为不同行业和应…

前端监控为什么采用GIF图片做埋点?

一、什么是埋点监控 前端监控是开发人员用来跟踪和维护应用程序表现层的运行状况的过程和工具。它主要包括三种类型&#xff1a;数据监控、性能监控和异常监控。 1、数据监控 主要是为了收集跟用户相关的数据&#xff0c;例如用户设备类型、浏览器版本、页面浏览量&#xff08;…

GIS之深度学习05:VisualStudio安装教程

在安装CUDA前&#xff0c;建议先安装VisualStudio&#xff0c;以防报错 VisualStudio安装步骤简单&#xff0c;但时间较长。。。。。。 正文开始&#xff1a; VisualStudio官网&#xff1a;Visual Studio: IDE and Code Editor for Software Developers and Teams 点击右上角…

XUbuntu22.04之解决:仓库xxx没有数字签名问题(二百一十七)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 优质专栏&#xff1a;多媒…

Gitlab: PHP项目CI/CD实践

目录 1 说明 2 CI/CD 2.1 部署方式一&#xff1a;增量部署 2.1.1 目标服务器准备 2.2.2 Gitlab及Envoy脚本 2.2 部署方式二&#xff1a;镜像构建与部署 2.2.1 推送到私有化容器仓库 准备工作 脚本 要点 2.2.2 推送到hub.docker.com 准备工作 脚本 3 参考&#x…

1905_ARMv7-M的堆栈寄存器

1905_ARMv7-M的堆栈寄存器 全部学习汇总&#xff1a; g_arm_cores: ARM内核的学习笔记 (gitee.com) ARMv7-M实现了2种堆栈&#xff0c;分别是MSP和PSP。复位的时候默认是MSP&#xff0c;而当前是哪种可以通过CONTROL.SPSEL寄存器的bit来查看。 SP寄存器的最低2bit&#xff0c;S…

⭐每天一道leetcode:27.移除元素(简单;vector)

⭐今日份题目 给你一个数组 nums 和一个值 val&#xff0c;你需要 原地 移除所有数值等于 val 的元素&#xff0c;并返回移除后数组的新长度。 不要使用额外的数组空间&#xff0c;你必须仅使用 O(1) 额外空间并 原地 修改输入数组。 元素的顺序可以改变。你不需要考虑数组中…