C++学习总结(十四)——类的运算符的重载原理,以及重载的实例

一.运算符重载原理

1.运算符重载,对运算符做出新的解释。

2.原有的基本语义不发生变化(优先级,结合性,操作数),且不能创建新的运算符。

3.运算符可以重载为友元函数和成员函数,友元函数用于不同类的数据类型相加。

二.重载实例

1.输入输出流的重载

输入输出重载需要定义为友元,并且引用流返回流。

#include
class fushu
{
public:
	int x=0;
	int y=0;
	fushu(int a,int b):x(a),y(b)
	{
	}
	~fushu()
	{
	}
	friend std::istream &operator >>(std::istream &in, fushu &myfushu);
	/*{
		std::cout << "myfushu.x=";
		in >> myfushu.x;
		std::cout << "myfushu.y=";
		in>> myfushu.y;
		return in;
	}*/
	friend std::ostream &operator <<(std::ostream &out,fushu &myfushu)
	{
		out << myfushu.x<<'+'<x++;
		this->y++;
	}
	void operator --()
	{
		this->x--;
		this->y--;
	}
	void operator ()(int num);

};
void fushu::operator ()(int num)
{
	std::cout << num<>(std::istream &in, fushu &myfushu)
{
std::cout << "myfushu.x=";
in >> myfushu.x;
std::cout << "myfushu.y=";
in>> myfushu.y;
return in;
}

void main2()
{
	fushu complex(1, 2);
	complex.show();
	complex(5);//括号的重载
	std::cin >> complex;//输入流的重载,用于输入类
	std::cout << complex;//输出流的重载,输出类
	std::cout << "123";
	++complex; //自增自减的重载
	--complex;
	std::cout << complex;
	std::cin.get();
	std::cin.get();
}
void main1()
{
	int a(1);
	int b(2);
	int c = a + b;
	char ch1[10] = "123";
	char ch2[10] = "456";
	char ch3[50] = {0};
	sprintf_s(ch3, "%s%s", ch1, ch2);

	std::cout << ch3;
	std::cin.get();
}
2.二元运算符的重载

#include
class MyClass
{
public:
	int a;
	int b;
public:
	MyClass(int num);
	~MyClass();
	void operator ()();
	void operator ++();//++a 先自增后引用
	void operator ++(int);//a++先引用后自增
	void operator --();
	/*MyClass operator +(MyClass adddata);*/
	friend MyClass operator +(MyClass add1,MyClass add2);
	friend MyClass operator +(MyClass adddata1, int x);


private:
	int x;
	int y;

};

MyClass::MyClass(int num)
{
	a = b = x = y = num;
}

MyClass::~MyClass()
{
}
void MyClass::operator()()
{
	std::cout << x <<' '<< y<<' ' << a<<' ' << b << std::endl;
}
void MyClass::operator ++()
{
	x++;
	y++;
	a++;
	b++;
}
void MyClass::operator ++(int)
{
	//MyClass temp(0);
	x++;
	y++;
	a++;
	b++;

}
void MyClass::operator --()
{
	x--;
	y--;
	a--;
	b--;
}
//MyClass MyClass::operator +(MyClass adddata)
//{
//	MyClass temp(0);
//	temp.x=this->x + adddata.a;
//	temp.y=this->y + adddata.b;
//	return temp;
//}
//二元运算符重载
MyClass operator +(MyClass add1, MyClass add2)
{
	MyClass temp(0);
	temp.x = add1.x + add2.x;
	temp.y = add1.y + add2.y;
	temp.a = add1.a + add2.a;
	temp.b = add1.b + add2.b;
	return temp;
}
MyClass operator +(MyClass adddata1, int x)
{
	MyClass temp(0);
	temp.x = adddata1.x + x;
	temp.y = adddata1.y + x;
	temp.a = adddata1.a + x;
	temp.b = adddata1.b + x;
	return temp;
}

void main4()
{
	MyClass m(10),mm(20),mmm(0);
	mmm = m + mm;
	mmm();
	mmm = m + 5;
	mmm();
	++m;
	m();
	--m;
	m();
	mm++;
	mm();


	std::cin.get();
}
3.自增与自减的重载的前置与后置,以及下标的重载。

#include
class m
{
public:
	int a = 0;
	int b = 0;
	m *p[10];

public:
	m()
	{
		p[5] = new m;

	}
	m *operator ++()// 前置
	{
		this->a += 1;
		this->b += 1;
		return this;
	}

	m *operator ++(int)//后置
	{
		m *p = new m;
		p->a = this->a;//保存当前值
		p->b = this->b;
		this->a += 1;//然后自增
		this->b += 1;
		return p;										  
	}
	m *operator [](int i)
	{
	    p[i]->a=100;
		return p[i];
	}
};

void main()
{
	m m1,m2;
	m1++;
	std::cout<a;

	
	std::cin.get();
}




你可能感兴趣的:(C++,C++学习总结)