如何重载输入输出流(<<、>>)
来源:互联网 发布:linux新建 删除文件夹 编辑:程序博客网 时间:2024/05/16 18:57
如何重载输入输出流?
Description
有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加(如c=a+b)。
重载流插入运算符“<<”和流提取运算符“>>”,使之能用于该矩阵的输入和输出。
Input
两个2行3列矩阵
Output
矩阵之和
Sample Input
1 2 3
4 5 6
7 8 9
1 2 3
Sample Output
8 10 12
5 7 9
#include <iostream>using namespace std;class Matrix{public: Matrix(); friend Matrix operator+(Matrix &,Matrix &); friend ostream& operator<<(ostream&,Matrix&); friend istream& operator>>(istream&,Matrix&);private: int mat[2][3];};Matrix::Matrix(){int i,j;for(i=0;i<2;i++)for(j=0;j<3;j++)mat[i][j]=0;}Matrix operator+(Matrix &a,Matrix &b){Matrix c;int i,j; for(i=0;i<2;i++) for(j=0;j<3;j++) c.mat[i][j]=a.mat[i][j]+b.mat[i][j];//遗漏点:.mat return c;//}ostream& operator<<(ostream &output,Matrix &c)//ostream类对象output{int i,j;for(i=0;i<2;i++){for(j=0;j<3;j++){output<<c.mat[i][j];//.matif(j!=2) output<<' ';}output<<endl;}return output;//返回}istream& operator>>(istream &in,Matrix &c) //istream类对象in{int i,j;for(i=0;i<2;i++)for(j=0;j<3;j++)in>>c.mat[i][j];return in;//返回} int main(){ Matrix a,b,c; cin>>a; cin>>b; c=a+b; cout<<c<<endl; return 0;}
0 0
- 如何重载输入输出流(<<、>>)
- 重载输入输出流
- 输入输出流运算重载模板
- 重载输入输出流:Overload Ostream And Istream
- 重载运算符和输入输出流
- 【模板类中重载输入输出流 】
- C++重载运算符和输入输出流
- C++ 流输入输出运算符重载
- c++中的输入输出流重载函数实现
- 如何理解输入输出流
- 重载输入输出操作符
- 重载之输入输出操作
- 输入输出运算符重载
- 重载输入输出操作符
- 重载输入输出运算符
- 输入输出操作符重载
- 输入输出运算符重载
- 输入输出运算符重载
- SSH深度历险(四) Maven初步学习
- 在struts1框架下用jQuery进行异步提交出现缺少method属性的异常
- 【闲聊产品】之四:代码的万劫不复
- MySQL数据恢复过程
- Linux下批量杀掉包含某个关键字的程序进程
- 如何重载输入输出流(<<、>>)
- ARM体系结构与编程-2
- 使用BabeLua在Quick-cocos2d-x中编辑和调试Lua
- jQuery基础----选择器,过滤器
- 单例模式
- 第2次实验——算法基本功 与 综合思考
- ReportStudio入门教程(四十一) - 固定报表列宽
- TCP滑动窗口
- Java数据结构与算法之集合