C 過載流插入和流輸出運算子

2022-08-30 16:27:14 字數 978 閱讀 2235

demo:

/*

name: 過載輸入輸出流運算子使力**

author: zhaozhe

date: 07/12/13 00:11

description:

首先定義乙個複數類,然後過載輸入,輸出運算子

以及+運算子,以實現複數的輸出,相加和輸出操作

*/#includeusing namespace std;

class complex

complex(double r,double i)

friend complex operator+(complex &c1,complex &c2);

friend ostream & operator << (ostream &output,complex &c);

friend istream & operator >> (istream &input ,complex &c);

private:

double real;

double imag;

};complex operator+(complex &c1,complex &c2)

ostream & operator << (ostream &output,complex &c)

int main()

{ complex c1,c2,c3;

cout<<"please input c1:";

cin>>c1;

cout<<"please input c2:";

cin>>c2;

c3=c1+c2;

cout<<"c3=c1+c2"<

/*please input c1:3 5

please input c2:2 -1

c3=c1+c2

c1:(3,5)

c2:(2,-1)

c3=c1+c2

c3:(5,4)

*/

過載流插入運算子《和流提取運算子》

includeclass complex complex double r,double i complex operator complex c2 friend ostream operator ostream output,complex c private double real double...

過載流插入運算子和流提取運算子

有兩個矩陣a和b,均為2行3列。求兩個矩陣之和。過載運算子 使之能用與矩陣相加,如c a b。過載流插入運算子 和流提取運算子 使之能用與該矩陣的輸入和輸出,如cin a,cout 我是剛學習c de菜鳥 源 如下 include using namespace std class matrix m...

關於過載流輸入輸出運算子

盡可能簡潔清晰的向同我一樣的小白們寫範例以便於理解。以下為單獨的流輸出過載 include include using namespace std class t 建構函式,並賦初始值 private double aa double bb ostream operator ostream outp...