013--C++运算符重载(+,-,*,前后++,>>,<<)(最优)

简介:(注意前后++的写法,以及输入输出重载的写法,关键在于什么时候用&引用以及什么时候const.由于目前不知道该怎样解释,就暂且落下,自己去多多琢磨。)

运算符重载:在类或结构中,对运算符进行重新的定义,让这个运算符能适应这个类型的操作。


代码学习:(写了更多注释,方便理解)

//运算符重载
#include<iostream>
using namespace std;
class MyPoint{
	int x;
	int y;
public:
	MyPoint(){};
	MyPoint(int x,int y){
		this->x = x;
		this->y = y;
	};
	~MyPoint(){};
   //实现两个点的矢量相加
	MyPoint operator+(const MyPoint& srcPoint){//运算符重载实现两个坐标相加 
		MyPoint temp;
		temp.x = this->x + srcPoint.x;
		temp.y = this->y + srcPoint.y;
		return temp;
	}
	MyPoint operator-(const MyPoint& srcPoint){//运算符重载实现两个坐标相加 
		MyPoint temp;
		temp.x = this->x - srcPoint.x;
		temp.y = this->y - srcPoint.y;
		return temp;
	}//运算符重载一般this 算一个参数,所以可以少写一个参数,如果非要多
	//写这个参数,就用友元,不属于类
	//对于双目运算符来说,因为类中的函数自带this指针,所以只需要一个参数。如果只想
	//两个参数的话,那么可以在函数前加上friend.
	friend MyPoint operator*(const MyPoint& p1, const MyPoint& p2);
	MyPoint& operator++(){//前加加
		this->x++;
		this->y++;
		return *this;//this 是指针,*(this)解引用就是一个对象
	}
	MyPoint operator++(int){//后加加
		MyPoint temp = *this;
		this->x++;
		this->y++;
		return temp;
	}
	friend istream& operator>>(istream& is,MyPoint& src){
		is >> src.x;//输入流重载
		is >> src.y;
		return is;
	}
	friend ostream& operator<<(ostream& os,const MyPoint& src){
		os << "(" << src.x << "," << src.y << ")" << endl;
		return os;//输出流重载
	}
	void getPoint(){
		cout << "(" << x << "," << y << ")" << endl;
	}
};
MyPoint operator*(const MyPoint& p1, const MyPoint& p2){
	MyPoint temp;
	temp.x = (p1.x)*(p2.x);
	temp.y = (p1.y)*(p2.y);
	return temp;
}
int main(){
	cout << "运算符重载测试:初始坐标m1(1,2),m2(2,3):" << endl;
	MyPoint m1(1,2);
	MyPoint m2(2,3);
	cout << "----------------------------" << endl;
	cout << "运算符重载相加:" << endl;
	MyPoint m=m1 + m2;//隐式调用运算符重载+
	m.getPoint();
	cout << "----------------------------" << endl;
	cout << "运算符重载相减:" << endl;
	MyPoint m3;
	m3 = m1.operator-(m2);//显示调用运算符重载-
	m3.getPoint();
	cout << "----------------------------" << endl;
	cout << "运算符重载相乘:" << endl;
	MyPoint m4 = m1*m2;
	m4.getPoint();
	cout << "----------------------------" << endl;
	cout << "运算符重载前++:" << endl;
	cout << (++m2) << endl;
	cout << "----------------------------" << endl;
	cout << "运算符重载后++:" << endl;
	cout <<(m2++)<< endl;
	cout << "----------------------------" << endl;
	cout << "运算符重载输入:" << endl;
	MyPoint m7;
	cin >> m7;
	cout << "运算符重载输出:" << endl;
	cout << m7 << endl;
	cout << "----------------------------" << endl;
	return 0;

}

这辈子没啥多的想法,多赚钱娶个漂亮老婆·········加油··········

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的复数类Complex,重载了+,-,*,/,<<,>>等运算符的示例代码: ```c++ #include <iostream> using namespace std; class Complex { public: Complex(double real = 0, double imag = 0) : m_real(real), m_imag(imag) {} Complex(const Complex& other) : m_real(other.m_real), m_imag(other.m_imag) {} Complex operator+(const Complex& other) const { return Complex(m_real + other.m_real, m_imag + other.m_imag); } Complex operator-(const Complex& other) const { return Complex(m_real - other.m_real, m_imag - other.m_imag); } Complex operator*(const Complex& other) const { return Complex(m_real * other.m_real - m_imag * other.m_imag, m_real * other.m_imag + m_imag * other.m_real); } Complex operator/(const Complex& other) const { double denominator = other.m_real * other.m_real + other.m_imag * other.m_imag; return Complex((m_real * other.m_real + m_imag * other.m_imag) / denominator, (m_imag * other.m_real - m_real * other.m_imag) / denominator); } friend ostream& operator<<(ostream& os, const Complex& c) { os << "(" << c.m_real << ", " << c.m_imag << ")"; return os; } friend istream& operator>>(istream& is, Complex& c) { is >> c.m_real >> c.m_imag; return is; } private: double m_real; double m_imag; }; int main() { Complex c1(1, 2); Complex c2(3, 4); cout << "c1 = " << c1 << endl; cout << "c2 = " << c2 << endl; cout << "c1 + c2 = " << c1 + c2 << endl; cout << "c1 - c2 = " << c1 - c2 << endl; cout << "c1 * c2 = " << c1 * c2 << endl; cout << "c1 / c2 = " << c1 / c2 << endl; Complex c3; cout << "Input a complex number: "; cin >> c3; cout << "The complex number you input is: " << c3 << endl; return 0; } ``` 在上述代码中,我们重载了Complex类的+,-,*,/,<<,>>等运算符,并利用了友元函数的特性来重载输出运算符<<和输入运算符>>。在main函数中,我们对重载后的运算符进行了测试,输出了两个复数的和、差、积、商以及用户输入的复数。 希望这个示例代码对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码字界陈冠希

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值