1

Write a class Array that contains an array of integers as data member. The class contains the following member functions:

  1. A constructor that initializes the array elements to -1.
  2. Input function to input the values in the array.
  3. Show function to display the values of the array.
  4. Overload == operator to compare the values of two objects. The overloaded function returns 1 if all values of both objects are same and returns 0 otherwise.

Problem: I can't figure out how to compare using == operator.

My code:

#include<iostream>
using namespace std;
#define SIZE 10
class array{
public:
    int arr[SIZE];
public: 
    array(){
        for (int i = 0; i < SIZE; i++){

            a[i] = -1;
        }
    }

    void input(){
-       cout << "Enter values";
        for (int i = 0; i < SIZE; i++){
            cout << "Enter value number" << i + 1;
            cin >> arr[i];
        }
    }

    void show(){
        for (int i = 0; i < SIZE; i++){
            cout << arr[i];
        }

    }

    bool operator==(array& p) const
    {
        bool result = true;
        if (p.a != arr)
            result = false;

        return result;
    }
};



int main(){

    array arr, b, c;
    a.input();
    b.input();
    a.show(); b.show();

        return 0;
}
Maya
  • 85
  • 1
  • 2
  • 10

4 Answers4

1

What you actually want is probably:

bool operator==(const array& p) const
             // ^^^^^
{
    if(this == &p)
        return true;
    for(size_t i = 0; i < SIZE; ++i) {
        if(arr[i] != p.arr[i])
        // ^^^         ^^^
            return false;
    }

    return true;
}

Note that there are some other errors in your code:

array(){
    for (size_t i = 0; i < SIZE; i++){
      // ^^^^^^

        arr[i] = -1;
     // ^^^
    }
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

what is you definition of equal? I assume you mean that the sizes are the same, that all the elements are the same. If that is the case then write code that says that.

bool operator==(array& p) const
{
  // are size ==, yes they are hard coded to 20 ! not a good plan
  for (int i = 0; i < 20 ; i++)
  {

     if(a[i] != p.a[i])
        return false;
  }
  return true;
}

The code you have written says that 2 arrays are equal if they have the same address. Ie if it is the same array. maybe thats what you mean

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
pm100
  • 48,078
  • 23
  • 82
  • 145
1

You can't compare arrays like this:

if (p.a != arr)

That will decay point arrays to pointers and just do a pointer comparison. That pointer comparison will only be true if &p == this, which is not what you want. You'd need to actually manually compare all the elements in the array with a loop:

bool operator==(array const& p) const {
    for (int i = 0; i < SIZE; ++i) {
        if (arr[i] != p.arr[i]) {
            return false;
        }
    }
    return true;
}    

Or you could just use the algorithm std::equal:

bool operator==(array const& p) const
{
    return std::equal(std::begin(arr), std::end(arr), std::begin(p.arr));
}

Or you could instead use std::array, which has its own operator== defined for you:

class array{
    std::array<int, SIZE> arr;
public:
    bool operator==(array const& p) const {
        return arr == p.arr;
    }
};

Although that may defeat the entire purpose of the exercise.

Barry
  • 286,269
  • 29
  • 621
  • 977
0

You need to make use of std algorithms to simplify this code.

To compare for equality use std::equal.

For input, std::copy && std::istream_iterator.

To fill with a fixed value see here.

As mentioned above, you could use std::array, instead of your own class, it's not adding much.

All these techniques are well described in "Effective STL" by Scott Myers

Finally, if you find typing begin/end tedious, you can try the boost range library, but I'd advise learning the std versions first.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Thagi
  • 81
  • 2