I have my assignment and need help very badly - would you be kind enough to answer the bellow question?
1 - Convert the Matrix class to a template class. Test the matrix template with a suitable main function that declares a matrix of complex numbers, inputs from keyboard/file and outputs to screen/file.
So the original code follows as
#ifndef MATRIX_H //include guard
#define MATRIX_H
#include <iostream> //generic IO
#include <fstream> //file IO
#include <stdexcept> //provides exceptions
#include "vector.h" //we use Vector in Matrix implementation
class Matrix {
private:
Vector v; // Vector used to store the matrix elements
int nrows; // number of rows of the matrix
int ncols; // number of columns of the matrix
public:
// CONSTRUCTORS
Matrix(); // default constructor, uses default constructor for v
Matrix(int Nrows, int Ncols); // alternate constructor
Matrix(const Vector& v); // build a matrix from a vector
Matrix(const Matrix& m); // copy constructor
// ACCESSOR METHODS
int getNrows() const; // get the number of rows
int getNcols() const; // get the number of cols
// OVERLOADED FUNCTION CALL OPERATORS
double& operator() (int i, int j); // function call overload (-,-) for assignment
double operator() (int i, int j) const; // for reading matrix values
Matrix& operator=(const Matrix& m); // overloaded assignment operator
bool operator==(const Matrix& m) const; // overloaded comparison operator
// INPUT & OUTPUT
friend std::istream& operator>>(std::istream& is, Matrix& m);// keyboard input
friend std::ostream& operator<<(std::ostream& os, const Matrix& m);// screen output
//the file output operator is compatible with file input operator,
//ie. everything written can be read later.
friend std::ifstream& operator>>(std::ifstream& ifs, Matrix& m);// file input
friend std::ofstream& operator<<(std::ofstream& ofs, const Matrix& m);// file output
};
// Note: There is no strict need for a copy constructor or
// assignment operator in the Matrix class since the
// compiler versions of these methods will work OK. It will copy/assign
// the values of nrows and ncols and call the Vector copy
// constructor/assignment operator automatically for the
// Vector part v inside the Matrix. However they are written
// for clarity and understanding
#endif
In order to convert it to a template I changed the features with template
#ifndef MATRIX_H //include guard
#define MATRIX_H
#include <iostream> //generic IO
#include <fstream> //file IO
#include <stdexcept> //provides exceptions
#include "vector.h" //we use Vector in Matrix implementation
class Matrix {
template <typename T> class T
protected:
Vector v; // Vector used to store the matrix elements
int num; // Number of elements
int nrows; // number of rows of the matrix
int ncols; // number of columns of the matrix
T* pdata; // Pointer to the data
void Init(int Num); // private function since user should not call it
public:
// CONSTRUCTORS
Matrix(); // default constructor, uses default constructor for v
Matrix(int Nrows, int Ncols); // alternate constructor
Matrix(const Matrix& v); // build a matrix from a vector
Matrix(const Matrix& m); // copy constructor
// SIZE
int size() const; //get number of elements in the vector
// ACCESSOR METHODS
int getNrows() const; // get the number of rows
int getNcols() const; // get the number of cols
// OVERLOADED FUNCTION CALL OPERATORS
Matrix<T>& operator() (int i, int j); // function call overload (-,-) for assignment
T& operator() (int i, int j) const; // for reading matrix values
T operator[] (int i) const; // overloaded array access operator for reading
Matrix& operator=(const Matrix& m); // overloaded assignment operator
bool operator==(const Matrix& m) const; // overloaded comparison operator
// INPUT & OUTPUT
friend std::istream& operator>>(std::istream& is, Matrix& m);// keyboard input
friend std::ostream& operator<<(std::ostream& os, const Matrix& m);// screen output
//the file output operator is compatible with file input operator,
//ie. everything written can be read later.
friend std::ifstream& operator>>(std::ifstream& ifs, Matrix& m);// file input
friend std::ofstream& operator<<(std::ofstream& ofs, const Matrix& m);// file output
};
// Note: There is no strict need for a copy constructor or
// assignment operator in the Matrix class since the
// compiler versions of these methods will work OK. It will copy/assign
// the values of nrows and ncols and call the Vector copy
// constructor/assignment operator automatically for the
// Vector part v inside the Matrix. However they are written
// for clarity and understanding
#endif
and finally to test the Matrix I entered at a separate class
#include <iostream>
#include <cmath>
#include <vector.h>
#include <complex.h>
#include <matrix.h>
Matrix<Complex> matrix_vector(10);
std::cin >> matrix_vector;
Please please, I'm getting errors and not entirely sure if I am correct at all places. I would highly appreciate. Thanks so much