c++运算符重载

#include<iostream>

/*
C++的运算符重载:使对象的运算表现得和编译器内置类型一样

*/
class CComplex
{
public:
	CComplex(int r = 0, int i = 0) :m_real(r), m_imaginary(i)
	{

	}
	//指导编译器怎么做CComplex类对象的加法操作
	CComplex operator+(const CComplex& src)
	{
		CComplex comp;
		comp.m_real = this->m_real + src.m_real;
		comp.m_imaginary = this->m_imaginary + src.m_imaginary;
		return comp;			
	}
	//前置++
	CComplex operator++(int)
	{
		/*CComplex comp = *this;
		m_real += 1;
		m_imaginary += 1;
		return comp;*/
		return CComplex(m_real++, m_imaginary++);
	}
	//后置++
	CComplex& operator++()
	{
		m_real += 1;
		m_imaginary += 1;
		return *this;
	}

	void operator+=(const CComplex& src)
	{
		m_real += src.m_real;
		m_imaginary += src.m_imaginary;
		

	}
	void show()
	{
		std::cout << "real:" << m_real << "  imaginary:" << m_imaginary << std::endl;
	}
private:
	int m_real;
	int m_imaginary;
	friend  CComplex operator+(const CComplex& lhs, const CComplex& rhs);//友元函数:让全局CComplex operator+(const CComplex& lhs, const CComplex& rhs) 访问CComplex私有成员变量
	friend std::ostream& operator<<(std::ostream& out, const CComplex& src);//友元函数
	friend std::istream& operator>>(std::istream& in, CComplex& src);
};
CComplex operator+(const CComplex& lhs, const CComplex& rhs)
{
	return CComplex(lhs.m_real + rhs.m_real, lhs.m_imaginary + rhs.m_imaginary);
}
//重载cout<<输出
std::ostream& operator<<(std::ostream& out, const CComplex& src)
{
	out << "real" << src.m_real << " image:" << src.m_imaginary;
	return out;
}
//重载cin>>输入
std::istream& operator>>(std::istream& in, CComplex& src)
{
	in >> src.m_real >> src.m_imaginary ;
	return in;
}
int main()
{
	CComplex comp1(10, 10);
	CComplex comp2(20, 20);
	//comp1.operator+(comp2) 加法运算符的重载函数
	CComplex comp3 = comp1 + comp2;
	comp3.show();

	CComplex comp4 = comp1 + 20;//comp1.operator(20)  int=>CComplex  CComplex(int)
	comp4.show();

	//如果没有定义全局CComplex operator+(const CComplex& lhs, const CComplex& rhs) 编译报错:error C2677: 二进制“+”: 没有找到接受“CComplex”类型的全局运算符(或没有可接受的转换)
	// 编译器做对象的运算的时候,会调用对象的运算符重载函数(优先调用成员方法)
	// 如果没有成员方法,就在全局作用域找合适的运算符重载函数
	// ::operator+(30,comp1)
	//如果定义全局CComplex operator+(const CComplex& lhs, const CComplex& rhs),下面代码将正确编译并运行
	CComplex comp5 = 30+comp1;
	comp5.show();

    //++ -- 单目运算符  operator++() 前置++ 而 operator++(int) 后置++
	std::cout << "---------后置comp1++;结果如下-----" << std::endl;
	comp5 = comp1++;
	comp1.show();
	comp5.show();


	std::cout << "---------前置++comp1;结果如下-----" << std::endl;
	comp5 = ++comp1;
	comp1.show();
	comp5.show();
   
	//void comp1.operator+=(comp2)  

	std::cout << "---------comp1 += comp2-----" << std::endl;
	comp1 += comp2;
	//comp1.show();//对象信息的输出
	std::cout << comp1 << std::endl;
	// std::cout为什么不用成员方法,因为成员方法是需要对象方左边,而std::cout对象右边
	// 所以需要以全局方式
	// cout  ::operator<<(cout,comp1)
	// ostream& operator<<(ostream & out,const CComplex &src)
	//std::cout<<comp1<<std::endl;

	std::cout << "---------cin >> comp1 >> comp2-----" << std::endl;
	std::cin >> comp1 >> comp2;
	std::cout << comp1 << comp2 << std::endl;


	
	return 0;
}

输出如下:

real:30  imaginary:30
real:30  imaginary:10
real:40  imaginary:10
---------后置comp1++;结果如下-----
real:11  imaginary:11
real:10  imaginary:10
---------前置++comp1;结果如下-----
real:12  imaginary:12
real:12  imaginary:12
---------comp1 += comp2-----
real32 image:32
---------cin >> comp1 >> comp2-----
1 1
2 2
real1 image:1real2 image:2
posted @ 2025-10-03 22:45  焦涛  阅读(9)  评论(0)    收藏  举报