C++ 输入/输出运算符重载
C++ 能够使用流提取运算符 >> 和流插入运算符 << 来输入和输出内置的数据类型。您可以重载流提取运算符和流插入运算符来操作对象等用户自定义的数据类型。
在这里,有一点很重要,我们需要把运算符重载函数声明为类的友元函数,这样我们就能不用创建对象而直接调用函数。
下面的实例演示了如何重载提取运算符 >> 和插入运算符 <<。
实例
#include<iostream>usingnamespacestd;
classDistance{private:
intfeet; // 0 到无穷intinches; // 0 到 12public:
// 所需的构造函数Distance(){feet = 0;
inches = 0;
}Distance(intf, inti){feet = f;
inches = i;
}friendostream &operator<<(ostream &output,
constDistance &D){output << "F : " << D.feet << " I : " << D.inches;
returnoutput;
}friendistream &operator>>(istream &input, Distance &D){input >> D.feet >> D.inches;
returninput;
}};
intmain(){DistanceD1(11, 10), D2(5, 11), D3;
cout << "Enter the value of object : " << endl;
cin >> D3;
cout << "First Distance : " << D1 << endl;
cout << "Second Distance :" << D2 << endl;
cout << "Third Distance :" << D3 << endl;
return0;
}
当上面的代码被编译和执行时,它会产生下列结果:
$./a.out Enter the value of object : 70 10 First Distance : F : 11 I : 10 Second Distance :F : 5 I : 11 Third Distance :F : 70 I : 10