c++操作符重载,单目和双目运算符的重载

1.c++允许重载的操作符
2.重载不能改变运算符的优先级别
3.不能改变运算符的结合性
4.重载运算符的函数不能有默认的参数

5.重载的运算符必须用户定义的自定义类型的对象一起使用,其参数至少应有一个是类对象(或是对象的引用)
6用于类对象的运算符一般必须重载,但是有两个例外,运算符"=“和运算符”&"不用用户重载。
7.应当使重载运算符的功能类似与在运算符作用于标准类型数据时候所实现的功能。
8.运算符重载函数可以是类的成员函数,也可以是类的友元函数,还可以是既非类的成员函数,也不是类的友元函数的普通函数。
在这里插入图片描述

// 友元函数.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<math.h>
#include<iostream>

using namespace std;
class Complex {
public:
	Complex(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	void printComplex()
	{
		cout << "(" << this->a << "." << this->b << "i)" << endl;
	}
	friend Complex addd(Complex &c1, Complex &c2);
	//friend Complex operator+(Complex &c1, Complex &c2);
	//friend Complex& operator-(Complex &c1, Complex &c2);
	friend Complex operator-(Complex &c1, Complex &c2);
	Complex addd(Complex &another)
	{
		Complex temp(this->a + another.a, this->b + another.b);
		return temp;
	}
	Complex operator+(Complex &another)
	{
		Complex temp(this->a + another.a, this->b + another.b);
		return temp;
	}
	Complex operator-(Complex &another)
	{
		Complex temp(this->a - another.a, this->b - another.b);
		return temp;
	}
private:
	int a; int b;
};
Complex addd(Complex &c1, Complex &c2)
{
	Complex temp(c1.a + c2.a, c1.b + c2.b);
	return temp;
}
//操作符重载写在全局
/*Complex operator+(Complex &c1, Complex &c2)
{
	Complex temp(c1.a + c2.a, c1.b + c2.b);
	return temp;
}*/
/*Complex& operator-(Complex &c1, Complex &c2)
{
	Complex temp(c1.a - c2.a, c1.b - c2.b);
	return temp;
}*/
/*Complex operator-(Complex &c1, Complex &c2)
{
	Complex temp(c1.a - c2.a, c1.b - c2.b);
	return temp;
}*/
int main()
{ 
	Complex c1(1, 2);
	Complex c2(3, 4);
	c1.printComplex();
	c2.printComplex();
	cout << "****************" << endl;
	Complex c3 = addd(c1, c2);
	c3.printComplex();
	cout << "****************" << endl;
	Complex c4 = c1.addd(c2);
	c4.printComplex();
	cout << "****************" << endl;
	Complex c5 = c1 + c2;//这种写法会自动匹配这两种方式,我们这里把两种方法都写了,二义性,报错
	c5.printComplex();//等价于C6这个 operator+(c1, c2);
	                  //c1.operator(c2)
	//Complex c6 = operator+(c1, c2);
	//c6.printComplex();
	cout << "****************" << endl;
	/*
	//friend Complex& operator-(Complex &c1, Complex &c2);
	Complex c7 = c1 - c2-c1;//c1-c2返回temp别名 c1-c2执行完后,temp回收,再减c1->乱码
	c7.printComplex();
	*/
	Complex c8 = c1 - c2 ;

    return 0;
}


单目,双目运算符重载

operator#(L,R);//全局函数
L.opetator#®//成员函数

// 友元函数.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<math.h>
#include<iostream>

using namespace std;
class Complex {
public:
	Complex(int a, int b)
	{
		this->a = a;
		this -> b = b;
	}
	void printComplex()
	{
		cout << "(" << this->a << " " << this->b << "i)" << endl;
	}
	//friend Complex & operator+=(Complex &c1, Complex &c2);
	//friend Complex& operator++(Complex &c1);
	//friend	const Complex  operator++(Complex &c1, int);
	Complex & operator+=(Complex & another)
	{
		this->a += another.a;
		this->b += another.b;
		return *this;
	}
	const Complex  operator++( int)//亚元,天坑用的
	{
		//Complex temp(this->a,this->b);
		Complex temp = *this;
		this->a++;
		this->b++;
		return temp;
	}
private:
	int a;
	int b;
};

//全局
/*Complex & operator+=(Complex &c1, Complex &c2)//返回引用这样可以连续+=
{
	//Complex temp(c1.a + c2.a, c1.b + c2.b);
	//c1 = temp;
	//return c1;
	c1.a += c2.a;
	c1.b += c2.b;
	return c1;
}*/
/*
Complex& operator++(Complex &c1)
{
	c1.a =c1.a+1;
	c1.b = c1.b+1;
	return c1;

}
const Complex  operator++(Complex &c1, int)//占位符,重载后++
{//不加const会实现连加,加上const使得到的结果不再发改变,const c1++,不会再加,有const修饰
	Complex temp(c1.a, c1.b);
	c1.a++;
	c1.b++;
	return temp;

}
*/
int main()
{ 
	Complex c1(1, 2);
	Complex c2(3, 4);
	//(c1 += c2)+=c2;
	//c1.printComplex();
	//++++c1;
	//c1.printComplex();
	c1++;//后++会自动调用亚元操作符,带有占位符的
	c1.printComplex();
	
    return 0;
}


左移右移操作符重载

// 友元函数.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<math.h>
#include<iostream>

using namespace std;
class Complex {
public:
	Complex(int a, int b)
	{
		this->a = a;
		this -> b = b;
	}
	void printComplex()
	{
		cout << "(" << this->a << " " << this->b << "i)" << endl;
	}
	/*ostream& operator<<(ostream &os)//c1.operator(cout)
	{
		os<< "(" << this->a << " " << this->b << "i)" << endl;
		return os;
	}*/
	friend ostream& operator<<(ostream &os, Complex &c);
	friend istream& operator>>(istream &is, Complex &c1);
private:
	int a;
	int b;
};

ostream& operator<<(ostream &os, Complex &c)
{
	os<< "(" << c.a << " " << c.b << "i)" << endl;
	return os;
}
istream& operator>>(istream &is, Complex &c1)
{
	cout << "a:   b:  " ;
	is >> c1.a >> c1.b;
	return is;
}
int main()
{ 
	Complex c1(1, 2);
	Complex c2(3, 4);
	cout << c1<<c2;//operator<<(cout,c1)
	//c1 << cout;//反了 所以左移操作符只能写在全局
	cin >> c1;
	cout << c1;
    return 0;
}


发布了35 篇原创文章 · 获赞 2 · 访问量 2419

猜你喜欢

转载自blog.csdn.net/weixin_41375103/article/details/104381066