C++ 关系运算符重载与赋值运算符重载

在我之前的文章中,介绍了算术运算符重载的基本使用,现在我将继续介绍关系运算符重载和赋值运算符重载。

还没看过的朋友可以点击链接前去看一下:C++ 运算符重载
https://blog.csdn.net/cpp_learner/article/details/104208689

其实他们之间的用法都是类似的,没有什么区别,都是围绕着关键字“operator"展开。

下面我举了两例子来介绍关系运算符重载与赋值运算符重载。
例一:
定义男孩类,再根据男孩类定义出两个男孩对象,对两个男孩进行比较
比较规则是:(薪资 * 系数 - 年龄 * 系数) / 100

例二:
根据例一的男孩类定义出三个对象b1, b2, b3
b1赋初值(“王五”, 33, 48000, 8);
然后再将b1赋值给b2 和 b3
最后打印出来看结果

下面是代码:

Boy.h

#pragma once
#include <string>

class Boy {
public:
	Boy(const char *name=NULL, int age=0, int salary=0, int coefficient=0);
	~Boy();

	// 关系运算符重载
	bool operator>(const Boy &boy);
	bool operator<(const Boy &boy);
	bool operator==(const Boy &boy);
	
	// 运算函数
	int power() const;

	/////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////
	/////////////////////////////////////////////////////////////////////

	// 赋值运算符重载
	Boy &operator=(const Boy &boy);

	std::string descrition() const;

private:
	char *name;
	int age;
	int salary;
	int coefficient;	// 系数
};

Boy.cpp

#include <sstream>
#include "Boy.h"

Boy::Boy(const char *name, int age, int salary, int coefficient) {
	// 如果name指针不是NULL值的话,先释放该内存,然后再申请
	if (!name) {
		name = "无名";
	}

	// 这样赋值指针name时,内存就刚刚好了
	this->name = new char[strlen(name)+1];
	strcpy_s(this->name, strlen(name)+1, name);

	this->age = age;
	this->salary = salary;
	this->coefficient = coefficient;
}

Boy::~Boy() {
	if (name) {
		delete[] name;
	}
}

// 关系运算符重载
bool Boy::operator>(const Boy &boy) {
	if (power() > boy.power()) {
		return true;
	} else {
		return false;
	}
}

bool Boy::operator<(const Boy &boy) {
	if (power() < boy.power()) {
		return true;
	} else {
		return false;
	}
}

bool Boy::operator==(const Boy &boy) {
	if (power() == boy.power()) {
		return true;
	} else {
		return false;
	}
}

int Boy::power() const {
	// 比较规则
	// (salary * coefficient - age * coefficient) / 100
	return (salary * coefficient - age * coefficient) / 100;
}

/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////

// 赋值运算符重载
Boy& Boy::operator=(const Boy &boy) {
	if (name) {
		delete[] name;
	}

	this->name = new char[strlen(boy.name)+1];
	strcpy_s(this->name, strlen(boy.name)+1, boy.name);

	this->age = boy.age;
	this->salary = boy.salary;
	this->coefficient = boy.coefficient;

	return *this;
}

std::string Boy::descrition() const {
	std::stringstream ret;

	ret << "姓名:" << name << "   年龄:" << age << "   薪资:" << salary << "   系数:" << coefficient;

	return ret.str();
}

main

#include <iostream>
#include "Boy.h"

using namespace std;

int main(void) {
	// 关系运算符重载
	// 比较两个人的大小
	// 比较规则是:(salary * coefficient - age * coefficient) / 100
	cout << "---关系运算符重载---" << endl;
	Boy boy1("张三", 30, 38000, 10);
	Boy boy2("李四", 32, 40000, 6);

	if (boy1 > boy2) {
		cout << "选择boy1" << endl;
	} else if (boy1 < boy2) {
		cout << "选择boy2" << endl;
	} else if (boy1 == boy2) {
		cout << "两人平等,差不多啊" << endl;
	}

	cout << "boy1 = " << boy1.power() << endl;
	cout << "boy2 = " << boy2.power() << endl;


	cout << "\n\n\n\n\n";
	////////////////////////////////////////////////////////////
	
	cout << "---赋值运算符重载---" << endl;
	// 赋值运算符重载
	// 定义出三个对象b1, b2, b3
	// b1赋初值("王五", 33, 48000, 8);
	// 然后再将b1赋值给b2 和 b3

	Boy b1("王五", 33, 48000, 8);
	Boy b2, b3;

	cout << "b1:" << b1.descrition() << endl;
	cout << "b2:" << b2.descrition() << endl;
	cout << "b3:" << b3.descrition() << endl;

	cout << "赋值后:" << endl;
	b3 = b2 = b1;

	cout << "b1:" << b1.descrition() << endl;
	cout << "b2:" << b2.descrition() << endl;
	cout << "b3:" << b3.descrition() << endl;

	system("pause");
	return 0;
}

运行截图:

在这里插入图片描述

总结:
1.定义关系运算符时一般采用bool值作为返回值;
2.定义赋值运算符时一般返回该对象本身的应用,以便做链式连续赋值处理:b1 = b2 = b3;
3.无论是算术运算符还是关系运算符还是赋值运算符也好,参数必须是const类型修饰,防止里面的值被修改。
4.使用成员函数实现或者使用友元函数实现都可以

C++ 友元:https://blog.csdn.net/cpp_learner/article/details/104193181

发布了39 篇原创文章 · 获赞 17 · 访问量 3834

猜你喜欢

转载自blog.csdn.net/cpp_learner/article/details/104223074