1 #include2 template 3 class Matrix 4 { 5 public: 6 Matrix(int r=0,int c=0); 7 Matrix(Matrix &m); 8 ~Matrix() 9 {10 delete []melem;11 }12 void input();13 void print();14 T & operator ()(int i,int j);15 void transmat(Matrix &,Matrix &);16 private:17 int rows,cols;18 T *melem;19 };20 template 21 Matrix ::Matrix(int r,int c)22 {23 rows=r;24 cols=c;25 melem=new T[r*c];26 }27 template 28 Matrix ::Matrix(Matrix &m) //构造函数29 {30 rows =m.rows;31 cols =m.cols;32 melem =new T[rows*cols];33 for(int i =0 ; i 37 T &Matrix ::operator () (int i,int j) //取元素运算38 {39 return melem [(i-1)*cols +j-1];40 }41 42 template 43 void Matrix ::input()44 {45 for(int i=0;i >melem [i];47 }48 template 49 void Matrix ::print()50 {51 for (int i=0;i 60 void Matrix ::transmat(Matrix &a, Matrix &b)61 {62 int i,j;63 for(i=1;i<=a.rows;i++)64 for(j=1;j<=a.cols;j++)65 {66 b(j,i)=a(i,j);67 }68 }69 int main()70 {71 Matrix A(3,2) ,B(2,3) ;72 A.input();73 cout<<"A:";74 A.print();75 A.transmat(A,B);76 cout< <<"B:";77 B.print();78 cout<