OOP help with comparing 2D arrays with a class

Hello out there!

I have an assignment that wants me to create two squares with the dimensions of the user input. Once I have the length and width inputs from the user I pass the info to my class which creates the square with a 2D array thats declared in my private member variables. I then fill the array with 1s and 0s.

My question is, once my I create two objects, or squares, of the same class how do I compare the two objects that are arrays and tell the compiler "if square one has a 1 in the same spot([row][column]) as square 2 then create a 3rd array and put a 1 in the same spot.

Here is my code. Thank you in advance.

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

//Grid.h

#ifndef OOP_HW3_GRID_H
#define OOP_HW3_GRID_H

class Grid {

private:

    int length, width;    //variables for size of grids

//    Initialize Array 
    static const int numRows = 20;  //rows for 2D Array
    static const int numCols = 20;  //columns for 2D Array
    int array1[numRows][numCols];    //Array of Grid One

public:

    Grid(); //Default Constructor
    Grid(int length, int width);    //initializes all length/width

//    Set Setters functions

//  set length of grids
    void setLength(int x1) { length = x1; }

//    Set Width of grids
    void setWidth(int y1) { width = y1; }

//    Set Getters functions

//  Get lengths of the 3 grids
    int getLength() { return length; }

//    Get Widths of the 3 grids
    int getWidth() { return width; }

//    Fill array
    void fillArray();  //Fills Array

//    Display array
    void displayArray();

};
#endif //OOP_HW3_GRID_H

//Grid.cpp

#include "Grid.h"
#include <iostream>
#include <cstdlib>
using namespace std;

Grid::Grid()
{
    length = 0;
    width = 0;
}

Grid::Grid(int _length, int _width)
{
    length = _length;
    width = _width;
}

//  Function fills array
void Grid::fillArray()
{


    for (int i = 0; i < length; i++)   //Fills row
        for (int j = 0; j < width; j++)   //Fills column
        {
            int randNum = rand() % 2;    //Generates random number divides by modulo of 2
            if (randNum == 1 || randNum == 0)   //if randNUm is equal to 0 or 1
            {
                array1[i][j] = randNum;
            }
        }
}

//  Function displays array
void Grid::displayArray()
{

    for (int i = 0; i < length; i++)
    {
        for (int j = 0; j < width; j++)
        {
            cout << array1[i][j] << " ";
        }
        cout << endl;

    }
}


int main() {

    int x1, y1;    //x & y size for 1st grid
    int x2, y2;    //x & y size for 2nd grid
    Grid Object1;

    cout << endl;
    cout << "Lets build two grids and compare which ones " << endl
         << "have matching squares filled with '1' !"<< endl
         << "Please enter the width (Between 1-20) of grid one : ";
    cin >> x1;  //user inputs x of grid1
    cout << "Please enter the length(Between 1-20) of grid one: ";
    cin >>y1;   //user input y of grid1

    cout << "Now enter the width (Between 1-20) of grid TWO: ";
    cin >> x2;  //user inputs x of grid2
    cout << "And, finally, the length (Between 1-20) of grid TWO: ";
    cin >> y2;  //user inputs y of grid2

//    Use setters for setting dimensions of Grids
    Object1.setLength(x1); //Length of Grid one set
    Object1.setWidth(y1);  //Width of Grid one set

//    Equivalent to previous 2 lines
    Grid Object2(x2, y2);   //uses constructor to set 2nd object and array.

//    Fills two arrays
    Object1.fillArray();
    Object2.fillArray();

//    Displays two arrays
    Object1.displayArray();
    cout << "\n\n";

    Object2.displayArray();



//    Compare two arrays

    return 0;
}
https://en.cppreference.com/w/cpp/language/operators
Scroll down to Relational operators
Implement something like
bool operator==(const Grid& lhs, const Grid& rhs){ /* do actual comparison */ }
Topic archived. No new replies allowed.