当前位置: 首页 > news >正文

C++日期类的完整实现,以及this指针的const修饰等的介绍

文章目录

  • 前言
  • 一、日期类的实现
  • 二、this指针的const修饰
  • 总结


前言

C++日期类的完整实现,以及this指针的const修饰等的介绍


一、日期类的实现

// Date.h
#pragma once#include <iostream>
using namespace std;#include <assert.h>class Date
{// 友元函数friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
public:// 构造函数Date(int year = 1970, int month = 1, int day = 1);void Print() const{cout << _year << '-' << _month << '-' << _day << endl;}// 拷贝构造函数Date(const Date& d);// 析构函数~Date();// 赋值运算符重载Date& operator=(const Date& d);// 运算符重载bool operator<(const Date& d) const;bool operator==(const Date& d) const;bool operator<=(const Date& d) const;bool operator>(const Date& d) const;bool operator>=(const Date& d) const;bool operator!=(const Date& d) const;// 获取每月天数int GetMonthDay(int year, int month);// + += - -= 天数Date& operator+=(const int day);Date operator+(const int day) const;Date& operator-=(const int day);Date operator-(const int day) const;// 前置++Date& operator++();// 后置++Date operator++(int);// 前置--Date& operator--();// 后置--Date operator--(int);// 日期-日期int operator-(const Date& d) const;private:int _year;int _month;int _day;
};// 流插入
ostream& operator<<(ostream& out, const Date& d);// 流提取
istream& operator>>(istream& in, Date& d);

// Date.cpp
#define  _CRT_SECURE_NO_WARNINGS#include "Date.h"// 构造函数
Date::Date(int year, int month, int day)
{if (month > 0 && month < 13 && day > 0 && day <= GetMonthDay(year, month)){_year = year;_month = month;_day = day;}else{cout << "非法日期" << endl;assert(false);}}// 拷贝构造函数
Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}// 析构函数
Date::~Date()
{_year = 0;_month = 0;_day = 0;
}// 赋值运算符重载
Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}// 运算符重载
bool Date::operator<(const Date& d) const
{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) const
{if (_year != d._year){return false;}else if (_year == d._year && _month != d._month){return false;}else if (_year == d._year && _month == d._month && _day != d._day){return false;}else{return true;}
}bool Date::operator<=(const Date& d) const
{return (*this < d || *this == d);
}bool Date::operator>(const Date& d) const
{return !(*this <= d);
}bool Date::operator>=(const Date& d)  const
{return !(*this < d);
}bool Date::operator!=(const Date& d) const
{return !(*this == d);
}// 获取月份天数
int Date::GetMonthDay(int year, int month)
{static int dayArray[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 dayArray[month];}
}// + += - -= 天数
Date& Date::operator+=(const int day)
{if (_day < 0){return *this -= -_day;}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}return *this;
}Date Date::operator+(const int day) const
{Date tmp(*this);tmp += day;return tmp;
}Date& Date::operator-=(const int day)
{if (_day < 0){return *this += -_day;}_day -= day;while (_day <= 0){_month--;if (_month == 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}
Date Date::operator-(const int day) const
{Date tmp(*this);tmp -= day;return tmp;
}// 前置++
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;
}// 日期-日期
int Date::operator-(const Date& d) const
{Date max = *this;Date min = d;int flag = 1;if (max < min){max = d;min = *this;flag = -1;}int num = 0;while (min != max){num++;++min;}return num * flag;
}ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}istream& operator>>(istream& in, Date& d)
{int year, month, day;in >> year >> month >> day;if (month > 0 && month < 13 && day > 0 && day <= d.GetMonthDay(year, month)){d._year = year;d._month = month;d._day = day;}else{cout << "非法日期" << endl;assert(false);}return in;
}

// test.cpp
#define  _CRT_SECURE_NO_WARNINGS#include "Date.h"void TestDate1()
{Date d1(2023, 9, 1);Date d2(2000, 1, 1);Date d3(2000, 3, 1);cout << (d1 < d2) << endl;cout << (d1 == d2) << endl;cout << (d2 == d3) << endl;cout << (d1 <= d2) << endl;cout << (d1 > d2) << endl;cout << (d2 <= d3) << endl;cout << (d2 > d3) << endl;Date d4(1949, 10, 1);Date d5(1949, 10, 1);cout << (d4 != d5) << endl;
}void TestDate2()
{Date d1(2024, 5, 5);d1 += 1000;d1.Print();Date d2(2024, 10, 1);(d2 + 100).Print();Date d3(2024, 12, 31);d3 -= 100;d3.Print();
}void TestDate3()
{Date d1(2024, 5, 5);Date d2(2050, 12, 30);cout << d1 - d2 << endl;cout << d2 - d1 << endl;
}void TestDate4()
{Date d1(2024, 5, 5);Date d2(2050, 12, 30);Date d3(1328, 1, 4);d1 = d2 = d3;
}void TestDate5()
{Date d1(2024, 5, 5);Date d2(2050, 12, 30);Date d3(1328, 1, 4);d1 += 1000;d2 -= 50;d3 += 500;cout << d1;cout << d2;cout << d3;
}void TestDate6()
{Date d1;Date d2;cin >> d2 >> d1;cout << d1 << d2;}void TestDate7()
{Date d1(1328, 1, 1);d1.Print();const Date d2(1368, 1, 4);d2.Print();}void TestDate8()
{Date d1(1328, 1, 1);const Date d2(1368, 1, 4);cout << (d1 < d2) << endl;}int main()
{TestDate8();return 0;
}

二、this指针的const修饰

// 简单的日期类
#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1368, int month = 1, int day = 4){_year = year;_month = month;_day = day;}void Print() {cout << _year << "-" << _month << "-" << _day << endl;}Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}~Date(){_year = 0;_month = 0;_day = 0;}
private:int _year;int _month;int _day;
};int main()
{Date d1(1949, 10, 1);d1.Print();const Date d2(1945, 8, 15);d2.Print();return 0;
}

上述日期类实现无法打印d2,原因如下:
使用const修饰创建类,使d2在调用Print函数时,传入的this指针是有const修饰的,与日期类定义类型冲突,所以报错。如下:
在这里插入图片描述

修改如下:

	void Print() const{cout << _year << "-" << _month << "-" << _day << endl;}

使this指针变为const修饰,需要在成员函数名后加const修饰。

在这里插入图片描述


总结

C++日期类的完整实现,以及this指针的const修饰等的介绍

相关文章:

C++日期类的完整实现,以及this指针的const修饰等的介绍

文章目录 前言一、日期类的实现二、this指针的const修饰总结 前言 C日期类的完整实现&#xff0c;以及this指针的const修饰等的介绍 一、日期类的实现 // Date.h #pragma once#include <iostream> using namespace std;#include <assert.h>class Date {// 友元函…...

缓冲区溢出

本文作者&#xff1a;杉木涂鸦智能安全实验室 前置知识点 栈 栈&#xff08;Stack&#xff09;是计算机中的一种数据结构&#xff0c;用于存储临时数据。它的特点是后入先出&#xff08;LIFO&#xff09;&#xff0c;只能在栈顶添加或删除数据。在程序中&#xff0c;栈被用于…...

step7:“模拟量界面”逻辑

文章目录 文章介绍效果图AnalogPage.qml结构图调用 SerialPortHandler.sendData(message); serialporthandler.cpp 文章介绍 之前的6步实现了案例MF的界面设计和串口界面的逻辑设计&#xff0c;本文将实现模拟量界面的逻辑设计 新增功能&#xff1a; 1&#xff09;弹出提示框 …...

Arduino - 继电器

Arduino - 继电器 In a previous tutorial, we have learned how to turn on/off an LED. In this tutorial, we are going to learn how to turn on/off some kind of devices that use the high voltage power supply(such as a light bulb, fan, electromagnetic lock, lin…...

状态压缩DP——AcWing 327. 玉米田

状态压缩DP 定义 状态压缩 DP 是一种通过二进制压缩状态的动态规划算法。它通过使用位运算来加速状态的转移和计算&#xff0c;从而提高算法的效率。 注意事项 数据范围&#xff1a;状态压缩 DP 通常适用于数据范围较小的问题&#xff0c;因为它需要使用二进制来表示状态&a…...

kafka(二)安装部署(2)windows

目录 一、前提 1、jdk 2、Zookeeper 2.1、解压 2.2、创建data文件夹 2.3、配置文件 2.4、添加环境变量 2.5、启动zk&#xff1a;zkServer 2.6、客户端 3、Scala 3.1、下载安装 3.2、配置环境变量 3.3、验证是否安装成功 二、kafka下载安装 1、下载 2、安装 2.1…...

aliplayer Server returned 403 Forbidden (access denied)

最近在接入阿里云播放器的sdk,项目的播放地址是m3u8的,h265的url 输入播放源以后播放报错,提示403,拒绝访问,起初以为是crt路径问题和key的问题,然后检查了以后没问题,后来又看了一下是不是白名单的问题,但是项目资源没通过阿里云平台存储 AVPUrlSource *source [[AVPUrlSou…...

单例模式(下)

文章目录 文章介绍步骤安排及单例讲解step1&#xff1a;注册单例类型&#xff08;main.cpp&#xff09;step2&#xff1a;定义类和私有构造函数&#xff08;keyboardinputmanager.h&#xff09;step3:&#xff08;keyboardinputmanager.cpp&#xff09;step4&#xff1a;在qml中…...

合约期VS优惠期,搞明白他们的区别才能避免很多坑!

在购买流量卡时&#xff0c;相信大家也都发现了&#xff0c;市面上的不少套餐都是有合约期和优惠期的&#xff0c;尤其是联通和移动&#xff0c;那么&#xff0c;什么是合约期&#xff1f;什么又是优惠期呢&#xff1f; ​ 其实&#xff0c;目前很多在网上办理的大流量卡都是有…...

函数式反应式编程(FRP)在Scala中的实践与探索

函数式反应式编程&#xff08;Functional Reactive Programming&#xff0c;简称FRP&#xff09;是一种编程范式&#xff0c;它结合了函数式编程&#xff08;Functional Programming&#xff0c;FP&#xff09;的声明式特性和反应式编程&#xff08;Reactive Programming&#…...

NGINX配置web文件服务

一、需求描述 系统需要提供文件&#xff08;pdf、图片&#xff09;等上传后支持预览功能。 二、实现方式 2.1 文件权限配置 chmod arwx -R public/chmod 是更改文件权限的命令。-R 是递归选项&#xff0c;表示更改目录及其所有子目录和文件的权限。arwx 是权限设置&#xf…...

deepspeed docker集群实现多机多卡训练----问题记录及解决方案资源汇总

. Docker中实现Deepspeed多机多卡训练 【掘金-雨田君的记事本】docker容器中deepspeed多机多卡集群分布式训练大模型 . 问题记录及解决方案资源汇总 问题1&#xff1a;deepspeed socketStartConnect: Connect to 172.18.0.3<54379> failed : Software caused connectio…...

恢复 IntelliJ IDEA 中消失的菜单栏

要恢复 IntelliJ IDEA 中消失的菜单栏&#xff0c;可以按照以下简单步骤操作&#xff1a; 使用快捷键打开搜索&#xff1a;首先&#xff0c;双击 Shift 键打开全局搜索对话框。 搜索“Menu”&#xff1a;在搜索框中输入 menu&#xff0c;然后从搜索结果中选择与“Main Menu”相…...

漏洞利用开发基础学习记录

文章目录 简介Win32缓冲区溢出内容难点 SEH 溢出内容难点 Egg Hunters内容难点 Unicode 溢出内容难点 x86-64 缓冲区溢出内容难点 参考资料 简介 本文基于ERC.Xdbg漏洞分析文章进行初步归纳整理&#xff0c;主要有Win32 缓冲区溢出、SEH 溢出、Egg Hunters、Unicode 溢出、x86…...

云通SIPX,您的码号资源智能调度专家!

在数字化转型的浪潮中&#xff0c;号码资源作为企业与客户沟通的重要桥梁&#xff0c;其管理效率直接关系到企业运营的成败。随着运营商对号码资源管理的规范化和精细化&#xff0c;企业对高效、智能的号码资源管理需求日益增长&#xff0c;以实现对外呼叫的降本增效。 一、什么…...

04-Mysql 索引,事务

MySQL 索引介绍 索引是一个排序的列表&#xff0c;在这个列表中存储着索引的值和包含这个值的数据所在行的物理地址。在数据十分庞大的时候&#xff0c;索引可以大大加快查询的速度。这是因为使用索引后可以不用扫描全表来定位某行的数据&#xff0c;而是先通过索引表找到该行…...

U盘提示格式化怎么搞定?本文有5种方法(内含教程)

U盘提示格式化是一种常见故障&#xff0c;即&#xff1a;当U盘插入电脑后&#xff0c;电脑上弹出对话框&#xff0c;提示该U盘需要格式化才能使用。 接触不良、文件系统损坏、热插拔、感染病毒、芯片损坏等原因都可能导致U盘出现此故障。这时点击“格式化”&#xff0c;大概率会…...

day02-登录模块-主页鉴权

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 1.分析登录流程1.1传统思路是登录校验通过之后&#xff0c;直接调用接口&#xff0c;获取token之后&#xff0c;跳转到主页1.2vue-element-admin模板的登录思路&…...

git rebase的使用

没有排版&#xff0c;但是干货 因为项目要求&#xff0c;所以使用rebase指令 我使用的是rebase 的分支变基的功能 情景描述&#xff1a; 一共有两个分支&#xff1a;master owner 我在owner分枝上开发&#xff0c;有好多次commit master上也有同事在正常commit&#xff0c; …...

LICEcap-开源GIF 屏幕录制工具

LICEcap-开源GIF 屏幕录制工具 开源GIF 屏幕录制工具 下载可以访问&#xff1a;https://www.cockos.com/licecap/ 点击Record&#xff0c;开始录制 点击Stop&#xff0c;停止录制 点击Record&#xff0c;进入该页面 display in animation&#xff08;在动画中显示&#xff09; …...

【OSG学习笔记】Day 18: 碰撞检测与物理交互

物理引擎&#xff08;Physics Engine&#xff09; 物理引擎 是一种通过计算机模拟物理规律&#xff08;如力学、碰撞、重力、流体动力学等&#xff09;的软件工具或库。 它的核心目标是在虚拟环境中逼真地模拟物体的运动和交互&#xff0c;广泛应用于 游戏开发、动画制作、虚…...

Zustand 状态管理库:极简而强大的解决方案

Zustand 是一个轻量级、快速和可扩展的状态管理库&#xff0c;特别适合 React 应用。它以简洁的 API 和高效的性能解决了 Redux 等状态管理方案中的繁琐问题。 核心优势对比 基本使用指南 1. 创建 Store // store.js import create from zustandconst useStore create((set)…...

深入理解JavaScript设计模式之单例模式

目录 什么是单例模式为什么需要单例模式常见应用场景包括 单例模式实现透明单例模式实现不透明单例模式用代理实现单例模式javaScript中的单例模式使用命名空间使用闭包封装私有变量 惰性单例通用的惰性单例 结语 什么是单例模式 单例模式&#xff08;Singleton Pattern&#…...

MODBUS TCP转CANopen 技术赋能高效协同作业

在现代工业自动化领域&#xff0c;MODBUS TCP和CANopen两种通讯协议因其稳定性和高效性被广泛应用于各种设备和系统中。而随着科技的不断进步&#xff0c;这两种通讯协议也正在被逐步融合&#xff0c;形成了一种新型的通讯方式——开疆智能MODBUS TCP转CANopen网关KJ-TCPC-CANP…...

Python 包管理器 uv 介绍

Python 包管理器 uv 全面介绍 uv 是由 Astral&#xff08;热门工具 Ruff 的开发者&#xff09;推出的下一代高性能 Python 包管理器和构建工具&#xff0c;用 Rust 编写。它旨在解决传统工具&#xff08;如 pip、virtualenv、pip-tools&#xff09;的性能瓶颈&#xff0c;同时…...

Java求职者面试指南:Spring、Spring Boot、MyBatis框架与计算机基础问题解析

Java求职者面试指南&#xff1a;Spring、Spring Boot、MyBatis框架与计算机基础问题解析 一、第一轮提问&#xff08;基础概念问题&#xff09; 1. 请解释Spring框架的核心容器是什么&#xff1f;它在Spring中起到什么作用&#xff1f; Spring框架的核心容器是IoC容器&#…...

LangChain知识库管理后端接口:数据库操作详解—— 构建本地知识库系统的基础《二》

这段 Python 代码是一个完整的 知识库数据库操作模块&#xff0c;用于对本地知识库系统中的知识库进行增删改查&#xff08;CRUD&#xff09;操作。它基于 SQLAlchemy ORM 框架 和一个自定义的装饰器 with_session 实现数据库会话管理。 &#x1f4d8; 一、整体功能概述 该模块…...

腾讯云V3签名

想要接入腾讯云的Api&#xff0c;必然先按其文档计算出所要求的签名。 之前也调用过腾讯云的接口&#xff0c;但总是卡在签名这一步&#xff0c;最后放弃选择SDK&#xff0c;这次终于自己代码实现。 可能腾讯云翻新了接口文档&#xff0c;现在阅读起来&#xff0c;清晰了很多&…...

PHP 8.5 即将发布:管道操作符、强力调试

前不久&#xff0c;PHP宣布了即将在 2025 年 11 月 20 日 正式发布的 PHP 8.5&#xff01;作为 PHP 语言的又一次重要迭代&#xff0c;PHP 8.5 承诺带来一系列旨在提升代码可读性、健壮性以及开发者效率的改进。而更令人兴奋的是&#xff0c;借助强大的本地开发环境 ServBay&am…...

消防一体化安全管控平台:构建消防“一张图”和APP统一管理

在城市的某个角落&#xff0c;一场突如其来的火灾打破了平静。熊熊烈火迅速蔓延&#xff0c;滚滚浓烟弥漫开来&#xff0c;周围群众的生命财产安全受到严重威胁。就在这千钧一发之际&#xff0c;消防救援队伍迅速行动&#xff0c;而豪越科技消防一体化安全管控平台构建的消防“…...