找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2237|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

重載流運(yùn)算符

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:109770 發(fā)表于 2016-3-22 20:02 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
本帖最后由 51黑bing 于 2016-3-22 20:03 編輯

#pragma once

#include "../inc/Matrix.h"
#include<iostream>

using namespace std;

class Matrix
{
public:
friend ostream& operator <<(ostream & out, Matrix& m);
friend istream& operator >>(istream & in, Matrix& m);
private:
int array[9];
};

-------------------------------------------------------------------

#include "../inc/Matrix.h"

---------------------------------------------------------------------------------------------------------
  1. #include <iostream>
  2. #include "../inc/Matrix.h"

  3. using namespace std;

  4. //定義全局函數(shù),并且聲明為Matrix的友元
  5. ostream& operator <<(ostream & out, Matrix& m)
  6. {
  7. cout << "數(shù)組的數(shù)據(jù)為:" << endl;
  8. int i = 0;
  9. for (; i < 3; i++)
  10. {
  11. out << m.array[i] << "  ";
  12. }
  13. cout << endl;
  14. for (i = 3; i < 6; i++)
  15. {
  16. out << m.array[i] << "  ";
  17. }
  18. cout << endl;
  19. for (i = 6; i < 9; i++)
  20. {
  21. out << m.array[i] << "  ";
  22. }
  23. cout << endl;
  24. return out;
  25. }
  26. istream& operator >>(istream & in, Matrix& m)
  27. {
  28. cout << "請(qǐng)輸入數(shù)組的數(shù)據(jù),每輸入一個(gè)請(qǐng)按回車按鍵確認(rèn)。" << endl;
  29. for (int i = 0; i < 9; i++)
  30. {
  31. in >> m.array[i];
  32. }
  33. return in;
  34. }

  35. int main()
  36. {
  37. Matrix m;
  38. cin >> m;
  39. cout << m;
  40. return 0;
  41. }
復(fù)制代碼






---------------------------------------------------------------------------------------------------------
結(jié)果:




分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表