C++【类和对象】(取地址运算符重载与实现Date类)

devtools/2024/9/29 23:55:27/

文章目录

  • 取地址运算符重载
    • const成员函数
    • 取地址运算符重载
  • Date类的实现
    • Date.h
    • Date.cpp
      • 1.检查日期合法性
      • 2. 构造函数/赋值运算符重载
      • 3.得到某月的天数
      • 4. Date类 +- 天数的操作
        • 4.1 日期 += 天数
        • 4.2 日期 + 天数
        • 4.3 日期 -= 天数
        • 4.4 日期 - 天数
      • 5. Date的前后置++/--
        • 5.1 前置++
        • 5.2 后置++
        • 5.3 前置--
        • 5.4 后置--
      • 6. Date类之间的比较
        • 6.1 < 操作符
        • 6.2 ==操作符
        • 6.3 <=操作符
        • 6.4 >操作符
        • 6.5 >=操作符
        • 6.6 !=操作符
      • 7.日期-日期
      • 8. Date的流插入与流提取
        • 8.1流插入函数
        • 8.2 流提取函数
    • Test.cpp
  • 结语

取地址运算符重载

const成员函数

  • 将const修饰的成员函数称之为const成员函数,const修饰成员函数放到成员函数参数列表的后面。
  • const实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。const修饰Date类的Print成员函数,Print隐含的this指针由Date* const this变成 const Date* const this

还是拿Date类举例,如果我用const 修饰的对象来调用Print会发生报错,原因是Print隐含的this指针是Date* const this,当我用const对象来调用Print会发生权限放大。
在这里插入图片描述
但是用const修饰成员函数,就可以解决问题,因为用const修饰后Print隐含的this指针由Date* const this变成 const Date* const this,就不会出现权限放大。

#include<iostream>
using namespace std;
class Date
{
public:Date(int year = 1, int month = 1, int day = 1){//cout << "这是构造函数" << endl;_year = year;_month = month;_day = day;}//void Print(const Date* const this)void Print() const{cout << _year << '/' << _month << '/' << _day << endl;}private:int _year;int _month;int _day;
};

在这里插入图片描述

取地址运算符重载

取地址运算符重载分为普通取地址运算符重载和const取地址运算符重载,⼀般这两个函数编译器自动生成的就可以够我们用了,不需要去显示实现。除非一些很特殊的场景,比如我们不想让别⼈取到当前类对象的地址,就可以自己实现⼀份,胡乱返回⼀个地址。

    Date* operator&(){//return nullptr;(胡乱给的地址)return this;}const Date* operator&() const{//return nullptr;(胡乱给的地址)return this;}

Date类的实现

我们在实现一个任务的时候,可以让声明与定义分离,这是一个好习惯。

Date.h

#pragma once
#include<iostream>
using namespace std;
class Date
{
public://1.检查日期的合法性bool CheckDate();//2.构造函数与赋值运算符重载Date(int year = 1, int month = 1, int day = 1);Date& operator=(const Date& d);bool LeapYear(int year){if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){return true;}return false;}//3.得到某月天数int GetMonthDay(int year, int month){static int month_day[] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (month == 2 && LeapYear(year)){return month_day[month] + 1;}return month_day[month];}//4. Date类 +- 天数的操作Date operator+(const int day);Date& operator+=(const int day);Date operator-(const int day);Date& operator-=(const int day);//5. Date的前后置++/--Date& operator++();Date operator++(int);Date& operator--();Date operator--(int);//6. Date类之间的比较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);void Print();//7. 日期-日期(算相差多少天)int operator-(const Date& d);//8. 日期的流插入与流提取friend ostream& operator<<(ostream& out, Date& d);friend istream& operator>>(istream& in, Date& d);private:int _year;int _month;int _day;
};

Date.cpp

实现前要包头文件

#include"Date.h"

1.检查日期合法性

	//检擦日期合法性bool Date:: CheckDate(){if (_month <= 0 || _month >= 13 || _day > GetMonthDay(_year, _month) || _day <= 0){return false;}return true;}

2. 构造函数/赋值运算符重载

构造函数

    Date::Date(int year, int month, int day){//cout << "这是构造函数" << endl;_year = year;_month = month;_day = day;while (!CheckDate()){cout << "非法日期" << endl;cout << *this;cout << "请重新输入日期" << endl;cin >> *this;}}

其实我们是不需要写拷贝构造和赋值运算符重载,因为Date类内部的成员对象并没有指向资源。
但是我们写赋值运算符重载是为了实现连续赋值。

    Date& Date::operator=(const Date&d){//如果不是自己给自己赋值if (this != &d){_year = d._year;_month = d._month;_day = d._day;}//d1 = d2 应该返回前者(因为前者是被改变的) 所以要返回*thisreturn *this;}

3.得到某月的天数

因为GetMonthDay这个函数会被频繁大量的调用,所以我们放在类里面定义(类里面的函数默认inline展开)

	//判断闰年(其实这个函数没必要写,但我为了美观就写了该函数)bool LeapYear(int year){if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){return true;}return false;}//得到某月的天数int GetMonthDay(int year, int month){//将month_day静态化,这样就不用反复的创建数组static int month_day[] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (month == 2 && LeapYear(year)){return month_day[month] + 1;}return month_day[month];}

4. Date类 ± 天数的操作

加减的是天数

4.1 日期 += 天数

加等使用的是进位思想,当这个月满了,向下个月借天数(同时月份也会+1)当月份大于12时,就将年份+1,同时月份到一月,直到日期为合法。

    Date& Date::operator+=(const int day){if (day < 0){return *this -= (-day);}_day += day;while (_day > GetMonthDay(_year,_month)){int tmp = GetMonthDay(_year, _month);_day -= tmp;++_month;if (_month > 12){_month = 1;++_year;}}return *this;}
4.2 日期 + 天数

这里我们可以直接复用 日期+=天数 !

    Date Date::operator+(const int day){Date tmp(*this);tmp += day;return tmp;}
4.3 日期 -= 天数

-=与+=的实现十分类似,只不过-=使用的是退位的思想,向前一个月借天数(月份-1),当月份小于1,年份-1,月份到12月,直到日期合法

    Date& Date:: operator-=(const int day){if (day < 0){return *this += (-day);}_day -= day;while (_day <= 0){--_month;if (_month == 0){_month = 12;--_year;}int tmp = GetMonthDay(_year, _month);_day += tmp;}
4.4 日期 - 天数

和+一样,复用-=

    Date Date::operator-(const int day){Date tmp = *this;tmp -= day;return tmp;}

5. Date的前后置++/–

前置不需要加入int形参,后置需要加入int形参。

5.1 前置++

复用+=

    Date& Date::operator++(){*this += 1;return *this;}
5.2 后置++

在参数位置上加一个int形参

    Date Date::operator++(int){Date tmp = *this;*this += 1;return tmp;}
5.3 前置–
    Date& Date::operator--(){*this -= 1;return *this;}
5.4 后置–
    Date Date::operator--(int){Date tmp = *this;*this -= 1;return tmp;}

6. Date类之间的比较

完成 < + == 或者 > + ==其他的比较就可以复用了。
本文实现的是 < + ==

6.1 < 操作符
    bool Date:: operator<(const Date& d){if (_year > d._year){return false;}else if (_year == d._year){if (_month >= d._month){return false;}else if (_month == d._month){if (_day >= d._day){return false;}}}return true;}
6.2 ==操作符
   bool Date::operator==(const Date& d){return _year == d._year &&_month == d._month &&_day == d._day;}
6.3 <=操作符
   bool Date::operator<=(const Date& d){return (*this < d) || (*this == d);}
6.4 >操作符
    bool Date::operator>(const Date& d){   return !(*this <= d);}
6.5 >=操作符
    bool Date::operator>=(const Date& d){return !(*this < d);}
6.6 !=操作符
    bool Date::operator!=(const Date& d){return !(*this == d);}

7.日期-日期

我们使用最简单的思路,取到区分较大和较小的日期,让较小的日期+1加到与较大的日期相等,同时记加了多少次,就得到了日期之间的天数。(cup的运行速度超级快,每秒上亿次,所以这点运算量对于cup来说轻轻松松)

    //Date-Dateint Date::operator-(const Date& d){int n = 0;int flag = 1;//假设法Date min = *this;Date max = d;if (min > max){min = d;max = *this;flag = -1;}//计算天数while (min != max){++min;++n;}return n * flag;}

8. Date的流插入与流提取

8.1流插入函数
ostream& operator<<(ostream& out, Date& d)
{cout << d._year << '/' << d._month << '/' << d._day << endl;return out;
}

注意,这里流插入和流提取都是全局函数,而非类的成员函数,其实我们在类里面也可以实现,但是会很怪。

由于成员函数的第一个参数默认是this,但运算符重载的规定是,当两个变量使用重载的运算符,会将左边的变量视为第一个参数,右边的变量视为第二个参数;这样的话,我们就不能写成cout << Date类实例化的对象,而是要写成Date类实例化的对象 << coutcin同理。

但是这样并不符合我们的习惯,所以我们设置成了全局函数,但是全局函数又有一个问题,就是Date类内部的成员变量是私有的,外部不能访问。
对此我们有以下几个方法:

  1. 将成员变量公有化(最最最不推荐的方法)
  2. 用get函数来得到成员变量
  3. 使用友元函数
  4. 放到类的内部(因参数问题不考虑)

这里我们使用的是友元函数(后面会详细讲解)。

8.2 流提取函数
istream& operator>>(istream& in, Date& d)
{cin >> d._year >> d._month >> d._day;while (!d.CheckDate()){cout << "非法日期" << endl;cout << d;cout << "请重新输入日期" << endl;cin >> d;}return in;
}

Test.cpp

#include"Date.h"void Test1()
{Date d1;Date d2(2026, 2, 28);//Date d3(2029, 9, 33);Date d3(2029, 9, 30);d1.Print();d2.Print();d3.Print();d3 = d2 = d1;d1.Print();d2.Print();d3.Print();
}void Test2()
{Date d1(2026, 2, 28);d1 += 10000;d1.Print();d1 -= 10000;d1.Print();Date d2 = d1 + 10000;d2.Print();Date d3 = d1 - 10000;d3.Print();
}void Test3()
{Date d1(2026, 2, 28);Date d2 = ++d1;d2.Print();Date d3 = d1++;d3.Print();    Date d4 = --d1;d4.Print();   Date d5 = d1--;d5.Print();
}
void Test4()
{Date d1(2024, 9, 28);Date d2(2026, 2, 28);cout << (d1 == d2) << endl;cout << (d1 != d2) << endl;cout << (d1 < d2) << endl;cout << (d1 <= d2) << endl;cout << (d1 > d2) << endl;cout << (d1 >= d2) << endl;
}void Test5()
{Date d1(2024, 9, 28);Date d2(2026, 2, 28);cout << d1 - d2 << endl;
}void Test6()
{Date d1;cin >> d1;cout << d1;
}int main()
{//Test1();//Test2();//Test3();//Test4();//Test5();Test6();return 0;
}

结语

这次的分享就到这里结束了~
最后感谢您能阅读完此片文章~
如果您认为这篇文章对你有帮助的话,可以用你们的手点一个免费的赞并收藏起来哟~
如果有任何建议或纠正欢迎在评论区留言~
也可以前往我的主页看更多好文哦(点击此处跳转到主页)。


http://www.ppmy.cn/devtools/118955.html

相关文章

【鸿蒙HarmonyOS NEXT】数据存储之分布式键值数据库

【鸿蒙HarmonyOS NEXT】数据存储之分布式键值数据库 一、环境说明二、分布式键值数据库介绍三、示例代码加以说明四、小结 一、环境说明 DevEco Studio 版本&#xff1a; API版本&#xff1a;以12为主 二、分布式键值数据库介绍 KVStore简介&#xff1a; 分布式键值数据库…

图为科技大模型一体机,智领未来社区服务

当AI与边缘计算相遇&#xff0c;一幅关于智慧生活的宏伟蓝图正缓缓展开。 今天&#xff0c;让我们一同探索&#xff0c;如何通过图为大模型一体机&#xff0c;为物业服务插上智能的翅膀。 通过整合采集物业数据&#xff0c;大模型一体机可全方位为物业行业赋能&#xff0c;实…

docker - maven 插件自动构建镜像(构建镜像:ebuy-docker:v2.0)

文章目录 1、docker服务端开启远程访问2、在pom.xml文件plugins下添加Maven的docker插件3、编写dockerfile文件4、执行maven的打包命令5、查看 镜像 ebuy-docker:v2.06、创建 容器 ebuy-dockerv2.0 上面手动构建镜像的过程比较繁琐&#xff0c;使用Maven的docker插件可以实现镜…

Java 之 ssm框架入门

SSM框架作为Java Web开发的热门选择&#xff0c;其强大功能和易用性吸引了众多开发者。以下是我对该框架的理解以及学习建议&#xff0c;仅供参考 一、 SSM框架深度解析 1. Spring 核心技术 IoC (控制反转) 概念: 将对象的创建和管理权利交给Spring容器&#xff0c;通过依赖注…

OpenCV图像文件读写(2) 检查 OpenCV 是否支持某种图像格式的写入功能函数haveImageWriter()的使用

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 haveImageWriter 函数用于检查 OpenCV 是否支持某种图像格式的写入功能。这个函数可以帮助开发者在编写代码时确定是否可以成功地将图像写入特定…

【Java】内部类【主线学习笔记】

文章目录 前言内部类内部类的使用举例内部类的分类对于成员内部类的理解 前言 Java是一门功能强大且广泛应用的编程语言&#xff0c;具有跨平台性和高效的执行速度&#xff0c;广受开发者喜爱。在接下来的学习过程中&#xff0c;我将记录学习过程中的基础语法、框架和实践技巧等…

React入门准备

React是什么 React是一个用于构建用户界面的JavaScript框架&#xff0c;用于构建“可预期的”和“声明式的”Web用户界面&#xff0c;特别适合于构建那些数据会随时间改变的大型应用的用户界面。 它起源于Facebook的内部项目&#xff0c;因为对市场上所有JavaScript MVC框架都…

Android—ANR日志分析

获取ANR日志&#xff1a; ANR路径&#xff1a;/data/anrADB指令&#xff1a;adb bugreport D:\bugrep.zip ANR日志分析步骤&#xff1a; “main” prio&#xff1a;主线程状态beginning of crash&#xff1a;搜索 crash 相关信息CPU usage from&#xff1a;搜索 cpu 使用信息…