c++重载运算符和重载函数

C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载。

重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是它们的参数列表和定义(实现)不相同。

当您调用一个重载函数或重载运算符时,编译器通过把您所使用的参数类型与定义中的参数类型进行比较,决定选用最合适的定义。选择最合适的重载函数或重载运算符的过程,称为重载决策。

c++中函数的重载

实例
同名函数 print() 被用于输出不同的数据类型:

#include <iostream>
using namespace std;
 
class printData
{
   public:
      void print(int i) {
        cout << "整数为: " << i << endl;
      }
 
      void print(double  f) {
        cout << "浮点数为: " << f << endl;
      }
 
      void print(char c[]) {
        cout << "字符串为: " << c << endl;
      }
};
 
int main(void)
{
   printData pd;
 
   // 输出整数
   pd.print(5);
   // 输出浮点数
   pd.print(500.263);
   // 输出字符串
   char c[] = "Hello C++";
   pd.print(c);
 
   return 0;
}

在这里插入图片描述
可以发现这里会自动识别我们输入的类型 之后自动输出

c++中运算重载符

您可以重定义或者重载大部分c++内部的运算符,这样就可以使用自定义的运算符

重载的运算符是带有特殊名称的函数,函数名称由关键字operator和其后要重载的运算符符号构成。和其他函数一样,重载运算符有一个返回类型和参数列表
在这里插入图片描述
然后我就依次给出实例供大家理解

单目运算符

下面我先给出一个进行取反运算的操作

#include <iostream>
using namespace std;

class TestClass {
public :
	int Testa;
	int Testb;

	TestClass() {
		//构造函数
		Testa = 0;
		Testa = 0;
	}
	TestClass(int a, int b) {
		Testa = a;
		Testb = b;
	}
	void DisplayDate() {
		cout << "Testa:"<<Testa << " " <<"Testb:"<<Testb << endl;
	}
	TestClass operator-() {
		Testa = -Testa;
		Testb = -Testb;
		return TestClass(Testa, Testb);
	}
};
int main() {
	TestClass tc1(1, 1), tc2(2, 2);
	-tc1;
	tc1.DisplayDate();

	tc2.DisplayDate();
	return 0;
}

在这里插入图片描述
从上边的例子我相信大家可以慢慢看懂的

双目运算符

我这个代码是在上个的基础上添加的

#include <iostream>
using namespace std;

class TestClass {
public :
	int Testa;
	int Testb;

	TestClass() {
		//构造函数
		Testa = 0;
		Testa = 0;
	}
	TestClass(int a, int b) {
		Testa = a;
		Testb = b;
	}
	void DisplayDate() {
		cout << "Testa:"<<Testa << " " <<"Testb:"<<Testb << endl;
	}
	//取反操作重载
	TestClass operator-() {
		Testa = -Testa;
		Testb = -Testb;
		return TestClass(Testa, Testb);
	}
	//加法操作重载
	TestClass operator+(const TestClass& b) {
		TestClass t;
		t.Testa = Testa + b.Testa;
		t.Testb = Testb + b.Testb;
		return t;
	}
};
int main() {
	TestClass tc1(1, 1), tc2(2, 2);
	-tc1;
	tc1.DisplayDate();

	tc2.DisplayDate();

	TestClass tc3 = tc1 + tc2;
	tc3.DisplayDate();
	return 0;
}

在这里插入图片描述

关系运算符

#include <iostream>
using namespace std;

class TestClass {
public :
	int Testa;
	int Testb;

	TestClass() {
		//构造函数
		Testa = 0;
		Testa = 0;
	}
	TestClass(int a, int b) {
		Testa = a;
		Testb = b;
	}
	void DisplayDate() {
		cout << "Testa:"<<Testa << " " <<"Testb:"<<Testb << endl;
	}
	//取反操作重载
	TestClass operator-() {
		Testa = -Testa;
		Testb = -Testb;
		return TestClass(Testa, Testb);
	}

	//加法操作重载
	TestClass operator+(const TestClass& b) {
		TestClass t;
		t.Testa = Testa + b.Testa;
		t.Testb = Testb + b.Testb;
		return t;
	}

	//重载小于(<)运算符
	bool operator<(const TestClass& d) {
		if (Testa < d.Testa && Testb < d.Testb) {
			return true;
		}
		else {
			return false;
		}
	}
};
int main() {
	TestClass tc1(1, 1), tc2(2, 2);
	-tc1;
	tc1.DisplayDate();

	tc2.DisplayDate();

	TestClass tc3 = tc1 + tc2;
	tc3.DisplayDate();

	if (tc1 < tc2) {
		cout << "tc1小于tc2" << endl;
	}
	else {
		cout << "tc1不小于tc2" << endl;
	}
	return 0;
}

在这里插入图片描述
通过这种运算符 我们可以轻易的判断两个对象之间的一些关系等

++和–运算符重载

#include <iostream>
using namespace std;

class TestClass {
public :
	int Testa;
	int Testb;

	TestClass() {
		//构造函数
		Testa = 0;
		Testa = 0;
	}
	TestClass(int a, int b) {
		Testa = a;
		Testb = b;
	}
	void DisplayDate() {
		cout << "Testa:"<<Testa << " " <<"Testb:"<<Testb << endl;
	}
	//取反操作重载
	TestClass operator-() {
		Testa = -Testa;
		Testb = -Testb;
		return TestClass(Testa, Testb);
	}

	//加法操作重载
	TestClass operator+(const TestClass& b) {
		TestClass t;
		t.Testa = Testa + b.Testa;
		t.Testb = Testb + b.Testb;
		return t;
	}

	//重载小于(<)运算符
	bool operator<(const TestClass& d) {
		if (Testa < d.Testa && Testb < d.Testb) {
			return true;
		}
		else {
			return false;
		}
	}

	//重载前缀自增运算符
	TestClass operator++() {
		++Testa; 
		++Testb;
		return TestClass(Testa, Testb);
	}
	//重载后缀自增运算符
	TestClass operator++(int) {
		TestClass tc(Testa, Testb);
		Testa++;
		Testb++;
		return tc;
	}
	
};
int main() {
	TestClass tc1(1, 1), tc2(2, 2);
	-tc1;
	tc1.DisplayDate();

	tc2.DisplayDate();
	cout << "------------------------" << endl;
	TestClass tc3 = tc1 + tc2;
	tc3.DisplayDate();
	cout << "------------------------" << endl;
	if (tc1 < tc2) {
		cout << "tc1小于tc2" << endl;
	}
	else {
		cout << "tc1不小于tc2" << endl;
	}
	cout << "------------------------" << endl;
	tc1++;
	tc1.DisplayDate();
	tc1++;
	tc1.DisplayDate();
	++tc2;
	tc2.DisplayDate();
	++tc2;
	tc2.DisplayDate();
	cout << "------------------------" << endl;
	return 0;
}

在这里插入图片描述

赋值运算符重载

#include <iostream>
using namespace std;

class TestClass {
public :
	int Testa;
	int Testb;

	TestClass() {
		//构造函数
		Testa = 0;
		Testa = 0;
	}
	TestClass(int a, int b) {
		Testa = a;
		Testb = b;
	}
	void DisplayDate() {
		cout << "Testa:"<<Testa << " " <<"Testb:"<<Testb << endl;
	}
	//取反操作重载
	TestClass operator-() {
		Testa = -Testa;
		Testb = -Testb;
		return TestClass(Testa, Testb);
	}

	//加法操作重载
	TestClass operator+(const TestClass& b) {
		TestClass t;
		t.Testa = Testa + b.Testa;
		t.Testb = Testb + b.Testb;
		return t;
	}

	//重载小于(<)运算符
	bool operator<(const TestClass& d) {
		if (Testa < d.Testa && Testb < d.Testb) {
			return true;
		}
		else {
			return false;
		}
	}

	//重载前缀自增运算符
	TestClass operator++() {
		++Testa; 
		++Testb;
		return TestClass(Testa, Testb);
	}
	//重载后缀自增运算符
	TestClass operator++(int) {
		TestClass tc(Testa, Testb);
		Testa++;
		Testb++;
		return tc;
	}

	//赋值运算符重载
	void  operator=(const TestClass &d) {
		Testa = d.Testa;
		Testb = d.Testb;
	}
	
};
int main() {
	TestClass tc1(1, 1), tc2(2, 2);
	-tc1;
	tc1.DisplayDate();

	tc2.DisplayDate();
	cout << "------------------------" << endl;
	TestClass tc3 = tc1 + tc2;
	tc3.DisplayDate();
	cout << "------------------------" << endl;
	if (tc1 < tc2) {
		cout << "tc1小于tc2" << endl;
	}
	else {
		cout << "tc1不小于tc2" << endl;
	}
	cout << "------------------------" << endl;
	tc1++;
	tc1.DisplayDate();
	tc1++;
	tc1.DisplayDate();
	++tc2;
	tc2.DisplayDate();
	++tc2;
	tc2.DisplayDate();
	cout << "------------------------" << endl;

	TestClass tc4 = tc1;
	tc4.DisplayDate();
	cout << "------------------------" << endl;
	return 0;
}

在这里插入图片描述

函数调用运算符()重载

#include <iostream>
using namespace std;

class TestClass {
public :
	int Testa;
	int Testb;

	TestClass() {
		//构造函数
		Testa = 0;
		Testa = 0;
	}
	TestClass(int a, int b) {
		Testa = a;
		Testb = b;
	}
	void DisplayDate() {
		cout << "Testa:"<<Testa << " " <<"Testb:"<<Testb << endl;
	}
	//取反操作重载
	TestClass operator-() {
		Testa = -Testa;
		Testb = -Testb;
		return TestClass(Testa, Testb);
	}

	//加法操作重载
	TestClass operator+(const TestClass& b) {
		TestClass t;
		t.Testa = Testa + b.Testa;
		t.Testb = Testb + b.Testb;
		return t;
	}

	//重载小于(<)运算符
	bool operator<(const TestClass& d) {
		if (Testa < d.Testa && Testb < d.Testb) {
			return true;
		}
		else {
			return false;
		}
	}

	//重载前缀自增运算符
	TestClass operator++() {
		++Testa; 
		++Testb;
		return TestClass(Testa, Testb);
	}
	//重载后缀自增运算符
	TestClass operator++(int) {
		TestClass tc(Testa, Testb);
		Testa++;
		Testb++;
		return tc;
	}

	//赋值运算符重载
	void  operator=(const TestClass &d) {
		Testa = d.Testa;
		Testb = d.Testb;
	}

	//函数运算符()重载
	TestClass operator()(int a,int b) {
		Testa += a;
		Testb += b;
		return TestClass(Testa, Testb);
	}

	
};
int main() {
	TestClass tc1(1, 1), tc2(2, 2);
	-tc1;
	tc1.DisplayDate();

	tc2.DisplayDate();
	cout << "------------------------" << endl;
	TestClass tc3 = tc1 + tc2;
	tc3.DisplayDate();
	cout << "------------------------" << endl;
	if (tc1 < tc2) {
		cout << "tc1小于tc2" << endl;
	}
	else {
		cout << "tc1不小于tc2" << endl;
	}
	cout << "------------------------" << endl;
	tc1++;
	tc1.DisplayDate();
	tc1++;
	tc1.DisplayDate();
	++tc2;
	tc2.DisplayDate();
	++tc2;
	tc2.DisplayDate();
	cout << "------------------------" << endl;

	TestClass tc4 = tc1;
	tc4.DisplayDate();
	cout << "------------------------" << endl;
	tc1(1, 1);
	tc1.DisplayDate();
	cout << "------------------------" << endl;
	return 0;
}

在这里插入图片描述

输入输出运算符重载

#include <iostream>
using namespace std;

class TestClass {
public :
	int Testa;
	int Testb;
	
	TestClass() {
		//构造函数
		Testa = 0;
		Testa = 0;
	}
	TestClass(int a, int b) {
		Testa = a;
		Testb = b;
	}
	void DisplayDate() {
		cout << "Testa:"<<Testa << " " <<"Testb:"<<Testb << endl;
	}
	//取反操作重载
	TestClass operator-() {
		Testa = -Testa;
		Testb = -Testb;
		return TestClass(Testa, Testb);
	}

	//加法操作重载
	TestClass operator+(const TestClass& b) {
		TestClass t;
		t.Testa = Testa + b.Testa;
		t.Testb = Testb + b.Testb;
		return t;
	}

	//重载小于(<)运算符
	bool operator<(const TestClass& d) {
		if (Testa < d.Testa && Testb < d.Testb) {
			return true;
		}
		else {
			return false;
		}
	}

	//重载前缀自增运算符
	TestClass operator++() {
		++Testa; 
		++Testb;
		return TestClass(Testa, Testb);
	}
	//重载后缀自增运算符
	TestClass operator++(int) {
		TestClass tc(Testa, Testb);
		Testa++;
		Testb++;
		return tc;
	}

	//赋值运算符重载
	void  operator=(const TestClass &d) {
		Testa = d.Testa;
		Testb = d.Testb;
	}

	//函数运算符()重载
	TestClass operator()(int a,int b) {
		Testa += a;
		Testb += b;
		return TestClass(Testa, Testb);
	}
	
	//输入输出运算符重载
	friend  ostream& operator<<(ostream& output, const TestClass& d) {
		output << "Testa:" << d.Testa << "  " << "Testb:" << d.Testb << endl;
		return output;
	}
	friend istream& operator>>(istream& input, TestClass& D)
	{
		input >> D.Testa >> D.Testb;
		return input;
	}
	
};
int main() {
	TestClass tc1(1, 1), tc2(2, 2);
	-tc1;
	tc1.DisplayDate();

	tc2.DisplayDate();
	cout << "------------------------" << endl;
	TestClass tc3 = tc1 + tc2;
	tc3.DisplayDate();
	cout << "------------------------" << endl;
	if (tc1 < tc2) {
		cout << "tc1小于tc2" << endl;
	}
	else {
		cout << "tc1不小于tc2" << endl;
	}
	cout << "------------------------" << endl;
	tc1++;
	tc1.DisplayDate();
	tc1++;
	tc1.DisplayDate();
	++tc2;
	tc2.DisplayDate();
	++tc2;
	tc2.DisplayDate();
	cout << "------------------------" << endl;

	TestClass tc4 = tc1;
	tc4.DisplayDate();
	cout << "------------------------" << endl;
	tc1(1, 1);
	tc1.DisplayDate();
	cout << "------------------------" << endl;
	TestClass tc5;
	cin >> tc5;
	cout << tc5;
	return 0;
}

在这里插入图片描述

下标运算符[]重载

下标操作符[]可以用来a耐高温数组元素 可以增强我们自己数组的功能

int& operator[](int i)
	{
		if (i > elems.size())
		{
			cout << "索引超过最大值" << endl;
			// 返回第一个元素
			return elems[0];//elems为数组元素
		}
		return elems[i];
	}

最常用就是我上边写的这些了 希望我写的对大家有所帮助
欢迎大家关注我的账号 有问题可以去主页联系我
我是头号理想 大家一起进步

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值