i understand your point, this my first time working with classes. Would i make the class a matrix by adding this code to the constructor instead?
Also, how could i use the new operator if i want to make it so the user can define as many objects of type matrix at they want? i've tried something like the code below but i think that there must be an easier way?
int number_of_mat;
cout << "How many matricies would you like to define: ";
cin>> number_of_mat;
vector<matrix> MATS(number_of_mat,0); // using a vector to keep track od all of the objects
for(int i=0;i<number_of_mat;i++){
matrix *ptr_mat; // creates a pointer to an object
ptr_mat = new matrix; // creates a new object
MATS[i] = *ptr_mat; // not sure why this like shouldnt be MATS[i] = ptr_mat; without the pointer?
}
Would i make the class a matrix by adding this code to the constructor instead?
You would not return the pointer/array from any function. Instead you would have the pointer/array be a member of the class.
1 2 3 4 5 6 7
class matrix
{
private:
int** data;
//...
};
You could then write member functions which get/set elements in the matrix.
Allocating space for the data would be done in the constructor, yes.
Also, how could i use the new operator if i want to make it so the user can define as many objects of type matrix at they want?
No need to use new if you're using vector. You actually did it in your code:
1 2 3 4 5 6
vector<matrix> MATS(N); // this does it
// you now have N matrixes:
MATS[0].Set(x,y,3); // set one element of the first matrix
int foo = MATS[1].Get(x,y); // get one element from the 2nd matrix
If you want to use new, you'd do it like so:
1 2 3 4 5
matrix* MATS = new matrix[N];
//...
delete[] MATS; // clean up when done
But note that it's one or the other. Don't new if you have a vector.
now i just have one question, in your code the .Set(x,y,3), is this a constructor or function or neither (i'm still trying to grasp the difference between the two)?