C++【类和对象】(超详细!!!)

ops/2025/3/12 8:45:34/

C++【类和对象】

  • 1.运算符重载
  • 2.赋值运算符重载
  • 3.日期类的实现

1.运算符重载

(1).C++规定类类型运算符使用时,必须转换成调用运算符重载。
(2).运算符重载是具有特殊名字的函数,名字等于operator加需要使用的运算符,具有返回类型和参数列表及函数体。
(3).重载运算符函数的参数个数和该运算符作用的运算对象的个数保持一致,一元运算符有一个参数,二元运算符有两个参数,二元运算符左边的传给第一个参数,右边的传给第二个参数。
(4).如果一个重载运算符函数是成员函数,那么它的第一个函数参数是this指针,所以显示出来就少一个参数。
(5).运算符重载以后,其优先级和结合性与对应的内置类型运算符保持⼀致。
(6).不能通过连接语法中没有的符号来创建新的操作符:比如operator@
(7).
在这里插入图片描述这五个运算符不能重载。
(8).C++为了区分前置++和后置++运算符重载函数,规定前置为operator++(),后置++为operator++(int)

#include<iostream>
using namespace std;
class Date
{
public:Date(int year, int month, int day){_year = year;_month = month;_day = day;}bool operator == (const Date& d){return _year == d._year&& _month == d._month&& _day == d._day;}//d1-d2
int	operator-(const Date& d);
//日期-天数
Date operator-(int day);
private:int _year;int _month;int _day;};
int main()
{Date d1(2024, 3, 20);Date d2(2025, 3, 18);if (d1 == d2){cout << "相等" << endl;}elsecout << "不相等" << endl;return 0;
}

2.赋值运算符重载

赋值运算符重载是一个默认成员函数(不能重载到全局),用于完成两个已经存在的对象直接的拷贝赋值。
赋值运算符重载的特点:
(1).赋值运算符重载是一个函数重载,规定重载必须为成员函数,赋值运算重载的参数建议写成const 当前类类型引用,否则会传值传参会有拷贝。
(2).有返回值,且建议写成当前类类型引⽤,引⽤返回可以提高效率,有返回值目的是为了支持连续赋值场景。
(3).没有显式实现时,编译器会自动生成⼀个默认赋值运算符重载,默认赋值运算符重载行为跟默认拷贝构造函数类似,对内置类型成员变量会完成值拷贝/浅拷贝(⼀个字节⼀个字节的拷贝),对自定义类型成员变量会调用他的赋值重载函数。
(4).如果一个类显示实现析构并释放了资源,就需要我们显示实现赋值运算符重载,否则就不需要。

#include<iostream>
using namespace std;
class Date
{
public:Date(int year, int month, int day){_year = year;_month = month;_day = day;}Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}Date& operator=(const Date& d){if (this != &d){_year = d._year;_month =d._month;_day = d._day;}return*this;}void Print(){cout << _year << "-" << _month << "-" << _day << endl;}
private:int _year;int _month;int _day;
};
int main()
{Date d1(2004,8,18);Date d2(d1);d1.Print();d2.Print();return 0;
}

3.日期类的实现

//Date.h
#include<iostream>
using namespace std;
class Date
{
public:Date(int year, int month, int day){_year = year;_month = month;_day = day;}int GetMonthDay(int year,int month){int monthDayArray[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;}elsereturn monthDayArray[month];}Date& operator+=(int day);Date operator+(int day);Date& operator-=(int day);Date operator-(int day);// ++d1 ->d1.operator++()Date& operator++();// d1++ ->d1.operator++(1);Date operator++(int);Date& operator--();Date operator--(int);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;
};
//Date.cpp
#include"Date.h";
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;
}// d1 + 100
Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}void Date::Print()
{cout << _year << "-" << _month << "-" << _day << endl;
}// ++d1
Date& Date::operator++()
{*this += 1;return *this;
}// d1++
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}// d1 < d2
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;}else{return false;}
}bool Date::operator==(const Date& d)
{return _year == d._year&& _month == d._month&& _day == d._day;
}// d1 <= d2
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);
}
//test.cpp
#include"Date.h"void TestDate1()
{Date d1(2025, 3, 9);d1.Print();Date d2 = d1 + 100;d2.Print();d1.Print();Date d3 = d1 += 100;d1.Print();d3.Print();
}void TestDate2()
{Date d1(2025, 3, 9);Date ret1 = ++d1;//Date ret1 = d1.operator++();d1.Print();ret1.Print();Date d2(2025, 3, 9);Date ret2 = d2++;//Date ret2 = d2.operator++(10000);d2.Print();ret2.Print();
}int main()
{TestDate2();return 0;
}

在这里插入图片描述


http://www.ppmy.cn/ops/165158.html

相关文章

Redis常用数据结构及命令详解:从基础到进阶

Redis作为一款高性能的键值存储系统&#xff0c;凭借其丰富的数据结构和灵活的用法&#xff0c;被广泛应用于缓存、队列、计数器等场景。Redis提供了多种数据结构&#xff0c;每种结构都有其独特的特性和适用场景。本文将详细介绍Redis的几种常用数据结构&#xff0c;并指出使用…

基于USB Key的Web系统双因素认证解决方案:构建安全与便捷的登录体系

摘要 在网络安全威胁日益严峻的背景下&#xff0c;传统的“用户名密码”认证方式已难以应对钓鱼攻击、密码窃取等风险。上海安当基于USB Key技术&#xff0c;推出了一套面向Web系统的双因素认证解决方案&#xff0c;通过硬件与密码学的深度融合&#xff0c;实现用户身份的高强度…

【每日八股】计算机网络篇(四):HTTP

目录 HTTP 与 HTTPS 的区别&#xff1f;HTTPS 加密与认证的过程&#xff1f;ClientHelloServerHello客户端回应服务端回应 HTTPS 一定安全可靠吗&#xff1f;HTTPS 状态码的含义&#xff1f;HTTP 缓存有哪些实现方式&#xff1f;HTTP 1.0、HTTP 1.1、HTTP 2.0 和 HTTP 3.0 的区…

MyBatis的级联查询(一对一、一对多、多对多)

MyBatis的级联查询 级联的优点是获取关联数据十分便捷。但是级联过多会增加系统的复杂度&#xff0c;同时降低系统的性能&#xff0c;此增彼减。所以记录超过 3 层时&#xff0c;就不要考虑使用级联了&#xff0c;因为这样会造成多个对象的关联&#xff0c;导致系统的耦合、负…

numpy常用函数详解

在深度神经网络代码中经常用到numpy库的一些函数&#xff0c;很多看过之后很容易忘记&#xff0c;本文对经常使用的函数进行归纳总结。 np.arange arange是numpy一个常用的函数&#xff0c;该函数主要用于创建等差数列。它的使用方法如下所示&#xff1a; numpy.arange([star…

llama.cpp编译

llam.cpp编译 1. 下载&编译 git clone https://github.com/ggml-org/llama.cpp cmake -S . -B build2. 下载模型验证 # 下载地址 https://huggingface.co/filipealmeida/open-llama-7b-v2-open-instruct-GGUF/blob/main/ggml-model-Q4_0.gguf# 验证 ./llama-cli.exe -m …

Python第十六课:深度学习入门 | 神经网络解密

🎯 本节目标 理解生物神经元与人工神经网络的映射关系掌握激活函数与损失函数的核心作用使用Keras构建手写数字识别模型可视化神经网络的训练过程掌握防止过拟合的基础策略一、神经网络基础(大脑的数字化仿生) 1. 神经元对比 生物神经元人工神经元树突接收信号输入层接收特…

使用DeepSeek+蓝耘快速设计网页简易版《我的世界》小游戏

前言&#xff1a;如今&#xff0c;借助先进的人工智能模型与便捷的云平台&#xff0c;即便是新手开发者&#xff0c;也能开启创意游戏的设计之旅。DeepSeek 作为前沿的人工智能模型&#xff0c;具备强大的功能与潜力&#xff0c;而蓝耘智算云平台则为其提供了稳定高效的运行环境…