c++运算符重载

1.加号运算符重载

作用:实现两个自定义数据类型相加的运算

单纯的让编译器去加,它不知道该如何去加,就比如说类里面有很多属性,我创建的对象是只需要让两个对象中的某些属性进行相加,这就需要用到运算符重载了。

#include
using namespace std;

class person
{
public:
	person(int a,int b)
	{
		this->m_a = a;
		this->m_b = b;
	}
	int m_a;
	int m_b;
	person operator+ (const person& p)      //成员函数实现+运算符重载
	{
		person temp(0, 0);  //有参构造存在的情况下不会去调用无参构造
		temp.m_a = m_a + p.m_a;
		temp.m_b = m_b + p.m_b;
		return temp;
	}
};
person operator+(const person& p1, const person& p2)  //全局函数实现运算符重载
{
	person temp(0, 0);
	temp.m_a = p1.m_a + p2.m_a;
	temp.m_b = p2.m_b + p1.m_b;
	return temp;
}
//运算符重载也可以发生函数重载,运算符重载也可以看成是实现了一个函数
person operator+(const person& p1, int num)  //使新建的对象属性是在p1的基础上每个属性都+num
{
	person temp(0, 0);
	temp.m_a = p1.m_a + num;
	temp.m_b = p1.m_b + num;
	return temp;
}
int main()
{
	//person p1(10, 20);
	//person p2(30, 40);
	成员函数实现重载的本质是 p3=p2.operator+(p1)
	//person p3 = p1 + p2;  //不定义运算符+怎么做的话,编译不通过
	//cout << p3.m_a << "  " << p3.m_b << endl;

	person p3(-1, -5);
	person p4(15, 25);
	person p5 = p3 + p4;
	cout << p5.m_a << "  " << p5.m_b << endl;
	//全局函数实现重载的本质是  operator+(p3,p4)

	person p6 = p3 + 10;  //本质是 p6 =operator+ (p3,10);
	cout << p6.m_a << " " << p6.m_b << endl;
}

还需要知道 内置的数据类型的表达式的运算符是不可能修改的,比如说定义函数是1+1=0,这是不可能的。

不要滥用运算符重载,加法不要写成减法,降低可读性。

2.左移运算符重载

作用:可以输出自定义数据类型

c++左移运算符是输出流,举个例子我要输出一个对象里面的所有属性,我得一个个的加点运算符使它全部打印出来,
我可以重新定义左移运算符,让我能够只写出对象名称就能将它的全部属性打印出来,我还可以控制输出哪些属性,哪些不输出,做到自定义输出。

#include
using namespace std;

class person
{
	friend ostream& operator<<(ostream& cout, person& p);
public:
	person(int a,int b)
	{
		this->m_a = a;
		this->m_b = b;
	}
private:
	int m_a;
	int m_b;

};
//成员函数实现不了我们想要的左移运算符重载,因为 p<
//全局函数实现左移重载
//ostream对象只能有一个
ostream& operator<<(ostream& cout, person& p)    //返回值是一个cout,那么可以继续使用cout,进行链式编程
{
	cout << "a:" << p.m_a << "   b:" << p.m_b << endl;
	return cout;
}
int main()
{
	person p(20, 15);
	cout << p << endl;
}


3.递增递减运算符重载

是为了控制在对象内让哪些成员去进行++或–操作

#include
using namespace std;

class myinteger
{
public:
	//前置递增运算符重载
	myinteger& operator++()
	{
		m_num++;
		return *this;   //返回引用是为了对一直对一个数据进行递增操作
	}
	myinteger operator++(int)   //使用占位参数来区分前置还是后置
	{
		myinteger temp; 
		temp = *this;  //记录当前本身的值,让它返回,之后再将它+1;
		m_num++;
		return temp;
	}

	myinteger()
	{
		m_num = 0;
	}
	int m_num;
};

ostream& operator<<(ostream& cout, myinteger myint)
{
	cout << myint.m_num << endl;
	return cout;
}

class Mydouble
{
public:
	Mydouble()
	{
		m_value = 5;
	}
	Mydouble& operator--()   //前置减减
	{
		m_value--;
		return *this;
	}
	Mydouble operator--(int)  //后置减减
	{
		Mydouble temp;
		temp = *this;
		m_value--;
		return temp;
	}
	double m_value;
};
ostream& operator<<(ostream& cout, Mydouble mydouble)
{
	cout << mydouble.m_value << endl;
	return cout;
}

int main()
{
	myinteger int1;
	++int1; //想让对象里面的值+1,或者当对象里面有许多属性时,控制具体哪些属性+1,这就是++运算符重载的主要作用
	cout << int1 << endl;
	cout << int1++;
	cout << int1;

	Mydouble double1;
	double1--;
	cout << double1;
	--double1;
	cout << double1;
	
}

4.赋值运算符重载

编译器提供的赋值是浅拷贝操作,当有数据在堆区的时候就会出现错误,这个详解可在构造函数那篇文章里面看到
文章
为了解决这个问题,提供了赋值运算符重载这个函数。

#include
using namespace std;

class person
{
public:
	int m_height;
	int* m_age;

	person(int age, int height)
	{
		m_age = new int(age);
		m_height = height;
	}

	person& operator=(const person& p1)    //返回自身是为了让它能够进行连等赋值操作  p1=p2=p3;
	{
		if (m_age == NULL)
		{
			delete(m_age);
			m_age = NULL;
		}
		m_age = new int(*p1.m_age);
		m_height = m_height;
		return *this;
	}
};

int main()
{
	person p1(18, 168);
	person p2 = p1;
	cout << *p2.m_age <<"   "<< p2.m_height;
	return 0;
}

5.关系运算符重载

为了比较两个自定义数据类型的大小
重载==,为了比较两个对象是否相同,即判断两个对象里面的各个属性相同。

#include
using namespace std;

class person
{
public:
	person(int age, int height)
	{
		m_age = age;
		m_height = height;
	}
	int m_age;
	int m_height;

	bool operator==(person& p1)   //bool代数
	{
		if (this->m_age == p1.m_age && this->m_height == p1.m_height)
			return true;
		else
		{
			return false;
		}
	}
};
int main()
{
	person p1(18, 170);
	person p2(18, 170);
	if (p1 == p2)
	{
		cout << "p1 and p2 is equal" << endl;
	}
	else
	{
		cout << "p1 and p2 is unequal" << endl;
	}
}



你可能感兴趣的:(c++,c++)