Matrix class operator overloading
Oct 27, 2016 at 1:52pm UTC
Hi Guys,
I'm trying to learn about classes and operator overloading. I took this code (also from this forum as a basis and changed some stuff around:
http://www.cplusplus.com/forum/general/87651/
My Problem is, that the copy assignment operator somehow does not work properly and I can't pinpoint the mistake. The result after performing the operation Matrix A = Matrix B ist still Matrix A.
I would be very grateful for any tip.
Here's the class file:
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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
#include "matrix.h"
#include <iostream>
//#include <stdio.h>
//#include <stdlib.h>
//#include <new>
using namespace std;
//matrix initialisation
matrix::matrix(int r, int c){ //defines the constructor to create a new matrix (dimension r*c) with 0 values
row = r;
col = c;
array = new int *[row]; //initialize empty rows
for (int i=0; i<row; i++){
array[i] = new int [col]; //initialize empty columns
}
for (int i=0; i<row; i++){
for (int j=0; j<col; j++){
array[i][j] = 0; //fills empty matrix with 0 vales
}
}
};
//initialises copy constructor
matrix::matrix(const matrix& m){ //defines the copying constructor
row = m.row;
col = m.col;
array = new int *[row];
for (int i=0; i<row; i++){
array[i] = new int [col];
}
for (int i=0; i<row; i++){
for (int j=0; j<col; j++){
array[i][j] = m.array[i][j];
}
}
}
//destructor
matrix::~matrix(){ //destroy matrix
for (int i=0; i<row; i++){
delete [] array[i]; //delete columns
}
delete [] array; //delete rows
};
void matrix::setValue(int r, int c, int e){
array[r][c] = e;
};
int matrix::getValue(int r, int c){
return array[r][c];
}
//overload operator= (copy assignment operator) - does not create new matrix
matrix matrix::operator =(const matrix& m){
matrix new_mat(row,col);
for (int i=0; i<row; i++){
for (int j=0; j<col; j++){
new_mat.array[i][j] = m.array[i][j];
}
}
return new_mat;
}
// overload + operator
matrix matrix::operator +(const matrix& m){
// if matrices do not have the same size, return original matrix
if (row != m.row || col != m.col){
cerr << "Matrix sizes do not match." ;
return (*this );
}
matrix new_mat(row,col);
for (int i=0; i<row; i++){
for (int j=0; j<col; j++){
new_mat.array[i][j] = array[i][j] + m.array[i][j];
}
}
return new_mat;
}
// overload * operator
matrix matrix::operator *(const matrix& m){
matrix new_mat(row,col);
for (int i=0; i<row; i++){ //i = rows
for (int j=0; j<col; j++){ //j = cols
for (int k = 0; k < col; ++k)
{
new_mat.array[i][j] += array[i][k] * m.array[k][j];
}
}
}
return new_mat;
}
Header:
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
#ifndef MATRIX_H
#define MATRIX_H
class matrix
{
int row;
int col;
int **array;
public :
matrix(int ,int ); //constuctor
matrix(const matrix&); //constructor to copy a matrix
~matrix(); //destructor
//operator overloads
matrix operator =(const matrix&);
matrix operator +(const matrix&);
matrix operator *(const matrix&);
void setValue(int r, int c, int e);
int getValue(int r, int c);
};
#endif // MATRIX_H
And Main.cpp
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
#include <iostream>
#include <matrix.h>
#include <cmath>
using namespace std;
int main()
{
int r = 4;
int c = 4;
matrix matrixA(r,c);
matrix matrixB(r,c);
matrix matrixC(r,c);
for (int i=0; i<r; i++){
for (int j=0; j<c; j++){
matrixA.setValue(i,j,1);
matrixB.setValue(i,j,2);
//matrixC.setValue(i,j,5);
}
}
cout << "matrixA" << endl;
for (int i=0; i<r; i++){
for (int j=0; j<c; j++){
cout << matrixA.getValue(i,j) << "\t" ;
}
cout << endl;
}
cout << endl;
cout << "matrixB" << endl;
for (int i=0; i<r; i++){
for (int j=0; j<c; j++){
cout << matrixB.getValue(i,j) << "\t" ;
}
cout << endl;
}
cout << endl;
cout << "matrixC before addition" << endl;
for (int i=0; i<r; i++){
for (int j=0; j<c; j++){
cout << matrixC.getValue(i,j) << "\t" ;
}
cout << endl;
}
cout << endl;
matrixC = matrixA; // + matrixB;
cout << "matrixC after addition" << endl;
for (int i=0; i<r; i++){
for (int j=0; j<c; j++){
cout << matrixC.getValue(i,j) << "\t" ;
}
cout << endl;
}
cout << endl;
}
Last edited on Oct 27, 2016 at 1:53pm UTC
Oct 27, 2016 at 2:14pm UTC
The copy assignment operator should modify the object that you called it on (i.e. *this) and return a reference to the same object.
Oct 27, 2016 at 2:19pm UTC
Great, problem solved!!
Thanks so much!!
Topic archived. No new replies allowed.