筆記:插入提取運算符的友元函數重載。
#include<iostream>
using namespace std;
class Complex{//為什麼使用運算符重載
int m_real;//解決複雜類型
int m_vir;
friend istream& operator>>(istream& in,Complex& c1);//插入運算符
friend ostream& operator<<(ostream& out,Complex& c1);//提取運算符
public:
Complex(int real=0,int vir=0):m_real(real),m_vir(vir){
}
};
istream& operator>>(istream& in,Complex& c1){//友元重載插入運算符
in>>c1.m_real>>c1.m_vir;
return in;
}
ostream& operator<<(ostream& out,Complex& c1){//友元重載提取運算符
out<<c1.m_real<<"+"<<c1.m_vir<<"i"<<endl;
return out;
}
int main(){
Complex cp1;
cin>>cp1;
cout<<cp1;
getchar();
system("pause");
return 0;
}