c++阶梯之类与对象(中)< 续集 >

前文:

c++阶梯之类与对象(上)-CSDN博客

c++阶梯之类与对象(中)-CSDN博客


前言:

在上文中,我们学习了类的六个默认成员函数之构造,析构与拷贝构造函数,接下来我们来看看剩下的默认成员函数。 


目录

前文:

前言:

5. 赋值运算符重载

5.1 运算符重载

普通函数版 

运算符重载版 

 5. 2 赋值运算符重载

1. 赋值运算符重载格式

 2. 实例

注意: 

 5.3 特殊的运算符重载 前置++与后置++

 6. Date类的实现(多功能)

Date.h 

Date.cpp 

main.cpp 

补充知识:

7.const成员 

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


5. 赋值运算符重载

5.1 运算符重载

我们知道函数重载,那么运算符重载又是什么?

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其
返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)
注意
<  > 不能通过连接其他符号来创建新的操作符:比如operator@
<  > 重载操作符必须有一个类类型参数
<  > 用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义
 <  > 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
 <  >   .*  ::  sizeof  ?:  . 注意以上5个运算符不能重载。这个经常在笔试选择题中出

我们可以这样认为,运算符重载就是给运算符赋予一个新的含义,<专为自定义类型而生> 不过默认并不会改变运算符的基本逻辑。

我们来看看他在实际中的应用,我们实现两个Date类对象比较,1.是否相等 2. 是否小于

普通函数版 

我们可以看到,在这个版本里,代码编写者取名十分随意,那么当别人看到这段代码,内心的活动估计十分激昂。虽然在功能上很完美,但其代码却会让人抓耳挠腮。 

class Date
{
public:
	Date(int year = 2024, int month = 2, int day = 6)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//Data类拷贝构造,赋值重载,析构都不需要自己实现

	bool compare1(const Date& d) //DateEqual 
	{
		return _year == d._year
			&& _month == d._month
			&& _day == d._day;
	}

	bool compare2(const Date& d)//DateLess
	{
		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;
	}

private:
	int _year;
	int _month;
	int _day;
};
int main()
{

	Date d1(2024, 2, 7);
	Date d2(1997, 1, 1);
	cout << d1.compare1(d2) << endl;
	cout << d1.compare2(d2) << endl;
	return 0;
}

运算符重载版 

这一版本的代码可读性大大增强,还免去了起名的麻烦,同时在调用函数的时候,自定义类型也可以像内置类型一样简单快捷的使用运算符。

class Date
{
public:
	Date(int year = 2024, int month = 2, int day = 6)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//Data类拷贝构造,赋值重载,析构都不需要自己实现

	bool operator==(const Date& d)
	{
		return _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)
		{
			if (_month < d._month)
			{
				return true;
			}
			else if (_month == d._month)
			{
				return _day < d._day;
			}
		}
		return false;
	}

private:
	int _year;
	int _month;
	int _day;
};
int main()
{

	Date d1(2024, 2, 7);
	Date d2(1997, 1, 1);
	return 0;
}

int main()
{
	Date d1(2024, 2, 7);
	Date d2(1997, 1, 1);
	/*cout << d1.operator<(d2) << endl;
	cout << d1.operator==(d2) << endl;*/

	cout << (d1 < d2) << endl;//在编译器看来,等同于cout << d1.operator<(d2) << endl;
	cout << (d1 == d2) << endl;//在编译器看来,等同于cout << d1.operator==(d2) << endl;
	return 0;
}

 注意:因为 << 的运算符很高,因此在调用时需要加括号。

运算符重载并不会改变运算符的底层逻辑,因此对于双操作数运算符来说,左操作数就是函数的第一个参数,右操作数是第二个参数。

那么运算符重载能定义成全局的吗,是的可以,但是这样我们就不能访问类的私有成员。但将类成员公有化,如何保证封装性呢?

我们可以使用友元函数,或者定义成成员函数。 

 5. 2 赋值运算符重载

1. 赋值运算符重载格式

参数类型:const T&,传递引用可以提高传参效率
返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
检测是否自己给自己赋值
返回*this :要复合连续赋值的含义 

 2. 实例

我们发现,所谓的赋值重载与之前学到的拷贝构造几乎是一个模子里刻出来的,既然他们一样,那我们还有必要学他吗?

这里有一个误区,他们的实现确实一样,但有一个不同点。

<  > 拷贝构造是对并不存在的对象进行拷贝构造,既有拷贝又有构造;

<  > 赋值重载是对已经存在的对象进行赋值(拷贝)。 

class Date
{
public:
	Date(int year = 2024, int month = 2, int day = 6)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	
	Date(Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	bool operator==(const Date& d)
	{
		return _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)
		{
			if (_month < d._month)
			{
				return true;
			}
			else if (_month == d._month)
			{
				return _day < d._day;
			}
		}
		return false;
	}
	
	bool operator!=(const Date& d)
	{
		return !(*this == d);
	}
	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(2024, 2, 7);
	Date d2(1997, 1, 1);
	int a = 10;
	int b = 20;

	Date d3 = d1;//拷贝构造,即Date d3(d1);
	Date d4;
	d4 = d1;//赋值重载
	return 0;
}

我们在汇编视角可以看的更加清晰。 

  

注意: 

赋值重载只能定义成类的成员函数,不能定义成全局函数。

<  > 编译器不支持定义为全局函数,如果定义成全局会报错。

<  > 定义成全局函数,那么类中没有实现,作为默认成员函数之一,编译器会自动生成一个默认赋值重载,那么当调用的时候就会产生冲突。

<  > 用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值

我们不写,编译器会自动生成,那么我们还需要写吗?

视情况而定,值拷贝对于Date类来说很适用,那么对于我们之前写的Stack类呢?他会造成不小的麻烦 。关于这一点,在c++阶梯之类与对象(中)-CSDN博客中的拷贝构造函数一节有详细讲解,感兴趣的宝子可以去看看。

 5.3 特殊的运算符重载 前置++与后置++

前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载
C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器
自动传递

Date operator++(int)//后置++,后置++使用后加1,因此返回的结果是自己本身,返回之后自身+1
{
	Date tmp = *this;
	_day+=1;
	return tmp;
}

Date& operator++()//前置++,前置++使用前++,前置++返回的结果是自身+1
{
	_day++;
	return *this;
}

 6. Date类的实现(多功能)

在Date类实现常用运算符的重载,日期计算器,两日期的间隔等功能。

Date.h 

#include<iostream>
using namespace std;
class Date
{
public:

	// 获取某年某月的天数
	int GetMonthDay(int year, int month);

	// 全缺省的构造函数
	Date(int year , int month , int day );

	// 拷贝构造函数
	Date(const Date& d);

	// 赋值运算符重载
	Date& operator=(const Date& d);

	// 析构函数
	~Date();

	// 日期+=天数
	Date& operator+=(int day);

	// 日期+天数
	Date operator+(int day);

	// 日期-天数
	Date operator-(int day);

	// 日期-=天数
	Date& operator-=(int day);

	// 前置++
	Date& operator++();

	// 后置++
	Date operator++(int);

	// 后置--
	Date operator--(int);

	// 前置--
	Date& operator--();

	// >运算符重载
	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"
//获取每个月的天数
int Date::GetMonthDay(int year, int month)
{
	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];
}
//全缺省构造函数
Date::Date(int year = 1997, int month = 1, int day = 1)
{
	_year = year;
	_month = month;
	_day = day;
}

//拷贝构造
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

//赋值运算符重载
Date& Date::operator=(const Date& d)
{
	if (*this != d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}

//析构
Date::~Date()
{
	;
}

//日期+=天数
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)
{
	_day -= day;
	while (_day <= 0)
	{
		_day += GetMonthDay(_year, _month);
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
	}
	return *this;
}

//日期-天数
Date Date::operator-(int day)
{
	Date tmp = *this;//拷贝构造
	tmp -= day;
	return tmp;

}

//后置++
Date Date::operator++(int)//后置++,后置++使用后加1,因此返回的结果是自己本身,返回之后自身+1
{
	Date tmp = *this;
	_day++;
	return tmp;
}

//前置++
Date& Date::operator++()//前置++,前置++使用前++,前置++返回的结果是自身+1
{
	_day++;
	return *this;
}

//后置--
Date Date::operator--(int)
{
	Date tmp = *this;
	_day --;
	return tmp;
}

//前置--
Date& Date::operator--()
{
	_day--;
	return *this;
}

//重载==
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);
}

//重载<
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);
}

//日期-日期=天数
int Date::operator-(const Date& d)
{
	Date max = *this, min = d;
	int flag = 1;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		min += 1;
		n++;
	}
	return n * flag;	
}

//打印
void Date::Print()
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

main.cpp 

#include"Date.h"

int main()
{
	Date d1(2024, 2, 7);
	Date d2(1997, 5, 3);
	cout << (d1 - d2) << endl;
	d1 -= 10;
	d1.Print();
	d2 += 20;
	d2.Print();
	cout << (d1 < d2) << endl;
	Date d3(1, 1, 1);
	d1.Print();
	d3 = d1++;
	d3.Print();
	++d3;
	d3.Print();
	
	return 0;
}

Date的实现是对近期知识的总结,糅合了许多语法,并且难度也不大,值得一写。 

补充知识:

补充上述相关的简略友元与操作数顺序知识。 这里使用重载流插入操作符<< 来进行演示。

 我们根据所学的知识写了 << 的重载成员函数,但在使用的时候出现了问题。

我们之前说过, 运算符重载并不会改变运算符的底层逻辑,因此对于双操作数运算符来说,左操作数就是函数的第一个参数,右操作数是第二个参数。

因此按我们写的,应该这么使用

但很显然,这与我们预期的不符。成员函数隐含形参this,这个我们没有办法更改,因此只能将他实现为全局函数,然后更改函数参数的顺序。这里会用到友元,现在我们只需要知道友元的存在,不必深究。

虽然此时可以使用 << 输出Date类型了,但他只能输出一个,正常的 << 是可以连续流插入的。

这里的问题在于运算符的结合性,<< 是从左往右,cout<<d1 之后需要返回一个值,然后再调用 << 。我们做如下更改,

此时的流插入操作符重载和我们心目中的就一般无二了。 

7.const成员 

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

我们知道,成员函数中隐含的参数this指针是不允许显式写出来的,因此我们要将this指针const化就只能通过上面的方法。

请思考下面的几个问题:
1. const对象可以调用非const成员函数吗?
2. 非const对象可以调用const成员函数吗?
3. const成员函数内可以调用其它的非const成员函数吗?
4. 非const成员函数内可以调用其它的const成员函数吗?

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

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

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

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

  • 24
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值