Hi, I am working on an exercise in operator overloading. I've created a matrix class and I am supposed to overload operators so I can do arithmetic on matrices efficiently.

My directions say that I am supposed to make two matrix arrays using the class constructor that has 2 parameters and a third matrix array that will be used to store the result of arithmetic using the default constructor (1 parameter).

Since I am going to use these arrays to overload operators they are going to need to be data members of the class (I think). However, I thought that classes were supposed to be as representative of real life things as possible so making a matrix class with multiple arrays doesn't make sense to me (a matrix is only one matrix).

Am I misunderstanding classes or is there a different way to make additional matrices using the class constructor I am not thinking of? Thanks all, here is the code in question.

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <cmath>
 
using namespace std;
 
class matrix
{
    friend ostream& operator << (ostream&, const matrix&);
 
    private:
    int size;
    int array[10][10];
 
    public:
    matrix(int);
    matrix(int, int);
};
 
int main()
{
    int sizeIn, rangeIn;
 
    cout << "What size matrix would you like to generate?" << endl;
    cin >> sizeIn;
    cout << "What is the highest value you would like to allow a number in the matrix to be?" << endl;
    cin >> rangeIn;
 
    matrix arrayPrint(sizeIn, rangeIn);
 
    srand (static_cast<int>(time(NULL)));
 
    cout << arrayPrint << endl;
 
    return 0;
}
 
 
matrix:: matrix (int sizeIn)
{
    int MAX_SIZE = 10;
 
    if (0 > sizeIn && sizeIn > 10)
    {
     size = MAX_SIZE;
    }
    else
    {
     size = sizeIn;
    }
 
    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
             array[i][j] = 0;
}
 
matrix:: matrix (int sizeIn, int rangeIn)
{
    int range;
    int MAX_SIZE = 10;
    int MAX_RANGE = 20;
 
    if (0 > sizeIn && sizeIn > 10)
    {
     size = MAX_SIZE;
    }
    else
    {
     size = sizeIn;
    }
 
    if (0 > rangeIn && rangeIn > 20)
    {
      range = MAX_RANGE;
    }
    else
    {
     range = rangeIn;
    }
 
    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
            array[i][j] = (rand() % (2 * range + 1) - range);
}
 
ostream & operator << (ostream & os, const matrix & arrayPrint)
{
    for (int i = 0; i < arrayPrint.size; i++)
    {
        cout << '|';
        for (int j = 0; j < arrayPrint.size; j++)
            {
            os << setw(4) << arrayPrint.array[i][j] << " ";
            }
        os << setw(2) << '|' << endl;
    }
    return os;
}

Edited 5 Years Ago by mwmnj: n/a

Hi, I am working on an exercise in operator overloading. I've created a matrix class and I am supposed to overload operators so I can do arithmetic on matrices efficiently.

This is where the fun part comes into play :D Enjoy it and let it roll.

My directions say that I am supposed to make two matrix arrays using the class constructor that has 2 parameters and a third matrix array that will be used to store the result of arithmetic using the default constructor (1 parameter).

Sounds about right.

Since I am going to use these arrays to overload operators they are going to need to be data members of the class (I think).

Yes. The data that you will be using for your particular class assignment will be class member values of the matrix class that you created.

However, I thought that classes were supposed to be as representative of real life things as possible so making a matrix class with multiple arrays doesn't make sense to me (a matrix is only one matrix).

Nice thinking. It wouldn't make much sense to see one matrix having more than one matrix inside when you are trying to create and have one matrix in all.

Am I misunderstanding classes or is there a different way to make additional matrices using the class constructor I am not thinking of? Thanks all, here is the code in question.

I think you have part of it down. Once you figure out how overloading operators work for arithmetic you will have a blast overloading everything from here to China! (Or if you are originally from China, possibly to USA, or even England, or somewhere in the middle of the Atlantic o.O )

For a -basic- view of overloading a matrix, or most data values leading to arithmetic, will be viewed as this. You will have two arguments that already have values, you will combine these two arguments and -then create a new object- to store the values of the equated expression.

The two original arguments will use the constructor with two parameters to create their own respective object. Once the call to do the manipulation between these two objects is made, your overloaded operator will kick in, combine then two objects in which will be then saved into a new -third- object. When that new third object is made your constructor with one parameter will likely be called.

C = A + B;     // where as A and B are the two original objects, and C is the third, new object
// object A and object B are unchanged after the addition, but their sum will be stored in object C

At this point, you can see how only one array is needed -per object-, but multiple objects are being used to handle multiple matrices.

A side note, possibly logical error, but I will blame it being past midnight if Im at fault xD

if (0 > sizeIn && sizeIn > 10)

// reads, if sizeIn is less than zero AND sizeIn greater than 10, do this, else do that.
// ... when would it ever be true?

You may want to change it to a more logical pattern of the pseduocode listed below

if (sizeIn is greater than 10 || sizeIn is less than 0)
          <do this code>
   else 
          <do that code>

OR, using your && (AND) operand

if (sizeIn is less than or equal to 10 && sizeIn is greater than or equal to 0)
         <do this code>
   else
         <do that code>
This article has been dead for over six months. Start a new discussion instead.