运算符重载+-<< >>

运算符重载
一元运算符*
二元运算符+ - = && || <<  >>
三元运算符  :#include<string.h>
#include<iostream>
using namespace std;
class Complex
{
public:
	Complex(int a, int b)
	{
		_a = a;
		_b = b;
	}
	Complex operator+(const Complex& src)const
	{
		int a = _a + src._a;
		int b = _b + src._b;
		return Complex(a, b);
	}
	Complex operator-(const Complex& src)const
	{
		int a = _a - src._a;
		int b = _b - src._b;
		return Complex(a, b);
	}
	bool operator&&(const Complex& src)const
	{
		return (_a && src._a);
		
	}
	void operator<<(ostream& out)//调用从c1.operator<<(cout);
	{
		out << _a << "+"<<_b<<endl;
	}
	friend ostream& operator<<(ostream& out, const Complex& src);
	friend istream& operator>>(istream& in, Complex& src);
	Complex& operator++()//前置++
	{
		++_a;
		return *this;
	}
	Complex operator++(int)
	{
		int tmp = _a;
		_a++;
		return Complex(tmp, _b);
	}
private:
	int _a;
	int _b;


};
ostream& operator<<(ostream& out, const Complex& src)//调用cout<<c1;
{
	out<< src._a << "+" <<src. _b << endl;
	return out;
}
istream& operator>>(istream& in, Complex& src)
{
	in >> src._a;
	in >> src._b;
	return in;
}
int main()
{
	string s1 = "aaaaaaa";
	string s2 = "bbbbbb";
	string s3 = s1 + s2;
	cout << s3 << endl;
	Complex c1(1, 2);
	Complex c2(2, 3);
	Complex c3 = c1+c2;//调用
	ostream& out = cout;
	istream& in = cin;
	if (c1 && c2)
	{
		cout << "all 0" << endl;
	}

	return 0;
}

输出流是ostream,输入流是istream,这也是两个类,并且只能有一个对象,因此在定义in和out时必须得加&(引用)。
输入输出流有两种方法,第一种调用不方便,是根据对象在调用,因为在类内有一个this指针,所以调用依赖对象,除了代码中方法调用,还可以用c1<<out,out带不带括号都可以,第二种是在类外实现无this指针,因此可以用简便的方法调用,out<<c1;当返回值是ostream的引用时还可以连着输出。但是得在类中声明该函数是他的朋友。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您解答。首先,运算符重载C++ 中非常重要的概念之一,它允许我们重载现有的运算符,以便我们可以对我们自己定义的类型执行特定的操作。在本例中,我们需要重载+和-运算符以实现加法和减法,同时需要重载<<和>>运算符以实现输入和输出。 下面是一个示例代码,可以帮助您更好地理解: ```c++ #include <iostream> using namespace std; class Complex { private: int real; int imag; public: Complex(int r = 0, int i = 0) { real = r; imag = i; } Complex operator+(Complex const &obj) { Complex res; res.real = real + obj.real; res.imag = imag + obj.imag; return res; } Complex operator-(Complex const &obj) { Complex res; res.real = real - obj.real; res.imag = imag - obj.imag; return res; } friend ostream &operator<<(ostream &out, const Complex &c); friend istream &operator>>(istream &in, Complex &c); }; ostream &operator<<(ostream &out, const Complex &c) { out << c.real; out << "+i" << c.imag << endl; return out; } istream &operator>>(istream &in, Complex &c) { cout << "Enter Real Part: "; in >> c.real; cout << "Enter Imaginary Part: "; in >> c.imag; return in; } int main() { Complex c1, c2; cout << "Enter first complex number:\n"; cin >> c1; cout << "Enter second complex number:\n"; cin >> c2; Complex c3 = c1 + c2; Complex c4 = c1 - c2; cout << "First Complex Number: " << c1; cout << "Second Complex Number: " << c2; cout << "Sum of Two Complex Numbers: " << c3; cout << "Difference of Two Complex Numbers: " << c4; return 0; } ``` 在上面的代码中,我们定义了一个名为Complex的类,其中包括两个私有变量real和imag,表示实部和虚部。我们还定义了两个运算符重载函数,分别用于实现加法和减法。在重载运算符函数中,我们使用“+”和“-”运算符来执行实际的加法和减法操作,并返回结果。此外,我们还定义了两个友元函数,用于重载输入和输出运算符。在这些函数中,我们使用“<<”和“>>”运算符来执行输入和输出操作。 最后,在主函数中,我们创建了两个复数对象c1和c2,并使用cin输入它们的值。然后,我们使用重载的加法和减法运算符计算它们的和和差,并将结果存储在c3和c4中。最后,我们使用重载的输出运算符将所有复数对象的值打印到屏幕上。 希望这个例子可以帮助您更好地理解C++运算符重载的概念。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值