Home > Back-end >  Help: combination of Vector class Vector, declare and implement a Matrix class template, said Matrix
Help: combination of Vector class Vector, declare and implement a Matrix class template, said Matrix

Time:09-20

Matrix is a kind of abstract objects in math, you can use c + + provides a static array of matrix and its size were fixed at compile time, cannot be modified at run time, and do not check whether the subscript crossing the line, can take advantage of the teaching material in this chapter provide the Vector class Vector, achieved through the method of Vector matrix, by a pointer to the Vector matrix, which each pointer is pointing to a Vector, and using them to represent the row Vector matrix, the matrix of logical structure as shown in the figure below,

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 & Operator [] (int index);
Vector Operator [] (int index) const;
Const int getRows ();
Const int getColumns ();
Private:
Vector * & gt; Rows;//storage matrix element
};
In the form of ordinary function overloading the * operator functions, realizes the matrix multiplication,
The template & lt; Typename T>
Matrix The operator * (const Matrix & LHS, const Matrix & RHS);

PrintMatrix function output value of the matrix,
The template & lt; Typename T>
Void printMatrix (const Matrix & M);
[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; i PVector [I]=value;
}
The template & lt; Typename T>
Vector : : Vector (const Vector & V) {
Size=v. considering;
PVector=new T [size];
for (int i=0; i PVector [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; i PVector [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; i PVector [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;
Matrix A (ROW_SIZE, COLUMN_SIZE);
Matrix B (ROW_SIZE, COLUMN_SIZE);
for (int i=0; i For (int j=0; J & lt; COLUMN_SIZE; + + j) {
Cin & gt;> A, [I] [j].
}
}
for (int i=0; i For (int j=0; J & lt; COLUMN_SIZE; + + j) {
Cin & gt;> B [I] [j];
}
}
PrintMatrix (a * b);
return 0;
}
  • Related