Alright so I'm completely stuck I don't understand my assignment.
My biggest problem is learning what gets passed where since our instructor has us working
across 2 .cpp files and a header file. I think I might be able to write the code if I understood HOW it was being passed into and accessed between the files.
The matrix.cpp file I created on my own but didn't know how to build the remaining functions.
matrix.h
#ifndef __MATRIX_H__
#define __MATRIX_H__
#include <iostream>
using namespace std;
#define MAXROWS 3
#define MAXCOLS 3
class Matrix {
private:
int elements[MAXROWS][MAXCOLS];
public:
Matrix();
void operator++();
void operator+=(int array[MAXROWS][MAXCOLS]);
Matrix operator-(Matrix& other) const;
Matrix operator+(Matrix& other) const;
void display();
};
#endif
matrix.cpp
#include <iostream>
#include "matrix.h"
#include <string>
using namespace std;
#define MAXROWS 3
#define MAXCOLS 3
Matrix::Matrix()
{
int elements[MAXROWS][MAXCOLS] = { {0} }; // Tried to initialize all to 0
}
void Matrix::operator++()
{
//I need to increment the array to {1,2,3,4,5,6,7,8,9} using array1 in main
}
void Matrix::operator+=(int array[MAXROWS][MAXCOLS])
{
}
Matrix Matrix::operator-(Matrix& other) const
{
}
Matrix Matrix::operator+(Matrix& other) const
{
}
void Matrix::display()
{
for(int i = 0; i < MAXROWS; i++)
{
for(int j = 0; j < MAXCOLS; j++)
{
cout << "[ " << elements[i][j] << " ]";
}
cout << endl;
}
cout << endl;
}
main.cpp
#include <iostream>
#include "matrix.h"
using namespace std;
#define MAXROWS 3
#define MAXCOLS 3
int main()
{
Matrix matrix1, matrix2, matrix3, matrix4;
int array1[MAXROWS][MAXCOLS] =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int array2[MAXROWS][MAXCOLS] =
{
{10, 11, 12},
{13, 14, 15},
{16, 17, 18}
};
cout << "Matrix object 'matrix1' instantiated:" << endl;
matrix1.display(); //How do I instantiate matrix1?
cout << "2D array1 added to matrix1 using += " << endl;
matrix1 += array1;
matrix1.display();
// Use overloaded assignment operator to copy the values across
cout << "2D array2 added to matrix2 using += " << endl;
matrix2 += array2;
matrix2.display();
cout << "All elements of matrix1 incremented with ++ " << endl;
++matrix1;
matrix1.display();
cout << "Using addition and subtraction operators" << endl;
matrix3 = matrix1 + matrix2;
matrix4 = matrix2 - matrix1;
cout << "matrix3 = matrix1 + matrix2" << endl;
matrix1.display();
cout << "+" << endl;
matrix2.display();
cout << "--------------" << endl;
matrix3.display();
cout << "matrix4 = matrix2 - matrix1" << endl;
matrix2.display();
cout << "-" << endl;
matrix1.display();
cout << "--------------" << endl;
matrix4.display();
return 0;
}
Sorry for throwing all the code out there I just wanted to be thorough.
Also the fact that it's a 2d array is confusing me even more.
I've been at this for hours but I have never done anything like this and my searches have always come up dry....what do you guys suggest?