【C++类和对象】类有哪些默认成员函数呢?(下)

文章目录

  • 一、类的6个默认成员函数
  • 二、日期类的实现
    • 2.1 运算符重载部分
    • 2.2 日期之间的运算
    • 2.3 整体代码
      • 1.Date.h部分
      • 2. Date.cpp部分
  • 三. const成员函数
  • 四. 取地址及const取地址操作符重载
    • 扩展内容
  • 总结


ヾ(๑╹◡╹)ノ" 人总要为过去的懒惰而付出代价ヾ(๑╹◡╹)ノ"


一、类的6个默认成员函数

如果一个类中什么成员都没有,简称为空类。

空类中并不是什么都没有,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数。
​​​​​​​​在这里插入图片描述
默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数

二、日期类的实现

2.1 运算符重载部分

  • inline不支持声明与定义分离,所以成员函数要成为inline,最好直接在类里面定义
  • 类里面定义成员函数的默认就是inline。代码较长的部分采用声明与定义分离的方法

1. 运算符重载部分1
此部分在Date.h类里面部分

//运算符重载部分1
	bool operator==(const Date& d);
	bool operator<(const Date& d);
	
	bool operator>(const Date& d)
	{
		return !(*this <= d);
	}
	bool operator>=(const Date& d)
	{
		return !(*this < d);
	}
	bool operator!=(const Date& d)
	{
		return !(*this == d);
	}
	//思路:把小于的情况写出来,剩下的就是大于的情况
	bool operator<=(const Date& d)
	{
		return *this < d || *this == d;//this是一个指针,指向类的指针
		//小于和等于都已经实现过了,所以就可以复用
	}
	//运算符重载部分,【+-】
	Date operator+(int day) const;
	Date& operator+=(int day);
	Date operator-(int day) const;
	Date& operator-=(int day);
  • 运算符重载部分【比较大小】,能复用其他函数的时候,尽可能的去复用
  • 此部分写一个小于等于,那么别的函数就都可以复用
  • 内联函数声明定义不能分离(符号表里没有函数的地址,所以使用的时候找不到函数定义),所以这部分代码,如果想设置成inline函数,就直接放到类里面。内联函数是在调用的地方,被展开,以空间换取时间的方法
  • 类里面的函数默认是内联函数

2. 运算符重载部分2
此部分在Date.cpp部分

//运算符重载部分2,==和<部分,
bool Date::operator==(const Date& d)//访问的本质上是:this->_year == d._year(一个是指针访问,一个是类直接访问)……
	//_year并不是类里面的_year(仅仅是一个声明),而是比较的另一个类
{
	if (_year == d._year
		&& _month == d._month
		&& _day == d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool Date::operator<(const Date& d)//要加上Date::(在类外面写)
{
	if (_year < d._year
		|| (_year == d._year && _month < d._month)
		|| (_year == d._year && _month == d._month && _day < d._day))
	{
		return true;
	}
	else
	{
		return false;
	}
}
//运算符重载
//d1+2=d2;因为此处运算符是+,d1是不变的,所以,不能直接对d1进行改动
Date Date::operator+(int day) const//这里的返回值不能用Date&,因为出了函数,空间就被回收了
{
	//+复用+=
	Date ret(*this);//拷贝构造
	ret += day;
	return ret;
	//Date ret(*this);//拷贝构造
	//ret._day += day;
	//while (ret._day > GetMonthDay(ret._year, ret._month))
	//{
	//	ret._day = ret._day - GetMonthDay(ret._year, ret._month);
	//	ret._month++;
	//	while (ret._month > 13)
	//	{
	//		ret._month = 1;
	//		ret._year++;
	//	}
	//}
	//return ret;
}


Date& Date::operator+=(int day)//因为出了作用域this还在,所以用引用比较好【在能用引用的情况下,尽可能去用引用】
{
	//+=复用+
	//*this = *this + day;
	//return *this;
	if (day < 0)
	{
		return *this -= -day;
	}
	_day += day;
	while (_day > GetMonthDay(_year, _day))
	{
		_day -= GetMonthDay(_year, _day);
		_month++;
		if (_month = 13)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}

Date Date::operator-(int day) const
{
	Date ret(*this);
	ret -= day;
	return ret;
}
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}
    _day -= day;
	while (_day <= 0)//日期是不能有0日的
	{
		_month--;
		while (_month == 0)
		{
			_month = 12;
			_year--;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
  • 运算法重载部分【比较大小】
    因为代码较长,不适合用inline函数,所以使用声明与定义的方法【==和<的代码更加容易实现】
  • 运算符重载部分【+、+=、-、-=】
  • d1=10,注意:这里是+和-,并不是+=和-=,所以d1是不变的,不要对d1直接进行改动。【利用拷贝构造】
  • +和+=之间和-和-=之间可以相互复用【写整块代码的时候,代码之间能相互复用,就相互复用【即简单又很大程度上保证了代码的正确性】】【+复用+=更优】
  • 加减法注意:这里的day要求必须是正数,否则减去一个负数,相当于加上一个正数;加上一个负数,相当于减去一个正数(相互复用即可)【因为-复用-=,所以-的函数就不用考虑负数问题】

补充知识点(1):
Date d1(2000, 9, 17); Date d2 = d1;//这里是拷贝构造,不是赋值【因为一个已经创建的对象去初始化另一个对象就是拷贝构造】
【赋值是两个已经创建好的对象进行初始化】
补充知识点(2):
写赋值重载函数和拷贝构造函数时,函数的形参要加const,因为既可以防止修改值,又可以防止当形参是函数的返回值【具有const属性】,会权限扩大,导致错误【无法修改的右值】。


2.2 日期之间的运算

前置++后置++,前置–,后置–【区分++是前置还是后置的,函数重载】
C++用无参数的代表前置,有一个参数的代表后置【参数只要是int类型的就符合要求,无论是整形的哪一个数字】
1. 日期之间的运算1
此部分在Date.h类里面部分
Date.h

//日期之间的运算
	Date& operator++()
	{
		*this += 1;
		return *this;
	}

	Date operator++(int)
	{
		Date tmp(*this);
		tmp += 1;
		return tmp;
	}

	Date& operator--()
	{
		*this -= 1;
		return *this;
	}

	Date operator--(int)
	{
		Date tmp(*this);
		tmp -= 1;
		return tmp;
	}

	//日期-日期
	int operator-(const Date& d) const;

2. 日期之间的运算2
此部分在Date.cpp里面部分
Date.c

//*this和d不确定哪一个日期大
int Date::operator-(const Date& d) const
{
	int day = 0;
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	while (min != max)
	{
		min++;
		day++;
	}
	day = day * flag;
	return day;
}

2.3 整体代码

1.Date.h部分

#pragma once

#include <iostream>
#include <assert.h>
using std::cin;
using std::cout;//在正式的项目中不要全展开
using std::endl;

class Date
{
public:
	//拷贝构造函数部分
	//判断是否是闰年
	bool isLeapYear(int year)
	{
		if (year % 4 == 0 && year % 100 != 0
			&& year % 400 == 0)
		{
			return true;
		}
		return false;
	}
	//每一月的天数
	int GetMonthDay(int year, int month);
	//构造函数(也可以不用写)//可能会有用户输入非法日期
	Date(int year = 1, int month = 1, int day = 1);
	//拷贝构造函数(也可以不用写),值拷贝
	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;
	}
	
	//不需要写析构函数,没有资源清理


	//打印Date,在函数后面加一个const,相当于void print(const Date* const d),const对象和普通对象都可以调用此函数
	//如果不加const,相当于void print(Date* const d),
	//只要不修改this,就可以加上
	void print() const
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	//运算符重载部分,【+-】
	Date operator+(int day) const;
	Date& operator+=(int day);
	Date operator-(int day) const;
	Date& operator-=(int day);
	
	//运算符重载部分1【比较大小】
	bool operator==(const Date& d) const;
	bool operator<(const Date& d) const;

	bool operator>(const Date& d) const
	{
		return !(*this <= d);
	}
	bool operator>=(const Date& d) const
	{
		return !(*this < d);
	}
	bool operator!=(const Date& d) const
	{
		return !(*this == d);
	}
	//思路:把小于的情况写出来,剩下的就是大于的情况
	bool operator<=(const Date& d) const
	{
		return *this < d || *this == d;//this是一个指针,指向类的指针
		//小于和等于都已经实现过了,所以就可以复用
	}

	//日期之间的运算
	Date& operator++()
	{
		*this += 1;
		return *this;
	}

	Date operator++(int)
	{
		Date tmp(*this);
		tmp += 1;
		return tmp;
	}

	Date& operator--()
	{
		*this -= 1;
		return *this;
	}

	Date operator--(int)
	{
		Date tmp(*this);
		tmp -= 1;
		return tmp;
	}

	//日期-日期
	int operator-(const Date& d) const;

private:
	int _year;
	int _month;
	int _day;
};

2. Date.cpp部分

#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"

//构造函数部分
Date::Date(int year = 1, int month = 1, int day = 1)
{
	if (year >= 1
		&& (month >= 1 && month <= 12)
		&& (day >= 1 && day <= GetMonthDay(year, month)))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "日期非法" << endl;
	}
}
int Date::GetMonthDay(int year, int month)
{
	assert(year > 0 && month >= 1 && month < 13);
	static int monthDayArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	//因为这部分空间需要频繁开辟,所以就用static,就可以节约时间
	if (month == 2 && isLeapYear(year))
	{
		return 29;
	}
	else
	{
		return monthDayArray[month];
	}
}


//运算符重载部分2,
bool Date::operator==(const Date& d) const//访问的本质上是:this->_year == d._year(一个是指针访问,一个是类直接访问)……
	//_year并不是类里面的_year(仅仅是一个声明),而是比较的另一个类
{
	if (_year == d._year
		&& _month == d._month
		&& _day == d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool Date::operator<(const Date& d) const//要加上Date::(在类外面写)
{
	if (_year < d._year
		|| (_year == d._year && _month < d._month)
		|| (_year == d._year && _month == d._month && _day < d._day))
	{
		return true;
	}
	else
	{
		return false;
	}
}

//运算符重载
//d1+2=d2;因为此处运算符是+,d1是不变的,所以,不能直接对d1进行改动
Date Date::operator+(int day) const//这里的返回值不能用Date&,因为出了函数,空间就被回收了
{
	//+复用+=
	Date ret(*this);//拷贝构造
	ret += day;
	return ret;
	//Date ret(*this);//拷贝构造
	//ret._day += day;
	//while (ret._day > GetMonthDay(ret._year, ret._month))
	//{
	//	ret._day = ret._day - GetMonthDay(ret._year, ret._month);
	//	ret._month++;
	//	while (ret._month > 13)
	//	{
	//		ret._month = 1;
	//		ret._year++;
	//	}
	//}
	//return ret;
}


Date& Date::operator+=(int day)//因为出了作用域this还在,所以用引用比较好【在能用引用的情况下,尽可能去用引用】
{
	//+=复用+
	//*this = *this + day;
	//return *this;
	if (day < 0)
	{
		return *this -= -day;
	}
	_day += day;
	while (_day > GetMonthDay(_year, _day))
	{
		_day -= GetMonthDay(_year, _day);
		_month++;
		if (_month = 13)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}

Date Date::operator-(int day) const
{
	Date ret(*this);
	ret -= day;
	return ret;
}
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}
    _day -= day;
	while (_day <= 0)//日期是不能有0日的
	{
		_month--;
		while (_month == 0)
		{
			_month = 12;
			_year--;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

//*this和d不确定哪一个日期大
int Date::operator-(const Date& d) const
{
	int day = 0;
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	while (min != max)
	{
		min++;
		day++;
	}
	day = day * flag;
	return day;
}

三. const成员函数

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

	void print() const
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
  • 打印Date,在函数后面加一个const,相当于void print(const Date* const d),const对象和普通对象都可以调用此函数
  • 如果不加const,相当于void print(Date* const d),
  • 只要不修改this,就可以加上const
  • (d+13).print();会报错,因为d+13是传值返回,传值返回的是临时拷贝,临时拷贝具有常性。【在print没有加const的情况下】

建议成员函数中,不修改成员变量的成员函数,都可以加上const。【普通对象和const对象都可以调用】 不加const,那么const修饰的对象就没有办法调用成员函数

1. const对象可以调用非const成员函数吗?不可以,权限放大不可以
2. 非const对象可以调用const成员函数吗?可以,权限缩小可以
3. const成员函数内可以调用其它的非const成员函数吗?不可以,权限放大
4. 非const成员函数内可以调用其它的const成员函数吗?可以,权限缩小

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

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

	Date* operator&()
	{
		return this;
	}//取地址重载

	const Date* operator&() const
	{
		return this;
	}//const取地址重载

这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如想让别人获取到指定的内容【不想要输出地址,而是别的内容】!【此时才需要我们自己写】


扩展内容

流插入运算符和流提取运算符重载
d1.operator<<(cout);//test函数中写,可以运行
cout<<d1;//不可以运行
void operator<<(std::ostream out)
{
out << _year<< “- “<< _month<<” -” << _day<<endl;
}
Date.h 类里面

//友元函数
	friend std::ostream& operator<<(std::ostream& out, const Date& d);
	friend std::istream& operator>>(std::istream& in, Date& d);

Date.cpp

std::ostream& operator<<(std::ostream& out, const Date& d)
{
	out << d._year << "-" << d._month << "-" << d._day << endl;
	return out;
}
std::istream& operator>>(std::istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}
  • 内置类型直接支持流插入和流提取【且自动识别类型,因为函数重载】
  • 全局变量函数不要放在.h里面,因为在别的文件展开的时候,会出现多个符号表里,导致重定义。【只有声明的时候,才会链接的时候去找函数】

总结

以上就是今天的所有内容了,类的默认成员函数就到此结束了。本文以及【C++类和对象】类有哪些默认成员函数呢?(上)】详细的介绍了类的默认成员函数,希望给友友们带来帮助!

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

ffmepg滤镜

代码实现&#xff1a; 1.get_format() 这个是QSV硬件解码时的回调函数&#xff0c;在这里初始化hw_frames_ctx, get_format会在解码时被调用。因此对滤镜的初始化init_filter()应在得到第一帧数据后调用。 2.hw_frames_ctx&#xff0c;需要按照要求把他们传给对应的filter 3.然…

向量数据库 Milvus Cloud Partition Key:租户数量多,单个租户数据少的三种解决方案

三种解决方案 这个问题提出的时候,Milvus 的最新版本是 2.2.8,我们做个角色互换,在当时站在这个用户的角度,留在我们面前的选择有这么几个: 为每个租户创建一个 collection 为每个租户创建一个 partition 创建一个租户名称的标量字段 接下来,我们依次分析下这三种方案的可…

OPPO A57刷机资源(附简略教程)

https://www.123pan.com/s/hcAqVv-fpHWd.html提取码:buAp 图中画框的为必须下载的&#xff08;xiaomiflash和twrp必须解压后使用&#xff09; ​ 打开xiaomiflash点击driver点击install&#xff08;就是框住的按钮&#xff09;等待安装完成 ​用数据线将oppo a57与电脑连接&a…

【数学建模】逻辑回归算法(Logistic Resgression)

逻辑回归算法 简介逻辑回归与条件概率绘制sigmoid函数 简介 逻辑回归算法是一种简单但功能强大的二元线性分类算法。需要注意的是&#xff0c;尽管"逻辑回归"名字带有“回归”二字&#xff0c;但逻辑回归是一个分类算法&#xff0c;而不是回归算法。 我认为&#xff…

用easyui DataGrid编辑树形资料

easyui显示编辑树形资料有TreeGrid元件&#xff0c;但是这个元件的vue版本和react版本没有分页功能。virtual scroll功能也表现不佳。 我用DataGrid来处理。要解决的问题点&#xff1a; &#xff08;1&#xff09;如何显示成树形。即&#xff0c;子节点如何有缩进。 先计算好…

一、进入sql环境,以及sql的查询、新建、删除、使用

1、进入sql环境 》》》mysql -u root -p 》》》输入密码 2、sql语言的分类 3、注意事项&#xff1a; 4、基础操作&#xff1a; &#xff08;1&#xff09;查询所有数据库&#xff1a; show databases; 运行结果&#xff1a; &#xff08;2&#xff09;创建一个新的数据库&…

网络通信原理传输层TCP三次建立连接(第四十八课)

ACK :确认号 。 是期望收到对方的下一个报文段的数据的第1个字节的序号,即上次已成功接收到的数据字节序号加1。只有ACK标识为1,此字段有效。确认号X+1SEQ:序号字段。 TCP链接中传输的数据流中每个字节都编上一个序号。序号字段的值指的是本报文段所发送的数据的第一个字节的…

MongoDB 简介

什么是MongoDB ? MongoDB 是由C语言编写的&#xff0c;是一个基于分布式文件存储的开源数据库系统。 在高负载的情况下&#xff0c;添加更多的节点&#xff0c;可以保证服务器性能。 MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB 将数据存储为一个…

从SaaS到RPA,没有真正“完美”的解决方案!

众所周知&#xff0c;SaaS行业越来越卷&#xff0c;利润也越来越“薄”&#xff0c;这是传统软件厂商的悲哀&#xff0c;也是未来数字化行业不得不面对的冷峻现状之一。 随着基于aPaaS、低代码的解决方案之流行&#xff0c;SaaS行业变得越来越没有技术门槛&#xff0c;IT人员的…

win10安装mysql和c++读取调用举例

一、下载mysql8.rar解压到C盘(也可以解压到其他位置) 在系统环境变量添加JAVA_HOMEC:\myslq8&#xff0c;并在path中添加%JAVA_HOME%\bin; 二、以管理员身份进入命令窗口 三、修改配置文件指定安装路径和数据库的存放路径 四、键入如下命令初始化并启动mysql服务,然后修改登录…

使用qsqlmysql操作mysql提示Driver not loaded

环境: win10 IDE: qt creator 编译器: mingw32 这里简单的记录下。我遇到的情况是在IDE使用debug和release程序都是运行正常&#xff0c;但是当我编译成发布版本之后。老是提示Driver not load。 这就很奇诡了。 回顾了下编译的时候是需要在使用qt先编译下libqsqlmysql.dll的…

Android中如何不编译源生模块

如果想让自己的app 替换系统的app 比如使用闪电浏览器替换系统的Browser 首先把闪电浏览器放到 vendor/rockchip/common/apps Android.mk LOCAL_PATH : $(call my-dir) include $(CLEAR_VARS)LOCAL_MODULE : Lightning LOCAL_SRC_FILES : $(LOCAL_MODULE).apk LOCAL_MODULE_C…

Eclipse使用插件时提示Plugin Error loading shared libraries

项目场景: 使用Eclipse的过程中,依赖openCONFIGURATOR插件进行新建项目时,弹出如下的错误: Plugin Error loading shared libraries 以及具体的信息为: Can’t find dependent libraries 这里我使用的插件为openCONFIGURATOR插件 问题描述 如上场景,提示缺少动态链接库…

实战篇之基于二进制思想的用户标签系统(Mysql+SpringBoot)

一&#xff1a; 计算机中的二进制 计算机以二进制表示数据&#xff0c;以表示电路中的正反。在二进制下&#xff0c;一个位只有 0 和 1 。逢二进一 位。类似十进制下&#xff0c;一个位只有 0~9 。逢十进一位。 二&#xff1a; 进制常用运算 &#xff08;位运算&#xff09;…

Django框架 靓号管理(增删改查)

Django框架 靓号管理&#xff08;增删改查&#xff09; 新建一个项目 backend 使用pycharm创建app startapp app项目目录 C:\code\backend ├── app | ├── admin.py | ├── apps.py | ├── migrations | ├── models.py | ├── tests.py | ├── views.…

【设计模式】抽象工厂模式

抽象工厂模式&#xff08;Abstract Factory Pattern&#xff09;是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式&#xff0c;它提供了一种创建对象的最佳方式。 在抽象工厂模式中&#xff0c;接口是负责创建一个相关对象…

Jenkins改造—nginx配置鉴权

先kill掉8082的端口进程 netstat -natp | grep 8082 kill 10256 1、下载nginx nginx安装 EPEL 仓库中有 Nginx 的安装包。如果你还没有安装过 EPEL&#xff0c;可以通过运行下面的命令来完成安装 sudo yum install epel-release 输入以下命令来安装 Nginx sudo yum inst…

神经网络基础-神经网络补充概念-14-逻辑回归中损失函数的解释

概念 逻辑回归损失函数是用来衡量逻辑回归模型预测与实际观测之间差异的函数。它的目标是找到一组模型参数&#xff0c;使得预测结果尽可能接近实际观测。 理解 在逻辑回归中&#xff0c;常用的损失函数是对数似然损失&#xff08;Log-Likelihood Loss&#xff09;&#xff…

1.物联网LWIP网络,TCP/IP协议簇

一。TCP/IP协议簇 1.应用层&#xff1a;FTP&#xff0c;HTTP&#xff0c;Telent&#xff0c;DNS&#xff0c;RIP 2.传输层&#xff1a;TCP&#xff0c;UDP 3.网络层&#xff1a;IPV4&#xff0c;IPV6&#xff0c;OSPF&#xff0c;EIGRP 4.数据链路层&#xff1a;Ethernet&#…

Syncfusion Essential Studio JavaScrip Crack

Syncfusion Essential Studio JavaScrip Crack 数据透视表 添加了在将数据透视表导出到PDF文档时自定义列宽的支持。 签名 添加了对在特定位置绘制文本的支持。 Syncfusion Essential Studio for JavaScript在一个包中包含80多个高性能、轻量级、模块化和响应式UI组件。包括Jav…