C++重载<<和>>(输入输出运算符)

在C++中,系统已经对左移右移运算符分别进行了重载,使其能够用于输入输出,但是输入输出的处理对象只能是系统内建的数据类型。系统重载这两个运算符是以系统类成员函数的形式进行的,因此:

cout<<var;语句可以理解为:cout.operator<<(var);

如果自定义了一种新的数据类型,需要用输入输出运算符去处理,那么就要重载。

重载输入运算符:

下面用全局函数的形式重载输入运算符,使它能够读入两个double类型的数据,并转换为一个复数,保存到复数对象中:

istream & operator>>(istream & in, complex & A){
    in >> A.real >> A.imag;
    return in;
}
istream 是输入流,cin 就是 istream 类的对象。因为重载运算符函数需要用到 complex 类的 private 成员变量,为了方便,我们将这个函数声明为 complex 类的友元函数。声明形式如下:

friend istream & operator>>(istream & in , complex & a);

该函数可以按照如下方式使用:

complex c;
cin>> c;

当输入1.45 2.34后,这两个小数就分别成为 complex 对象 c 的实部和虚部了。cin>> c;这一语句其实可以理解为:

operator<<(cin , c);

在重载输入运算符时,采用引用的方式进行参数传递:输入的参数里面包含一个 istream 类的引用,返回值仍然为该引用。这样做的一个明显好处就是可以采用链式输入(也就是连续输入),如下所示:

complex c1, c2, c3;
cin>> c1 >> c2 >> c3;

重载输出运算符:

同样的,我们也可以模仿上面的方式对输出运算符进行重载,让它能够输出复数。函数在类内部的声明如下:
friend ostream &(ostream & out, complex & A);
全局函数的实现如下:
ostream & operator<<(ostream & out, complex & A){
    out << A.real <<" + "<< A.imag <<" i ";
    return out;
}
与 istream 相反,ostream 表示输出流,cout 就是 ostream 类的对象。为了能够直接访问 complex 类的私有成员变量,同样需要将这个函数声明为 complex 类的友元函数。由于采用了引用的方式进行参数传递,该输出运算符重载函数可以实现链式输出。


结合输入输出运算符重载,给出例子:
#include <iostream>
using namespace std;

class complex{
private:
	double real;  //复数的实部
	double imag;  //复数的虚部
public:
	complex() : real(0.0), imag(0.0){ };
	complex(double a, double b) : real(a), imag(b){ };
	friend complex operator+(const complex & A, const complex & B);
	friend complex operator-(const complex & A, const complex & B);
	friend complex operator*(const complex & A, const complex & B);
	friend complex operator/(const complex & A, const complex & B);
	friend istream & operator>>(istream & in, complex & A);
	friend ostream & operator<<(ostream & out, complex & A);
};

//重载加法运算符
complex operator+(const complex & A, const complex &B){
	complex C;
	C.real = A.real + B.real;
	C.imag = A.imag + B.imag;
	return C;
}

//重载减法运算符
complex operator-(const complex & A, const complex &B){
	complex C;
	C.real = A.real - B.real;
	C.imag = A.imag - B.imag;
	return C;
}

//重载乘法运算符
complex operator*(const complex & A, const complex &B){
	complex C;
	C.real = A.real * B.real - A.imag * B.imag;
	C.imag = A.imag * B.real + A.real * B.imag;
	return C;
}

//重载除法运算符
complex operator/(const complex & A, const complex & B){
	complex C;
	double square = A.real * A.real + A.imag * A.imag;
	C.real = (A.real * B.real + A.imag * B.imag) / square;
	C.imag = (A.imag * B.real - A.real * B.imag) / square;
	return C;
}

//重载输入运算符
istream & operator>>(istream & in, complex & A){
	in >> A.real >> A.imag;
	return in;
}

//重载输出运算符
ostream & operator<<(ostream & out, complex & A){
	out << A.real << " + " << A.imag << " i ";;
	return out;
}

int main(){
	complex c1, c2, c3;
	cin >> c1 >> c2;

	c3 = c1 + c2;
	cout << "c1 + c2 = " << c3 << endl;

	c3 = c1 - c2;
	cout << "c1 - c2 = " << c3 << endl;

	c3 = c1 * c2;
	cout << "c1 * c2 = " << c3 << endl;

	c3 = c1 / c2;
	cout << "c1 / c2 = " << c3 << endl;

	return 0;
}

结果:



  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值