C++重载<<和>>(输入输出运算符)
来源:互联网 发布:网络叫萝卜什么意思啊 编辑:程序博客网 时间:2024/06/05 07:23
在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;}
结果:
0 0
- 【C++】重载输入输出运算符
- C++重载<<和>>(输入输出运算符)
- 【C++】输入输出运算符的重载
- 重载运算符和输入输出流
- C++重载运算符和输入输出流
- 输入输出运算符重载
- 重载输入输出运算符
- 输入输出运算符重载
- 输入输出运算符重载
- 输入输出运算符重载
- C++输入输出运算符重载(“>>”“<<”)
- C++输入输出运算符重载(“>>”“<<”)
- C++输入输出运算符重载(“>>”“<<”)
- C++重载输入输出运算符
- C++输入输出运算符重载
- C++输入输出运算符重载
- C++输入输出运算符重载
- C++ 输入输出运算符重载
- 批量给多台Android手机安装APK脚本
- git删除本地仓库
- struts2工作原理
- 随语
- 【Android】图片模糊效果,Blur image with glide or picasso
- C++重载<<和>>(输入输出运算符)
- Android Fragment---给动作栏添加项目
- Android开发总结笔记 SearchView&Search(搜索相关) 1-3-7
- 3张图解决 Windows cmd dos 乱码问题
- 折腾:瀑布流布局(基于多栏列表流体布局实现)
- C++primer plus第六版课后编程练习答案5.4
- iOS屏幕旋转问题
- iOS开发——网络请求案例汇总
- 安装jad仍不能反编译的问题