Matrix Class with Arrays of Arrays Plus Template

I am quite lost with this one, it is lab for class. Been working on it off and on for three weeks now, professor is never there for questions so it is quite frustrating. So any help in the right direction is very very welcome. What we have been doing so far has been pretty simple then they threw this into the mess. I have just recently learned about Matrices, don't really understand templates, and just starting to understand how to overload an operator, not sure how to properly add it to a class.

My main things that are making me stumble:
Can I overload the = operator twice in order to have it work for both a(i,j)=1 2 4...etc. But also have it be used for a(i,j)=b(i,j) as a copy constructor? Would that be a bad idea?

If I overload the = constructor and I'm inputting numbers how do I get it to grab one at a time?

What would belong in: T &operator=(HERE)
Also what is that T doing there? I was in the part of the code already given.

What difference does the template make from a normal class?

I have commented out my code so that it is distinguishable,. That which is uncommented was given. As you can see I set up my class differently than they seem to like in the given example, which makes it hard for me to understand exactly what is going on. Any help is greatly appreciated.
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;

}




This one sort of confuses me as well... Not the actual code itself but rather how he is accessing the elements within the array to which he is storing the elements. I am to assume that "R" and "C" to be assigned in the non argument constructor. Problem is: this value being less dynamic for change, since it is used in the argumented constructor for defining the length of the array that will emulate the matrix. It makes no distinction whatsoever between a(1,2) and a(2,1) - which will point to the same value unless R is of a changing value, but the utilization that he has does not do this. So when he called a(1,2) to point to 12 then calls a(2,1) to point to 21, he is changing a value of the same index on the defined array. (in this case (9))

Perhaps when you access these elements, this is where the confusion lies?
Line 26 and Line 30 are completely different methods that look similar from a syntactical point of view.

Line 30 denotes a constructor that takes R0, C0, the number of rows and columns in the array - an example of actual use on Line 71.

Line 26 denotes an accessor operator so you can assign to a specific element at (i,j) by getting a reference to a specific location within the matrix - examples of uses are on Lines 72-80

To understand what that T is, you have to read up on templates. On a very simple, superficial level, you can think of templates as a fancy kind of macro (templates can actually do much more, like parameter matching, create mixins, etc... but for now, think of it as a macro).

Here, T represents the type of matrix you want to create - Line 71 maps T to double. So anywhere inside the template class definition, replace T with double.
Last edited on
I'm thinking I should just trash what is provided and start my own from scratch. Then apply a template to it afterwards.
that's probably a good idea because your example actually uses many C++ features, some of which are like stepping onto programming landmines, especially if you don't understand all the separate features and how they work with each other...
Topic archived. No new replies allowed.