7.12流插入运算符“<<"和流提取运算符”>>“
2015-06-27 17:37
302 查看
#include<iostream> #include<iomanip> using namespace std; class Matrix{ private: int var[2][4]; public: Matrix(); //构造函数 friend Matrix operator+(Matrix& a, Matrix& b); //加法友元函数 friend ostream& operator<<(ostream&, Matrix&); //声明运算符“<<”重载为友元函数 friend istream& operator>>(istream&, Matrix&); //声明运算符“>>”重载为友元函数 }; Matrix::Matrix() { for (int i=0;i<2;i++) for (int j = 0; j < 4; j++) var[i][j] = 0; } Matrix operator+(Matrix &k, Matrix& p) //加法函数 { Matrix temp; for (int i = 0; i < 2;i++) for (int j = 0; j < 4; j++) temp.var[i][j] = k.var[i][j] + p.var[i][j]; return temp; } ostream& operator<<(ostream& out, Matrix& com) //定义运算符“<<”重载函数 { for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++) out <<setw(5)<< com.var[i][j]; out << endl; } out << endl; return out; } istream& operator>>(istream& in, Matrix& com) //定义运算符“>>”重载函数 { cout << "please input 8 numbers:"; for (int i = 0; i < 2; i++) { for (int j = 0; j < 4; j++) in>>com.var[i][j]; } return in; } int main() { Matrix a, b,c; cin >> a; cin >> b; cout << "Matrix a is:" << endl; cout << a; cout << "Matrix b is:" << endl; cout << b; c = a + b; cout << "Matrix a+b is:" << endl; cout << c; system("pause"); return 0; }
相关文章推荐
- 十分钟搞定pandas
- 应用MVP模式写出可维护的优美Android应用
- 关于保存文件
- Safe site for swtor money with news about SWTOR Exploit
- 比较JS合并数组的各种方法及其优劣
- 基于 CoreText 的排版引擎:进阶
- CSS3系列四(Media Queries移动设备样式)
- 用于数组的delete p324
- 笔记114--记录登陆界面的实现
- hdu 1873 看病要排队
- CSS/JQuery元素选择器之&&和||,选择器的逻辑操作
- 字符串基本处理算法
- DIV横向排列_CSS如何让多个div盒子并排同行显示
- (转)内联(inline)函数与虚函数(virtual)的讨论
- Step by Step:Linux C多线程编程入门(基本API及多线程的同步与互斥)
- awk调用系统命令
- 各种排序算法的分析及java实现
- python的logging模块详解
- POJ3253
- MacBook 连接Centos服务器