运算符重载 重载的两种方法 + - ++ ——运算符重载

一、运算符重载入门技术推演

#include<iostream>
using namespace std;

class Complex
{
public:
	int a;
	int b;
public:
	Complex(int a = 0,int b = 0)
	{
		this->a = a;
		this->b = b;
	}
	void printCom()
	{
		cout<<a<<"+"<<b<<"i"<<endl;
	}
protected:
private:
};
//定义 了全局函数
Complex myAdd(Complex &c1,Complex &c2)
{
	Complex tmp(c1.a + c2.a,c1.b + c2.b);
	return tmp;
}
//全局函数 完成 +操作符  重载
//Complex operator+(Complex &c1,Complex &c2)

//函数名  升级 
Complex operator+(Complex &c1,Complex &c2)
{
	Complex tmp(c1.a + c2.a,c1.b + c2.b);
	return tmp;
}


int main()
{
	
	int a = 0;
	int b = 0;
	int c;
	//一、a和b是基础数据类型 编译器已经知道如何运算
	c = a + b;
	
	 //a + bi复数运算规则
	Complex c1(1,2),c2(3,4);
	Complex c3;//二、类 也是一个数据类型 用户自定义的数据类型,c++编译器默认不知道如何运算的
	//c3 = c1 + c2;//报错
	//三、c++编译器应该给我们程序员提供一种机制。。。
	//让自定义数据类型有机会进行运算符操作====》这就是传说的运算符重载

	//四、运算符重载机制
	//步骤一
//	Complex c4 = myAdd(c1,c2);
//	c4.printCom();
	//步骤二---》complex c4 = c1 + c2;


	//步骤二
	//Complex c4 = operator+(c1,c2);
	//c4.printCom();
	//步骤三与步骤二的效果一样
	Complex c4 =c1 + c2;//不再报错了
	c4.printCom();


	/*总结:
		1:运算符重载 本质 是一个函数调用*/
		
	system("pause");
	return 0;
}
二、运算符重载的两种方法

#include<iostream>
using namespace std;

class Complex
{
public:
	int a;
	int b;
public:
	Complex(int a = 0,int b = 0)
	{
		this->a = a;
		this->b = b;
	}
	void printCom()
	{
		cout<<a<<"+"<<b<<"i"<<endl;
	}

//2:成员函数方法 实现 - 运算符重载
	Complex operator-(Complex &c2)
	{	
		Complex tmp(1,2);
		return tmp;
	}
protected:
private:
};
//1:全局函数法 实现 + 运算符重载
Complex operator+(Complex &c1,Complex &c2)
{
	Complex tmp(c1.a + c2.a ,c1.b + c2.b);
	return tmp;
}


int main()
{
/*
	定义运算符重载函数名的步骤
		全局函数、类成员函数方法实现运算符重载步骤
		1)要承认操作符重载是一个函数,写出函数名称operator+ () 
		2)根据操作数,写出函数参数 
		3)根据业务,完善函数返回值(看函数是返回引用 还是指针 元素),及实现函数业务
*/

	Complex c1(1,2),c2(3,4);
	
	//1:全局函数法 实现 + 运算符重载
	//Complex operator+(Complex &c1,Complex &c2) 
	Complex c3 = c1 + c2;
	c3.printCom();

	//2:成员函数方法 实现 - 运算符重载
	//c1.operator-(this,c2);
	//Complex operator+(Complex &c2);
	
	system("pause");
	return 0;
}

三、运算符重载的升级--使用友元函数 + 前置后置运算符重载

自增自减运算符

(1)前缀形式

前置形式  返回  改变后的对象,在成员运算符函数中返回 *this ;在全局运算函数中    返回  修改后的参数

(2)后缀形式

后缀形式 返回 改变之前的值,需要创建一个对象代表这个值的独立对象并返回它,因此后缀运算是通过传值方式返回的。

TV

概括来讲:前置返回对象引用,后置先创建对象后返回旧值


#include<iostream>
using namespace std;

class Complex
{
private://此处升级版
	int a;
	int b;
	//全局函数 重载+运算符
	friend Complex operator+(Complex &c1,Complex &c2);
	//重载前置 ++ 运算符 ----友元函数
	friend Complex& operator++(Complex &c1);
public:
	Complex(int a = 0,int b = 0)
	{
		this->a = a;
		this->b = b;
	}
	void printCom()
	{
		cout<<a<<"+"<<b<<"i"<<endl;
	}

//2:成员函数方法 实现 - 运算符重载

	Complex operator-(Complex &c2)
	{	
		Complex tmp(this->a - c2.a,this->b - c2.b);
		return tmp;
	}
//2:成员函数方法 实现 -- 运算符重载
	Complex& operator--()
	{
		this->a--;
		this->b--;
		return *this;
	}
protected:
private:
};
//1:全局函数法 实现 + 运算符重载
Complex operator+(Complex &c1,Complex &c2)
{
	Complex tmp(c1.a + c2.a ,c1.b + c2.b);
	return tmp;
}
//前置++
Complex& operator++(Complex &c1)
{
	c1.a++;
	c1.b++;
	return c1;
}
//后置++ ====== 函数的返回值不是函数重载的标准
Complex operator++(Complex &c1,int)//新的语法占位符
{
	Complex tmp(0,0);
	return tmp;
}

int main()
{
/*
	定义运算符重载函数名的步骤
		全局函数、类成员函数方法实现运算符重载步骤
		1)要承认操作符重载是一个函数,写出函数名称operator+ () 
		2)根据操作数,写出函数参数 
		3)根据业务,完善函数返回值(看函数是返回引用 还是指针 元素),及实现函数业务
*/

	Complex c1(1,2),c2(3,4);
	
	//1:全局函数法 实现 + 运算符重载
	//Complex operator+(Complex &c1,Complex &c2) 
	Complex c3 = c1 + c2;
	c3.printCom();

	//2:成员函数方法 实现 - 运算符重载
	//c1.operator-(this,c2);
	//Complex operator+(Complex &c2);
	Complex c4 = c1 - c2;
	c4.printCom();


	//前置++操作符 用全局函数实现
	//Complex& operator++(Complex &c1);
	++c1;
	c1.printCom();

	//前置--操作符 用成员函数实现
	//Complex& operator--();
	--c1;
	c1.printCom();

	//后置++操作符 用全局函数实现
	//Complex operator++(Complex &c1);//返回元素--与前置++不一样
	c1++;
	c1.printCom();
	
	system("pause");
	return 0;
};





发布了33 篇原创文章 · 获赞 2 · 访问量 8526

猜你喜欢

转载自blog.csdn.net/QQ960054653/article/details/59477295
今日推荐