Combination Vector class Vector, a Matrix class template declaration and implementation, said Matrix, the Matrix class template include:
Rows, a Vector type of private data members, depositing matrix element,
Constructor, the matrix of row and column size is set to the given parameters,
Constructor, the matrix of row and column size is set to the given parameters, give it an initial values of the same matrix element,
[], overloaded subscript operator,
GetRows accessor functions, used to get matrix rows,
GetColumns accessor function, matrix is used to get the number of columns,
Matrix class template is as follows:
The template & lt; Typename T>
The class Matrix {
Public:
Matrix (int row, int column);
Matrix (int row, int column, const T & amp; Value);
Vector
Vector
Const int getRows ();
Const int getColumns ();
Private:
Vector
};
In the form of ordinary function overloading the * operator functions, realizes the matrix multiplication,
The template & lt; Typename T>
Matrix
PrintMatrix function output value of the matrix,
The template & lt; Typename T>
Void printMatrix (const Matrix
[enter]
Enter 3 * 3 matrix and matrix b,
[output]
Matrix multiplied by a matrix of b as a result, each element output width is 4,
[input sample]
1 1 7
5 6 7
9 June 1
6 1
4 5 1
5 1
[example] output
17 and 18
68 42 73
79 20 85
# include & lt; Iostream>
#include
#include
using namespace std;
The template & lt; Typename T>
Class Vector {
Public:
The Vector (int size);//the constructor
The Vector (int size, const T& Value);//the constructor
The Vector (const Vector& V);//copy constructor
Virtual ~ Vector ();//destructors
Const Vector& Operator=(const Vector & Right);//overloaded assignment operator
T& Operator [] (int index);//overloaded subscript operator
T operator [] (int index) const;//overloaded subscript operator
Const int getSize ();
Void the resize (int size);
Private:
T * pVector;//pointer to storage array element of dynamically allocated memory space
Int size;//array length
};
The template & lt; Typename T>
Vector: : Vector (int size) {
If (size & gt; 0)
This - & gt; Size=size;
The else
Throw invalid_argument (" the length of the array must be positive integer!" );
PVector=new T [size];
}
The template & lt; Typename T>
Vector: : Vector (int size, const T& Value) {
If (size & gt; 0)
This - & gt; Size=size;
The else
Throw invalid_argument (" the length of the array must be positive integer!" );
PVector=new T [size];
for (int i=0; iPVector [I]=value;
}
The template & lt; Typename T>
Vector: : Vector (const Vector & V) {
Size=v. considering;
PVector=new T [size];
for (int i=0; iPVector [I]=Valerie plame Vector [I];
}
The template & lt; Typename T>
Vector: : ~ Vector () {
The delete [] pVector;
}
The template & lt; Typename T>
Const Vector& Vector : : operator=(const Vector & Right) {
If (this!=& amp; Right) {
If (the size!=right. The size) {
The delete [] pVector;
Size=right. The size;
PVector=new T [size];
}
for (int i=0; iPVector [I]=right. PVector [I];
}
return *this;
}
The template & lt; Typename T>
T& Vector: : operator [] (int index) {
If (index & lt; 0 | | index & gt; The size 1)
Throw out_of_range (" array subscript is beyond the scope allowed!" );
Return pVector [index];
}
The template & lt; Typename T>
T Vector: : operator [] (int index) const {
If (index & lt; 0 | | index & gt; The size 1)
Throw out_of_range (" array subscript is beyond the scope allowed!" );
Return pVector [index];
}
The template & lt; Typename T>
Int Vector: : getSize () const {
Return the size;
}
The template & lt; Typename T>
Void Vector: : resize (int size) {
If (size & gt; 0 {
If (this - & gt; The size!={
the size)T * old=pVector;
PVector=new T [size];
Int newSize=(this - & gt; The size & gt; The size)? Size: this - & gt; The size;
for (int i=0; iPVector [I]=old [I];
This - & gt; Size=size;
The delete [] old;
}
}
The else
Throw invalid_argument (" the length of the array must be positive integer!" );
}
/* please separately written here Matrix class, overloaded operator * function, printMatrix function */
Int main () {
Const int ROW_SIZE=3;
Const int COLUMN_SIZE=3;
MatrixA (ROW_SIZE, COLUMN_SIZE);
MatrixB (ROW_SIZE, COLUMN_SIZE);
for (int i=0; iFor (int j=0; J & lt; COLUMN_SIZE; + + j) {
Cin & gt;> A, [I] [j].
}
}
for (int i=0; iFor (int j=0; J & lt; COLUMN_SIZE; + + j) {
Cin & gt;> B [I] [j];
}
}
PrintMatrix (a * b);
return 0;
}