1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
// add the following:
// copy constructor
// destructor
// overload the = operator so assignment works for Matrix
// (note in sample code that = works for single elements)
// a member function that prints the matrix to standard output
// a member function that inputs a Matrix from standard input
// overload the >> and << operators so they read/write a matrix
// add calls to main demonstrating that your copy constructor, output
// and input functions work
#include <iostream>
#include <cstdlib>
using namespace std;
template <typename T > class Matrix
{
private:
int R; // row
int C; // column
T *m; // pointer to T
public:
//Matrix();
T &operator()(int r, int c)
{
return m[r+c*R];
}
Matrix(int R0, int C0) {R=R0; C=C0; m=new T[R*C];}
//Why is it R0 and C0? what do the numbers denote?
/*T &operator=(Matrix<int> & b)
{
for (int i=0;i<R; i++)
{
for (int j=0;j<C;j++)
{
m(i)(j)=b(i)(j);
}
}
return b;
}*/
};
/*
Matrix::Matrix()
{
for (int i=0; i<R;i++)
{for (int j=0;j<C;j++)
{
//a
//confused how I should make a default matrix
//could I do a[1][1]=1 instead?
}
}
}
*/
/*
Matrix::Matrix(int R0, int C0)
{
R=R0; C=C0; m=new T[R*C];
}
*/
int main()
{
Matrix<double> a(3,3);
a(1,1)=11;
a(1,2)=12;
a(1,3)=13;
a(2,1)=21;
a(2,2)=22;
a(2,3)=23;
a(3,1)=31;
a(3,2)=32;
a(3,3)=33;
cout << a(1,1) <<" ";
cout << a(1,2) <<" ";
cout << a(1,3)<<endl;
cout << a(2,1) <<" ";
cout << a(2,2) <<" ";
cout << a(2,3)<<endl;
cout << a(3,1) <<" ";
cout << a(3,2) <<" ";
cout << a(3,3)<<endl;
}
|