类与对象 中(剩余部分) 以及 日历

news/2025/3/6 2:29:12/

运算符重载

• 当运算符被⽤于类类型的对象时,C++语⾔允许我们通过运算符重载的形式指定新的含义。C++规定类类型对象使⽤运算符时,必须转换成调⽤对应运算符重载,若没有对应的运算符重载,则会编译报错。
• 运算符重载是具有特名字的函数,他的名字是由operator和后⾯要定义的运算符共同构成。和其他函数⼀样,它也具有其返回类型和参数列表以及函数体。
• 重载运算符函数的参数个数和该运算符作⽤的运算对象数量⼀样多。⼀元运算符有⼀个参数,⼆元
运算符有两个参数,⼆元运算符的左侧运算对象传给第⼀个参数,右侧运算对象传给第⼆个参数。
• 如果⼀个重载运算符函数是成员函数,则它的第⼀个运算对象默认传给隐式的this指针,因此运算符重载作为成员函数时,参数⽐运算对象少⼀个。
• 运算符重载以后,其优先级和结合性与对应的内置类型运算符保持⼀致。
• 不能通过连接语法中没有的符号来创建新的操作符:⽐如operator@。
• .* :: sizeof ?: . 注意以上5个运算符不能重载。(选择题⾥⾯常考,⼤家要记⼀下)
• 重载操作符⾄少有⼀个类类型参数,不能通过运算符重载改变内置类型对象的含义,如: int
operator+(int x, int y)
• ⼀个类需要重载哪些运算符,是看哪些运算符重载后有意义,⽐如Date类重载operator-就有意
义,但是重载operator+就没有意义。

• 重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,⽆法很好的区分。
C++规定,后置++重载时,增加⼀个int形参,跟前置++构成函数重载,⽅便区分。
• 重载<<和>>时,需要重载为全局函数,因为重载为成员函数,this指针默认抢占了第⼀个形参位
置,第⼀个形参位置是左侧运算对象,调⽤时就变成了对象<<cout,不符合使⽤习惯和可读性。
重载为全局函数把ostream/istream放到第⼀个形参位置就可以了,第⼆个形参位置当类类型对
象。

取地址运算符重载

• 将const修饰的成员函数称之为const成员函数,const修饰成员函数放到成员函数参数列表的后⾯。
• const实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进⾏修改。
const修饰Date类的Print成员函数,Print隐含的this指针由 Date* const this 变为 const Date* const this
取地址运算符重载分为普通取地址运算符重载和const取地址运算符重载,⼀般这两个函数编译器⾃动⽣成的就可以够我们⽤了,不需要去显⽰实现。除⾮⼀些很特殊的场景,⽐如我们不想让别⼈取到当前类对象的地址,就可以⾃⼰实现⼀份,胡乱返回⼀个地址

日历代码

头文件
#pragma once
#include<assert.h>
#include<iostream>
using namespace std;
class Date
{friend ostream& operator<<(ostream& out, const Date& p);friend istream& operator>>(istream& in,Date& p);
public:Date(int year, int month, int day);void Print(void);bool CheckDate(){if (_month < 1 || _month>12 || _day<1 || _day > GetMonthDay(_year, _month)){return false;}else{return true;}}int GetMonthDay(int year, int month){assert(month > 0 && month < 13);static int monthDayArray[13] = { -1,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 monthDayArray[month];}}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);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);//void operator<<(ostream& out);int operator-(const Date& d);private:int _year;int _month;int _day;
};ostream& operator<<(ostream& out,const Date& p);
istream& operator>>(istream& in,Date& p);
功能代码
#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"Date::Date(int year = 2000, int month = 1, int day = 1)
{_year = year;_month = month;_day = day;if (!CheckDate()){cout << "error" << endl;cout << *this;}
}void Date::Print()
{cout << _year << "-" << _month << "-" << _day << endl;
}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;}return false;
}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 || *this == d;
}bool Date::operator!=(const Date& d)
{return !(*this == d);
}bool Date::operator==(const Date& d)
{return _year == d._year && _month == d._month && _day == d._day;
}Date Date::operator+(int day)
{Date tmp = *this;tmp += day;return tmp;
}
Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month == 13){_year++;_month = 1;}}return (*this);
}Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){--_month;if (_month == 0){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}return *this;
}
Date Date::operator-(int day)
{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)
{Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int n = 0;while (min != max){min++;n++;}return n * flag;
}istream& operator>>(istream& in,Date& p)
{while(1){cout << "year month day";in >> p._year >> p._month >> p._day;if (p.CheckDate()){break;}else{cout << "error" << endl;}}return in;
}
ostream& operator<<(ostream& out,const Date& p)
{out << p._year << "年" <<p._month << "月" << p._day << endl;return out;
}

 


 


http://www.ppmy.cn/news/1538195.html

相关文章

自定义注解和组件扫描在Spring Boot中动态注册Bean(一)

​ 博客主页: 南来_北往 系列专栏&#xff1a;Spring Boot实战 在Spring Boot中&#xff0c;自定义注解和组件扫描是两种强大的机制&#xff0c;它们允许开发者以声明性的方式动态注册Bean。这种方式不仅提高了代码的可读性和可维护性&#xff0c;还使得Spring Boot应用的…

达梦数据库性能优化

1、SQL执行计划 拿到一条SQL的时候&#xff0c;首先要下达梦手册中提出的有效SQL规范&#xff0c;及是否命中了特殊OR子句的不规范&#xff0c;是否用了复杂的正则表达式&#xff0c;避免重复很高的索引&#xff0c;UINON ALL 是否可以替换UNION操作等,某些场景INSTR函数导致的…

vite+vue3实现动态路径导入

最近在做一个项目有个需求: 项目图片分为英语,中文,德语 ,我将这些图片存放到/image/language/下面的每个语言的文件夹内,如en,zh-cn文件夹下面存放对应的语言的图片,如果在代码里面写路径的话,除了要写一堆路径还要判断不同的语言,非常麻烦,但是在vue3vite里面import导入的是加…

多元统计实验报告内容

1 实验内容 实验目的: 利用R软件进行一些简单的数学运算,通过对简单统计量函数的操作了解R语言的基本操作过程,从而对R语言形成初步的认识。 实验项目名称: R语言软件的安装。R语言中赋值语句的练习。 在R中<-表示赋值,c()表示数组,X1<-c()即表示将一组数据赋…

十一、数据库的设计规范

文章目录 1. 为什么需要数据库设计2. 范式2.1 范式介绍2.2 范式都包括哪些2.3 键和相关属性的概念2.4 第一范式(1st NF)2.5 第二范式(2nd NF)2.6 第三范式(3rd NF)2.7 小结3. 反范式化3.1 概述3.2 应用举例3.3 反范式的新问题3.4 反范式的使用场景3.4.1 增加冗余字段的建议3.…

DELL R720服务器阵列数据恢复,磁盘状态为Foreign

服务器无法正常进入系统&#xff0c;物理磁盘状态变成了Foreign 虚拟磁盘状态变成了Failed 阵列已经丢失了&#xff0c;需要手工强制导入外部配置 单击 Main Menu 屏幕上的 Configuration Management。单击 Manage Foreign Configuration 单击 Preview Foreign Configurati…

企业级im架构详解, 分布式im 荔枝im即时消息分享 即时通讯im

1. 最近看了荔枝集团在infoQ 的im技术分享视频&#xff0c;干货满满。52im好像没有看到这个分享 2. b站链接&#xff1a; IM即时消息系统在荔枝的落地实践_哔哩哔哩_bilibili 3. infoQ 链接: IM即时消息系统在荔枝的落地实践 | InfoQ 公开课_架构_郑思宇_InfoQ精选视频 4. …

离岗睡岗预警系统 值班室离岗识别系统Python 结合 OpenCV 库

在众多工作场景中&#xff0c;存在着一些特殊岗位&#xff0c;这些岗位对于人员的专注度和警觉性有着极高的要求。然而&#xff0c;离岗睡岗现象却时有发生&#xff0c;给工作的正常开展和安全保障带来了严重的威胁。本文将深入探讨特殊岗位离岗睡岗的危害&#xff0c;以及如何…