《模版实现矩阵综合运算》由会员分享,可在线阅读,更多相关《模版实现矩阵综合运算(5页珍藏版)》请在金锄头文库上搜索。
1、 模版实现矩阵综合运算1程序代码#include #include #include #include using namespace std; namespace NameVector #define DEFAULT_ALLOC 2 templateclass matrix; template class vector private: friend class matrix; EIType * data; int len; public: int length()constreturn len; vector(); vector(int n); vector()delete data; v
2、ector(const vector vector EIType void zero(); vectoroperator+(const vector friend ostream friend istream ; template void vector:zero() for(int i=0;iEIType assert(data!=0);len=DEFAULT_ALLOC; template vector:vector(int n) data=new EITypelen=n; assert(data!=0); template vector:vector(const vector asser
3、t(data!=0); for(int i=0;i vector data=new EITypelen=original.len; assert(data!=0); for(int i=0;i vectorvector:operator+(const vector for(int i=0;i ostream return is; template class matrix vector *m; int rows,cols; public: matrix(); matrix(int r,int c); matrix(const matrix matrix()deletem; matrix vec
4、tor matrixoperator+(const matrix friend ostream friend istream ; template matrix:matrix() m=new vectorDEFAULT_ALLOC; assert(m!=0); rows=cols=DEFAULT_ALLOC; for(int i=0;iv; mi=v; template matrix:matrix(int r,int c) m=new vectorv(cols); mi=v; template matrix:matrix(const matrix rows=s.rows; m=new vect
5、orrows; assert(m!=0); cols=s.cols; for(i=0;i matrix rows=s.rows; cols=s.cols; m=new vectorrows; assert(m!=0); for(int i=0;i vector assert(rows=a.rows); assert(cols=a.cols); matrixans(a.rows,a.cols); for(i=0;i ostreami istream return is; using namespace NameVector; int main() vectorvec1(1),vec2(2),vec3; cinvec1;cinvec2; vec3=vec1+vec2; coutvec3; return 0; 2结果截图