C/C++ 重载+运算符重载加号 实现矩阵相加
2016-06-07 21:12
253 查看
#include<iostream> #include<iomanip> using namespace std; class Complex { private: int i,j,n,a[2][3]; public: Complex(); Complex operator+(Complex &c); void display(); void input(); } t1; Complex::Complex() { for(int i=0;i<2;i++) for(int j=0;j<3;j++) a[i][j]=0; } void Complex::input() { for(i=0;i<2;i++) for(j=0;j<3;j++) cin>>a[i][j]; } void Complex::display() { n=1; for(i=0;i<2;i++) for(j=0;j<3;j++) { n++; if(n%2==0) cout<<endl; cout<<setw(5)<<a[i][j]; } cout<<endl; } Complex Complex::operator+(Complex &c) { for(int i=0;i<2;i++) for(int j=0;j<3;j++) t1.a[i][j]=a[i][j]+c.a[i][j]; return t1; } int main() { Complex t2,t3,t4; cout<<"请输入2*3个整数"<<endl; t2.input(); cout<<endl; cout<<"请输入2*3个整数"<<endl; t3.input(); cout<<endl; cout<<"t2"; t2.display(); cout<<"t3"; t3.display(); t4=t2+t3; cout<<"t4=t2+t3="; t4.display(); return 0; }
相关文章推荐
- C_basic_summary--各种类型数据所占字节数
- Sliding Window Maximum
- 温习C/C++宏定义
- VC++ 数字图像处理第二章学习
- C++ 笔记5:LIST 和MAP
- leetcode 20 Valid Parentheses C++
- C/C++卡布列克运算验证
- C++ STL中Map的按Key排序和按Value排序
- 武器类属性
- C++智能指针(auro_ptr...)
- C++智能指针(auro_ptr...)
- C++ 线程池实现原理
- Invert Binary Tree
- ActiveMQ 与ActiveMQ-CPP
- C++ 笔记4:指针函数
- Product of Array Except Self
- C 语言中的宏
- Power of Two
- 在lldb调试中调用c++函数 - 如何使用QuartzCore里面的日志消息
- C语言中声明与定义的意义和使用方法