Matrix operator overloading program question

How would I copy an array that is made in a constructor into external arrays outside of the class and then use those copies for operator overloading?

I am trying to overload operators for matrices, If you look at the end of my code you can see i already have successfully overloaded the << operator using the matrix in the class. The problem is I need to do it with more than one matrix so I think I will need to call the constructor more than once and assign the matrix it creates to external matrices. Not sure how to copy the class array to though


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
    #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;
    }
Use proper indentation.

if (0 > rangeIn && rangeIn > 20) This will never be true. rangeIn can't be both less than zero and more than 20 at the same time. You should replace && with ||.

When you overload operator + for two matrices, your code will be
1
2
matrix a(10, 20), b(10, 20);
matrix c = a + b;

Constructor is called for the object that is being declared. You don't need to copy anything yourself.
Topic archived. No new replies allowed.