C++学习之运算符重载一

引言

  • 类 ,也是一种数据类型,相当于用户自定义的一种数据类型。那么,既然是用户自定义的,C++编译器肯定时无法识别的
  • 为此,要想对同一运算符实现不同的功能,就要我们人为重写一个重载函数,在程序中遇到运算符时,就可以调用我们写好的函数,实现运算符的另一功能
  • 这也就是运算符重载的本质:函数调用

  • 下边,以重载加号+运算符为例,认识运算符重载

  • 重载+运算符

#include<iostream>
using namespace std;
class Complex
{
public:
	Complex(int a,int b)
	{
		this->a = a;
		this->b =b;
	}
	void print()
	{
		cout<<a<<"+"<<b<<"i"<<endl;
	}
public:
	int a;int b;		
};
//重载+运算符  -->对+赋予了新的含义:对自定义数据类型进行相加
Complex operator+(Complex& c1,Complex& c2)
{
	Complex tmp(c1.a+c2.a,c1.b+c2.b);
	return tmp;
}
//实现复数相加 a+bi
void main()
{
	Complex c1(1,2),c2(2,3);
	Complex c3 = c1 + c2;
	c3.print();
	system("pause");
}
/*
 4 + 6i
 请按任意键继续. . .
 */

  • 有心的朋友可能发现,在上面第一个案例中,定义的一个全局函数可以直接使用类Complex中定义的变量a b;因为是定义的public变量。而我们一般都定义成private变量,所以,如果要实现这样的写法,就要用到下面介绍的 全局函数实现运算符重载,同时对比 成员函数方法 实现。
  • 全局函数和成员函数方法实现±运算符重载
#include<iostream>
using namespace std;
class Complex
{
	friend Complex operator+(Complex& c1,Complex& c2)friend Complex& operator++(Complex& c1);
	friend Complex& operator--(Complex& c1, int);
public:
	 Complex(int a,int b)
	 {
		  this->a = a;
		  this->b =b;
	 }
	 void print()
	 {
 		 cout<<a<<"+"<<b<<"i"<<endl;
 	}
 	//成员函数实现 - 运算符重载
 	Complex operator-(Complex& c1,Complex& c2)
 	{
		Complex tmp(c1.a-c2.a,c1.b-c2.b);
		 return tmp;
	}
	//成员函数实现 前置--
	Complex& operator--()
	{
		this->a--;
		this->b--;
		return *this;
	}
	//成员函数是按 后置--
	Complex& operator()
	{
		Complex tmp = *this;
		this->a--;
		this->b--;
		return tmp;
	}
private:
	 int a;int b;  
};
//全局函数 重载+运算符 
Complex operator+(Complex& c1,Complex& c2)
{
	//这里用到了private变量ab,所以要把该函数声明为类complex的友元函数
 	Complex tmp(c1.a+c2.a,c1.b+c2.b);
	return tmp;
}
//全局函数 重载 前置++
Complex& operator++(Complex& c1)
{
	c1.a++;
	c1.b++;
	return c1;
}
//全局函数 重载 后置++	
//为了和前置++区分开,这里 使用了一个占位符 int
Complex & operator++(Compelex &c1, int)
{
	//先使用后++ 所以返回自身
	Complex tmp =c1;
	c1.a++;
	c1.b++;
	return c1;
}
//实现复
//实现复数相加 a+bi
void main()
{
	 Complex c1(1,2),c2(2,3);
	 //函数原型
	 //1 全局函数 +
	 //Complex operator+(Complex &c1,Complex &c2)
	 Complex c3 = c1 + c2;
 	 c3.print();
 	 //2 成员函数 -
 	 //c1.operator-(this,Complex &c2)  左操作数由this指针传递
 	 //Complex c1.operator-(Complex &c2)	
 	 Complex c4 = c1 - c2;
 	 c4.print();
 	 //3 全局函数 前置++ 	 //返回一个自身
 	 ++c1;
 	 //Complex & operator++(Complex &c1) 
 	 c1.print();
 	 //4 成员函数 前置--
 	 --c2;
 	 //Complex& operator--()
 	 c2.print();
 	 //5 全局函数 后置++	
 	 c1++;
 	 //Complex& operator++(Complex &c1)
 	 c1.print();
 	 //6 成员函数 后置--
 	c2--;
 	// Complex &operator--()
 	c2.print();
 	 
	 system("pause");
}
/*
 4 + 6i
 请按任意键继续. . .
 */
`

  • 相信经过上面一系列的案例,大家已经对运算符重载有了一个基本的认识,下面再补充一个大家经常用到的,输入输出流 cin>>“123”>>endl; cout<<“345”<<endl;这里面就用到了对左移右移运算符的重载,下面给大家写个案例
  • 全局函数和成员函数方法实现<<运算符重载
#include<iostream>
using namespace std;
class Complex
{
	friend ostream & operator<<(ostream &out,Complex &c1);
public:
	Complex(int a=0;int b=0)
	{
		this->a = a;
		this->b = b;
	}
	
private:
	int a;
	int b;	
}; 
//全局实现 <<
ostream & operator<<(ostream &out,Complex &c1)
{
	out<<c1.a<<"+"<<c1.b<<"i"<<endl;
	return out;
}

void main()
{
	Complex c1(1,2);
	//1 全局函数实现
	cout << c1;
	//转到cout定义出查看类型:ostream
	//2 成员函数 不能实现: 源码改不了
	//3 全局函数方法升级:  实现链式编程
	cout<<c1<<"123";
	// 提示“<<”: 非法,左操作数包含 void 类型
	//cout.operator<<(c1).operator<<("aaa")
	//void.operator<<("aaa");
	cout<<c1<<"aaa"<<endl;
		
	system("pause");
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值