当前位置: 首页 > news >正文

【C++】踏上C++学习之旅(九):深入“类和对象“世界,掌握编程的黄金法则(四)(包含四大默认成员函数的练习以及const对象)

文章目录

  • 前言
  • 1. 实现Date类的构造函数
  • 2. 实现Date类的拷贝构造函数
  • 3. 实现Date类的赋值运算符重载
  • 4. 实现各Date对象之间的比较接口
  • 5. 实现Date对象的加减接口
  • 6. const成员
  • 7. 取地址及const取地址操作符重载

前言

在我们前面学习到了"类和对象"的四大默认成员函数(构造函数、析构函数、拷贝构造函数、赋值运算符重载),这四大默认成员函数也是我们在以后使用"类和对象"这块知识时经常遇到的。本章将会围绕着如何实现一个Date类,来让大家尽快学会编写和更加深刻理解关于"类"封装的思想在实际当中的应用!

本文会分板块逐一讲解,在文章的末尾放有本次实现Date类的全部源码。

哈哈哈

1. 实现Date类的构造函数

所谓的 “Date” 翻译过来就是 “日期” 的意思,那它的成员变量一定是年月日。那我们就可以这么实现Date类的构造函数。

class Date
{
public://全缺省的默认构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}private:int _year; //年int _month; //月int _day; //日
};

这里我们写成全缺省的默认构造函数是十分有讲究的,一方面当我们先使用这个缺省值时,我们就直接有类实例化出对象就行,不需要显式的传递参数;另一方面,当我们想用自己的传递的参数,就直接显式传递函数即可。一举两得。

2. 实现Date类的拷贝构造函数

class Date
{
public://全缺省的默认构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}//拷贝构造函数Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}private:int _year;int _month;int _day;
};

这里对于拷贝构造函数的思路没有太多的讲解,只需要大家注意一个点就是,形参一定是对于类类型的引用,避免引发无穷的递归调用。

3. 实现Date类的赋值运算符重载

class Date
{
public://全缺省的默认构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}//拷贝构造函数Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}//赋值运算符重载Date& operator=(const Date& d){_year = d._year;_month = d._month;_day = d._day;return *this;}private:int _year;int _month;int _day;
};

关于赋值运算符重载的返回值,一定是*this,而不是this。因为我们是要返回这个待赋值对象的引用,而this是这个待赋值对象的指针类型为Date* const。还有的人会考虑生命周期的问题,其实这里大可不必担心,虽然this指针的生命周期在这个成员函数中,出了这个作用域就会被销毁,但是我们返回的不是this而是*this*this就是那个待拷贝的对象,所以其的生命周期是在main函数中的!

4. 实现各Date对象之间的比较接口

本次的是实现主要涉及到大小之间的比较,目的是锻炼大家对于运算符重载的用法。

//年份之间的比较大小
class Date
{
public://全缺省的默认构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}//拷贝构造函数Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}//年份之间的比较大小bool 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 operator==(const Date& d){return _year == d._year && _month == d._month && _day == d._day;}bool operator!=(const Date& d){return !(*this == d);}bool operator>=(const Date& d){return (*this == d) || (*this > d);}bool operator<=(const Date& d){return (*this == d) || (*this < d);}bool operator<(const Date& d){return !(*this >= d);}//赋值运算符重载Date& operator=(const Date& d){_year = d._year;_month = d._month;_day = d._day;return *this;}private:int _year;int _month;int _day;
};

这里不仅体现了运算符重载,还体现出了代码复用的重要性!

5. 实现Date对象的加减接口

这里Date类对象的加减是指:年份与年份之间的相减,年份与天数之间的相减,年份与天数的相加。

class Date
{
public://全缺省的默认构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}//拷贝构造函数Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}Date operator+(int day) const{Date tmp = *this;tmp._day += day;//我们还要考虑一下天数相加会导致的 月进位 甚至是 年进位//所以我们得获取每个月的天数while (tmp._day > tmp.GetMonthDay()){tmp._day -= tmp.GetMonthDay();tmp._month++;if (tmp._month > 12){tmp._year++;tmp._month = 1;}}return tmp;}Date& operator+=(int day){_day += day;while (_day > GetMonthDay()){_day -= GetMonthDay();++_month;if (_month > 12){++_year;_month = 1;}}return *this;}Date& operator-=(int day){_day -= day;while (_day <= 0){--_month;if (_month < 1){_month = 12;--_year;}_day += GetMonthDay();}return *this;}//赋值运算符重载Date& operator=(const Date& d){_year = d._year;_month = d._month;_day = d._day;return *this;}Date& operator++(){_day += 1;while (_day > GetMonthDay()){_day -= GetMonthDay();++_month;if (_month > 12){++_year;_month = 1;}}return *this;}Date operator++(int){Date tmp = *this;_day += 1;while (_day > GetMonthDay()){_day -= GetMonthDay();++_month;if (_month > 12){++_year;_month = 1;}}return tmp;}private:int GetMonthDay(){int month_day[] = { 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 month_day[_month];}private:int _year;int _month;int _day;
};

这个可以很好的锻炼大家对于前置++和后置++的写法。

到这里Date类我们就全部实现完毕了,是不是很简单呢。一下就是Date类实现全部的源码:

class Date
{
public://全缺省的默认构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}//拷贝构造函数Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}Date operator+(int day) const{Date tmp = *this;tmp._day += day;//我们还要考虑一下天数相加会导致的 月进位 甚至是 年进位//所以我们得获取每个月的天数while (tmp._day > tmp.GetMonthDay()){tmp._day -= tmp.GetMonthDay();tmp._month++;if (tmp._month > 12){tmp._year++;tmp._month = 1;}}return tmp;}//年份之间的比较大小bool 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 operator==(const Date& d){return _year == d._year && _month == d._month && _day == d._day;}bool operator!=(const Date& d){return !(*this == d);}bool operator>=(const Date& d){return (*this == d) || (*this > d);}bool operator<=(const Date& d){return (*this == d) || (*this < d);}bool operator<(const Date& d){return !(*this >= d);}//赋值运算符重载Date& operator=(const Date& d){_year = d._year;_month = d._month;_day = d._day;return *this;}Date& operator+=(int day){_day += day;while (_day > GetMonthDay()){_day -= GetMonthDay();++_month;if (_month > 12){++_year;_month = 1;}}return *this;}Date& operator++(){_day += 1;while (_day > GetMonthDay()){_day -= GetMonthDay();++_month;if (_month > 12){++_year;_month = 1;}}return *this;}Date operator++(int){Date tmp = *this;_day += 1;while (_day > GetMonthDay()){_day -= GetMonthDay();++_month;if (_month > 12){++_year;_month = 1;}}return tmp;}Date& operator-=(int day){_day -= day;while (_day <= 0){--_month;if (_month < 1){_month = 12;--_year;}_day += GetMonthDay();}return *this;}//赋值运算符重载Date& operator=(const Date& d){_year = d._year;_month = d._month;_day = d._day;return *this;}private:int GetMonthDay(){int month_day[] = { 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 month_day[_month];}private:int _year;int _month;int _day;
};

6. const成员

将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改

函数

#include<iostream>
using namespace std;class Date
{
public:Date(int year, int month, int day){_year = year;_month = month;_day = day;}void Print(){cout << "Print()" << endl;cout << "year:" << _year << endl;cout << "month:" << _month << endl;cout << "day:" << _day << endl << endl;}void Print() const{cout << "Print()const" << endl;cout << "year:" << _year << endl;cout << "month:" << _month << endl;cout << "day:" << _day << endl << endl;}
private:int _year; // 年int _month; // 月int _day; // 日
};
void Test()
{Date d1(2022, 1, 13);d1.Print();const Date d2(2022, 1, 13);d2.Print();
}int main()
{Test();return 0;
}

图片

请思考下面的几个问题:

  1. const对象可以调用非const成员函数吗?

不可以,因为权限被放大了。

  1. 非const对象可以调用const成员函数吗?

可以,因为权限被缩小了。

  1. const成员函数内可以调用其它的非const成员函数吗?

不可以,因为权限被放大了

  1. 非const成员函数内可以调用其它的const成员函数吗?

可以,因为权限被缩小了。

那这里我们就可以根据这个const成员进一步优化我们的Date类的实现!

7. 取地址及const取地址操作符重载

这两个默认成员函数一般不用重新定义 ,编译器默认会生成

class Date
{
public:Date* operator&(){return this;}const Date* operator&() const{return this;}private:int _year;int _month;int _day;
};

这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如不想让别人获取到指定的内容的地址
我们就可以改造上面的函数:

class Date
{
public:Date* operator&(){return nullptr;}const Date* operator&() const{return nullptr;}private:int _year;int _month;int _day;
};

好了本文到这里就结束了。

如果觉得本文写的还不错的话,麻烦给偶点个赞吧!!!

哈哈哈

http://www.lryc.cn/news/488047.html

相关文章:

  • C++——智能指针剖析
  • 241119.LeetCode——383.赎金信
  • 基于SSM的农家乐管理系统+论文示例参考
  • 用 Python 从零开始创建神经网络(九):反向传播(Backpropagation)(还在更新中。。。)
  • Flink是如何实现 End-To-End Exactly-once的?
  • 【vulhub】nginx解析漏洞(nginx_parsing_vulnerability)
  • 网络协议之邮件协议(SMTP、POP3与IMAP)
  • python学习笔记(3)运算符
  • _FYAW智能显示控制仪表的简单使用_串口通信
  • 激光雷达定位初始化的另外一个方案 通过键盘按键移动当前位姿 (附python代码)
  • 从0-1逐步搭建一个前端脚手架工具并发布到npm
  • 河道水位流量一体化自动监测系统:航运安全的护航使者
  • 维护在线重做日志
  • ASCB1系列APP操控末端回路智能微断 物联网断路器 远程控制开关 学校、工厂、农场、商业大楼等可用
  • Python入门(10)--面向对象进阶
  • Makefile 之 自动化变量
  • 鸿蒙开发:ForEach中为什么键值生成函数很重要
  • 沃丰科技智能外呼机器人:超越人工,重塑外呼体验
  • 百度飞浆:paddle 线性回归模型
  • 【JavaSE】【网络编程】UDP数据报套接字编程
  • 45.坑王驾到第九期:Mac安装typescript后tsc命令无效的问题
  • 20241120-Milvus向量数据库快速体验
  • 【Golang】——Gin 框架中间件详解:从基础到实战
  • 量子计算来袭:如何保护未来的数字世界
  • VMware虚拟机(Ubuntu或centOS)共享宿主机网络资源
  • 光伏电站仿真系统的作用
  • Golang文件操作
  • 爬虫开发工具与环境搭建——使用Postman和浏览器开发者工具
  • React(二)
  • 同步原语(Synchronization Primitives)