【C++】学习笔记——类和对象_4

文章目录

  • 二、类和对象
    • 13.运算符重载
      • 赋值运算符重载
    • 14. 日期类的实现
      • Date.h头文件
      • Date.cpp源文件
      • test.cpp源文件
  • 未完待续


二、类和对象

13.运算符重载

赋值运算符重载

我们之前学了一个拷贝构造函数,本质上就是创建一个对象,该对象初始化为一个已经存在的对象的数据。

// 拷贝构造
Date d1(d2);

而赋值运算符重载则是重载一个赋值运算符 “=” ,然后让两个已经存在的对象,一个拷贝赋值给另一个。

// 赋值运算符重载
d1 = d2;

普通赋值运算符是支持连续赋值的,所以我们重载后的也需要连续赋值,即函数需要返回左值。
赋值运算符的实现:

#include<iostream>
using namespace std;class Date
{
public:Date(int year = 1111, int month = 1, int day = 1){_year = year;_month = month;_day = day;}~Date() {}void Print(){cout << _year << "-" << _month << "-" << _day << endl;}// 拷贝构造函数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;}
private:int _year;int _month;int _day;
};int main()
{Date d1(2222, 2, 2);Date d2(d1);d1 = d2;return 0;
}

赋值运算符重载函数也是6个默认的成员函数之一,所以我们不写编译器也会自动生成。它跟拷贝构造函数很相似,对内置类型进行浅拷贝处理,对自定义类型去调用它的赋值运算符重载函数。由于也是浅拷贝,所以涉及到堆上的空间开辟时,不能使用编译器自动生成的赋值运算符重载函数。

14. 日期类的实现

这个日期类的实现将会将之前学的所有知识进行融合。我们来进行标准的声明和定义分离。

Date.h头文件

头文件里只包括声明

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;class Date
{
public:// 构造函数的声明Date(int year = 1111, int month = 1, int day = 1);// 重载运算符的声明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);// ++d1Date& operator++();// d1++  为了跟前置++区分,后置++强行增加了一个int形参,构成重载区分Date operator++(int);Date& operator--();Date operator--(int);// 日期减日期int operator-(const Date& d);int GetMonthDay(int year, int month){// 断言月份错误assert(month >= 1 && month <= 12);// 静态变量只初始化一次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];}// 打印日期的声明void Print();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;
}// 重载运算符的定义
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 _day < d._day;}}return false;
}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 _year == d._year&& _month == d._month&& _day == d._day;
}bool Date::operator!=(const Date& d)
{return !(*this == d);
}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)
{// 拷贝构造,避免修改原来的日期Date tmp(*this);// 使用重载后的+=tmp += day;// 局部对象不能引用返回return tmp;
}Date Date::operator-(int day)
{// tmp是刚创建的对象,所以这是拷贝构造而不是赋值Date tmp = *this;tmp -= day;return tmp;
}Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}// ++d1
Date& Date::operator++()
{*this += 1;return *this;
}// d1++
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)
{int flag = 1;Date max = *this;Date min = d;if (*this < d){flag = -1;max = d;min = *this;}int n = 0;while (max != min){++min;++n;}return flag * n;
}// 打印日期的定义
void Date::Print()
{cout << _year << "-" << _month << "-" << _day << endl;
}

test.cpp源文件

#include"Date.h"int main()
{Date d1(2222, 2, 2);d1.Print();Date d2 = d1 + 10;d2.Print();cout << (d1 > d2) << endl;Date d3;d3.Print();d3 += 10000;d3.Print();--d3;d3.Print();cout << (d1 - d3) << endl;return 0;
}

结果:
在这里插入图片描述


未完待续

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

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

相关文章

291个地级市资源错配指数、劳动和资本相对扭曲指数(2006-2021年)

01、数据介绍 资源错配指数&#xff08;Misallocation Index&#xff09;是一个用于衡量资源配置效率的指标&#xff0c;它衡量的是生产要素的配置是否合理&#xff0c;是否达到了最优的状态。资源错配指数越高&#xff0c;资源的利用效率越低。资源错配指数主要用于衡量各种生…

医学影像增强:空间域方法与频域方法等

医学影像图像增强是一项关键技术,旨在改善图像质量,以便更好地进行疾病诊断和评估。增强方法通常分为两大类:空间域方法和频域方法。 一、 空间域方法 空间域方法涉及直接对医学影像的像素值进行操作,以提高图像的视觉质量。以下是一些常用的空间域方法: 对比度调整:通过…

Excel 公式的定义、语法和应用(LOOKUP 函数、HLOOKUP 函数、VLOOKUP 函数;MODE.MULT 函数; ROUND 函数)

一、公式的定义和语法 二、公式的应用 附录 查找Excel公式使用方法的官方工具【强烈推荐!!!】:Excel 函数(按字母顺序)【微软官网】 excel 函数说明语法LOOKUP 函数在向量或数组中查找值LOOKUP(lookup_value, lookup_vector, [result_vector])

YoloV8改进策略:注意力改进|Neck改进|自研全新的Mamba注意力|即插即用,简单易懂:附结构图|检测、分割、关键点均适用(独家原创,全世界首发)

摘要 无Mamba不狂欢,本文打造基于Mamba的注意力机制。全世界首发基于Mamba的注意力啊!对Mamba感兴趣的朋友一定不要错过啊! 本文使用Mamba改进YoloV8的Block和BackBone实现涨点。 环境 系统:ubuntu22.04 CUDA:12.1 python:3.11 显卡驱动:545 安装过程 系统、CUDA和…

JAVA面向对象(下)(四、内部类、枚举、包装类)

一、内部类&#xff08;续&#xff09; 1.1 内部类的分类 按照声明的位置划分&#xff0c;可以把内部类分为两大类&#xff1a; 成员内部类&#xff1a;方法外 局部内部类&#xff1a;方法内 public class 外部类名{【修饰符】 class 成员内部类名{ //方法外}【修饰符】 返…

15.Nacos服务分级存储模型

服务跨集群调用问题&#xff1a; 服务调用尽可能的选择本地集群的服务&#xff0c;跨集群调用延迟较高。 本地集群不可访问的情况下&#xff0c;再去访问其他集群。 如何配置集群的实例属性&#xff1a; spring: cloud:nacos:server-addr: localhost:8848 #nacos服务端地址d…

数据可视化(八):Pandas时间序列——动态绘图,重采样,自相关图,偏相关图等高级操作

Tips&#xff1a;"分享是快乐的源泉&#x1f4a7;&#xff0c;在我的博客里&#xff0c;不仅有知识的海洋&#x1f30a;&#xff0c;还有满满的正能量加持&#x1f4aa;&#xff0c;快来和我一起分享这份快乐吧&#x1f60a;&#xff01; 喜欢我的博客的话&#xff0c;记得…

五一档电影市场前瞻:题海战术,能否冲击新的票房记录?

五一假期&#xff0c;电影市场又将上演一场“厮杀”。 截止到发稿&#xff0c;已经有11部电影定档五一假期&#xff0c;涵盖动作、悬疑、喜剧、动画等多个题材类型&#xff0c;显得格外活跃&#xff0c;也让市场对五一档多了一些期待。 在电影市场持续回暖的大环境之下&#…

Linux中grep详解

一、grep基本介绍 全拼:Global search REgular expression and Print out the line. 从grep的全称中可以了解到&#xff0c;grep是一个可以利用”正则表达式”进行”全局搜索”的工具&#xff0c;grep会在文本文件中按照指定的正则进行全局搜索&#xff0c;并将搜索出的行打印出…

Linux cmake 初窥【1】

1.开发背景 linux 下编译程序需要用到对应的 Makefile&#xff0c;用于编译应用程序&#xff0c;但是 Makefile 的语法过于繁杂&#xff0c;甚至有些反人类&#xff0c;所以这里引用了cmake&#xff0c;cmake 其中一个主要功能就是用于生成 Makefile&#xff0c;cmake 的语法更…

虚拟局域网PPTP配置与验证

虚拟局域网PPTP配置与验证 前言PPTP服务侧安装配置REF 前言 虚拟专用网&#xff08;Virtual Private Network&#xff0c;VPN&#xff09;是一种通过公共网络建立安全的连接的技术。它能够在不同的地理位置之间建立私密的通信通道&#xff0c;实现远程访问网络资源的安全性和隐…

牛客NC233 加起来和为目标值的组合(四)【中等 DFS C++、Java、Go、PHP】

题目 题目链接&#xff1a; https://www.nowcoder.com/practice/7a64b6a6cf2e4e88a0a73af0a967a82b 解法 dfs参考答案C class Solution {public:/*** 代码中的类名、方法名、参数名已经指定&#xff0c;请勿修改&#xff0c;直接返回方法规定的值即可*** param nums int整型…