C++初阶:类和对象(四)运算符重载与日期类Date的实现

        在本节,我将通过实现日期类Date的实现来进一步阐释运算符重载的内容。

目录

一、Date.h

二、Date.cpp

三、test.cpp


一、Date.h

#include<iostream>
#include<cassert>
using namespace std;
class Date
{
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);// <运算符重载bool operator<(const Date& d);// <=运算符重载bool operator<=(const Date& d);// !=运算符重载bool operator!=(const Date& d);// 日期-日期  返回天数int operator-(const Date& d);//展示void Show(){cout << _year << "年" << _month << "月" << _day << "日" << endl;}
private:int _year;int _month;int _day;
};

二、Date.cpp

#include "Date.h"
// 全缺省的构造函数
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()
{cout << "窝被调用了" << endl;
}
// 日期+=天数
// 在该日期的基础上进行修改,需要返回该对象的引用,提高效率
Date& Date::operator+=(int day)
{assert(day >= 0);while (day + _day > GetMonthDay(_year, _month)){day -= GetMonthDay(_year, _month);_month++;if (_month == 12){_month = 1;_year++;}}_day += day;return *this;
}
// 日期+天数
// 不在该日期上修改,设置一个temp对象,对temp对象进行操作可以复用+=操作
Date Date::operator+(int day)
{assert(day >= 0);Date temp = *this;//原始写法/*while (day + _day > GetMonthDay(_year, _month)){day -= GetMonthDay(_year, temp._month);temp._month++;if (temp._month == 12){temp._month = 1;temp._year++;}}temp._day += day;*///实现了+=之后的写法temp += 60;return temp;
}
// 日期-天数
// 复用-=,减少代码量
Date Date::operator-(int day)
{assert(day>=0);Date temp=*this;//原始写法/* while(temp._day<day){day -= GetMonthDay(temp._year, temp._month);if(temp._month==1){temp._month=12;--temp._year;continue;}--temp._month;}temp._day -= day;*///实现了-=之后的写法temp -= day;return temp;
}
// 日期-=天数
// 有了+=的经验之后,可以先实现-=的操作,注意月份的转变
Date& Date::operator-=(int day)
{assert(day >= 0);while (_day < day){day -= GetMonthDay(_year, _month);if (_month == 1){_month = 12;--_year;continue;}--_month;}_day -= day;return *this;
}
// 前置++
// 前置++的效果是对对象自增后进行返回原对象的引用,得到的是自增后的对象
Date& Date::operator++()
{if (_day < GetMonthDay(_year, _month)){_day++;}else{_year= _month == 12 ? _year+1 : _year;_month = _month == 12 ? 1 : _month + 1;_day = 1;}return *this;
}
// 后置++
// 后置++的效果是对对象进行自增,然后返回自增前的对象。创建一个temp对象,对原对象进行自增后
// 返回temp对象的内容
Date Date::operator++(int)
{Date temp = *this;++*this;return temp;
}
// 后置--
// 同后置++
Date Date::operator--(int)
{Date temp = *this;--*this;return temp;
}
// 前置--
// 同前置++
Date& Date::operator--()
{if (_day == 1){if (_month == 1){_year--;_month = 12;}else{_month--;}_day = GetMonthDay(_year, _month);}else{_day--;}return *this;
}
// >运算符重载
// 依次比较,然后返回值
bool Date::operator>(const Date& d)
{if (_year > d._year){return true;}else if(_year==d._year){if (_month > d._month){return true;}else if (_month < d._month){return false;}else{if (_day > d._day){return true;}else{return false;}}}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 || *this == d;
}
// !=运算符重载
// ==的反面就是!=
bool Date::operator!=(const Date& d)
{return !(_year == d._year && _month == d._month && _day == d._day);
}
// 日期-日期  返回天数
// 日期追加的思想,让小日期追赶大日期,并且使计数器++
int Date::operator-(const Date& d)
{assert(d._year >= 0);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;
}

三、test.cpp

#include"Date.h"
int main()
{Date d1;Date d2(2024, 3, 11);/*d1 = d2;Date d3 = d2 + 60;*//* d2 += 60;d2.Show();*//*d3.Show();d2 = d2 - 90;d2.Show();*//*++d2;d2.Show();*//*d1=d2++;d1.Show();*/d1 = d2--;//d1++;d1.Show();d2.Show();(++d2).Show();Date d4(2025, 2, 24);cout << (d1 <= d2) << endl;cout << (d4 - d2) << endl;return 0;
}

        下节预告:类和对象初阶收尾

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

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

相关文章

seo js转码工具

js转码工具作用 用于把js加密 如果不想让别人看到自己的js 代码就可以使用这个方法 js工具网址 https://tool.chinaz.com/js.aspx 效果

【大厂AI课学习笔记NO.74】人工智能产业技术架构

包括基础层、技术层和应用层。 人工智能的产业技术架构是一个多层次、多维度的复杂系统&#xff0c;它涵盖了从基础硬件和软件设施到高级算法和应用技术的全过程。这个架构通常可以分为三个主要层次&#xff1a;基础层、技术层和应用层。下面我将详细论述这三个层次及其细分内…

基于Yolo5模型的动态口罩佩戴识别安卓Android程序设计

禁止完全抄袭&#xff0c;引用注明出处。 下载地址 前排提醒&#xff1a;文件还没过CSDN审核&#xff0c;GitHub也没上传完毕&#xff0c;目前只有模型的.pt文件可以下载。我会尽快更新。 所使用.ptl文件 基于Yolo5的动态口罩佩戴识别模型的pt文件资源-CSDN文库 项目完整文…

ES6基础4

Set 基本用法 ES6提供了新的数据结构Set。它类似于数组&#xff0c;但是成员的值都是唯一的&#xff0c;没有重复的值。Set本身是一个构造函数&#xff0c;用来生成Set数据结构。 // 例一 var set new Set([1, 2, 3, 4, 4]); [...set] // [1, 2, 3, 4]// 例二 var items new …

升级ChatGPT4.0失败的解决方案

ChatGPT 4.0科普 ChatGPT 4.0是一款具有多项出众功能的新一代AI语言模型。以下是关于ChatGPT 4.0的一些关键特点和科普内容&#xff1a; 多模态&#xff1a;ChatGPT 4.0具备处理不同类型输入和输出的能力。这意味着它不仅可以接收文字信息&#xff0c;还能处理图片、视频等多…

CNN中常见的池化操作有哪些,作用是什么?

CNN中常见的池化操作有哪些&#xff0c;作用是什么&#xff1f; CNN中常见的池化操作只要是两种&#xff0c;平均值池化和最大值池化最大值池化常用于分类任务&#xff0c;是指在输入数据的局部区域内取最大值作为输出。最大池化的作用是降低特征图的尺寸&#xff0c;减少参数…

C++17中auto作为非类型模板参数

非类型模板参数是具有固定类型的模板参数&#xff0c;用作作为模板参数传入的constexpr值的占位符。非类型模板参数可以是以下类型&#xff1a; (1).整型&#xff1b; (2).枚举类型&#xff1b; (3).std::nullptr_t&#xff1b; (4).指向对象的指针或引…

vscode中解决驱动编写的时候static int __init chrdev_init()报错的问题

目录 错误出错原因解决方法 错误 在入口函数上&#xff0c;出现 expected a ; 这样的提示 出错原因 缺少了 __KERNEL __ 宏定义 解决方法 补上__KERNEL__宏定义 具体做法&#xff1a;在vscode中按下ctrlshiftp &#xff0c;输入&#xff1a;C/C:Edit Configurations&#xff0…

AcWing 4956. 冶炼金属

对于这个题&#xff0c;V越大&#xff0c;除出来的数就越小&#xff0c;V越小&#xff0c;除出来的数就越大&#xff0c;当我们找一个最大和最小值的时候&#xff0c;就可以通过这个性质进行二分来求解。 可以通过求满足 [ A V ] [\frac{A}{V}] [VA​] 小于等于 B B B的最小的…

手把手教使用静默 搭建Oracle 19c 一主一备ADG集群

一、环境搭建 主机IPora19192.168.134.239ora19std192.168.134.240 1.配置yum源 1.配置网络yum源 1.删除redhat7.0系统自带的yum软件包&#xff1b; rpm -qa|grep yum >oldyum.pkg 备份原信息rpm -qa|grep yum|xargs rpm -e --nodeps 不检查依赖&#xff0c;直接删除…

EE5437-IOT(Lecture 07-Control Interface System)

Review&#xff1a; introduce the micro input device system&#xff08;MIDS&#xff09; • The calibration and testing has been covered • The introduction to filters with the example called Butterworth filter and the maths have been also demonstrated. …

鸿蒙Harmony应用开发—ArkTS声明式开发(基础手势:CalendarPicker)

日历选择器组件&#xff0c;提供下拉日历弹窗&#xff0c;可以让用户选择日期。 说明&#xff1a; 该组件从API Version 10开始支持。后续版本如有新增内容&#xff0c;则采用上角标单独标记该内容的起始版本。 子组件 无 接口 CalendarPicker(options?: CalendarOptions) …

跨境账号养号怎么做?Facebook、亚马逊运营必看

之前我们讨论过很多关于代理器的问题。它们的工作原理是什么?在不同的软件中要使用那些代理服务器?这些代理服务器之间的区别是什么?什么是反检测浏览器等等。 除了这些问题&#xff0c;相信很多人也会关心在使用不同平台的时代理器的选择问题。比如&#xff0c;为什么最好…

Mybatis操作sql报错ibatis.binding.BindingException: Parameter ‘empId‘ not found.

你们好&#xff0c;我是金金金。 场景 在使用Mybatis操作sql语句过程当中&#xff0c;更新操作&#xff0c;报错信息如下&#xff1a;Caused by: org.apache.ibatis.binding.BindingException: Parameter ‘empId’ not found. Available parameters are [arg1, arg0, param1, …

m序列生成器

function [m] mserial_generator(tap_set) % m序列产生器 % 输出为m序列&#xff0c;未进行极性变换。 L 2^(length(tap_set)-1)-1; x [zeros(1,(length(tap_set)-2)) 1]; for i 1:1:Lm(i)x(end);for j 1:1:length(tap_set)-1sum_vector(j)tap_set(j1)*x(j);endsum_x mod…

聊聊python中面向对象编程思想

面向对象编程思想 1、什么是面向过程 传统的面向过程的编程思想总结起来就八个字——自顶向下&#xff0c;逐步细化&#xff01; → 将要实现的功能描述为一个从开始到结束按部就班的连续的“步骤” → 依次逐步完成这些步骤&#xff0c;如果某一个步骤的难度较大&#xff…

2024暑期实习八股笔记

文章目录 自我介绍MySQL索引索引种类、B树聚簇索引、非聚簇索引联合索引、最左前缀匹配原则索引下推索引失效索引优化 日志、缓冲池redo log&#xff08;重做日志&#xff09;刷盘时机日志文件组 bin log&#xff08;归档日志&#xff09;记录格式写入机制 两阶段提交undo log&…

20240309web前端_第一周作业_古诗词

作业三&#xff1a;古诗词 成果展示&#xff1a; 完整代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0&q…

FLatten Transformer_ Vision Transformer using Focused Linear Attention

paper: https://arxiv.org/abs/2308.00442 code: https://github.com/LeapLabTHU/FLatten-Transformer 摘要 当将transformer模型应用于视觉任务时&#xff0c;自注意的二次计算复杂度( n 2 n^2 n2)一直是一个持续存在的挑战。另一方面&#xff0c;线性注意通过精心设计的映射…

事务的特性,API、隔离级别、传播行为

一、事务 1、事务的介绍 事务就是用户定义的一系列执行SQL语句的操作, 这些操作要么完全地执行&#xff0c;要么完全地都不执行&#xff0c; 它是一个不可分割的工作执行单元。 2、事务的使用场景 例如在日常生活中&#xff0c;有时我们需要进行银行转账&#xff0c;这个银行转…