⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 例10.8.txt

📁 是关于谭浩强老师的C++程序设计课程的程序源代码以及课件
💻 TXT
字号:
例10.8 在例10.7的基础上,增加重载流提取运算符“>>”,用“cin>>”输入复数,用“cout<<”输出复数。
#include <iostream>
using namespace std;
class Complex
{public:
friend ostream& operator << (ostream&,Complex&);    //声明重载运算符“<<”
friend istream& operator >> (istream&,Complex&);    //声明重载运算符“>>”
private:
double real;
double imag;
};
ostream& operator << (ostream& output,Complex& c)       //定义重载运算符“<<”
{output<<″(″<<c.real<<″+″<<c.imag<<″i)″;
return output;
}
istream& operator >> (istream& input,Complex& c)     //定义重载运算符“>>”
{cout<<″input real part and imaginary part of complex number:″;
input>>c.real>>c.imag;
return input;
}
int main( )
{Complex c1,c2;
cin>>c1>>c2; 
cout<<″c1=″<<c1<<endl; 
cout<<″c2=″<<c2<<endl;
return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -