欢迎来到贱贱的博客

扩大
缩小

c++重载输入输出运算符

1 最好打断点看看哦

2例子

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Complex2
 6 {
 7 public:
 8 
 9     Complex2(float x = 0, float y = 0)
10         :_x(x), _y(y){
11 
12         
13 
14     }
15     void dis()
16     {
17         cout << "(" << _x << "," << _y << ")" << endl;
18     }
19     friend  ostream & operator<<(ostream  &os, const Complex2 &c);
20     friend  istream & operator>>(istream  &is, Complex2 &c);
21 
22 private:
23     float _x;
24     float _y;
25 };
26 
27 ostream &operator<<(ostream  &os, const Complex2 &c)
28 {
29     os << "(" << c._x << "," << c._y << ")";
30     return os;
31 }
32 
33 istream & operator>>(istream  &is, Complex2 &c)
34 {
35     is >> c._x >> c._y;
36     return is;
37 }
38 int main()
39 {
40     Complex2 c(2, 3);
41     cout << c << endl;
42     cin >> c;
43     cout << c << endl;
44     cin.get();
45     return 1;
46 }

 

posted on 2017-08-07 10:53  L的存在  阅读(2492)  评论(0编辑  收藏  举报

导航