Dr.Noob
Dr.Noob

Reputation: 329

Operator overloading function is not working

I have a class Array this class has functions like set values to an array index or get a value of an array index, i wanted to make an overloading operator function to add two Arrays objects and the result is one array which every element is the sum of the same index elements of the other two arrays like: Array A={1,4,6}, Array B={1,20,3} c=A+B //c={1+1,4+20,6+3}. the problem is that in the Sum Array object i am getting some random values instead of the sum values ,i think they are some addresses cus my overloading function somehow is not working properly.

public:

    Array(int asize)
        :size(asize)
    {

        Arr = new int[size];
    }
Array(const Array &other) {
        size = other.size;
        Arr = new int[other.size];//
        for (int x = 0;x < other.size - 1;x++)
            Arr[x] = other.Arr[x];
    }
 Array operator+(const Array &other) {
        Array t(*this);// creating new array with the values of the left hand Array


        int ssize = (other.size < size) ? other.size : size;//if the right hand array size is 
        for (int x = 0;x < ssize;x++)                            //smaller than the left side(choose the smallest size)     
        {
            t.Arr[x] += other.Arr[x];
        }

        return t;
    }

    ~Array() {
        delete[] Arr;
    }
    int get(int index) {
        return Arr[index];
    }
    void set(int index, int value) {
        Arr[index] = value;
    }

    int getSize() {
        return size;
    }
    Array operator+(const  Array  &rhs)  {//I also tried without const and without & sign
        Array H(this->getSize());
        for (int i = 0;i < this->getSize();i++)
        {
            H.Arr[i] = rhs.get(i)+this->get(i);//or rhs.Arr[i]+this->Arr[i];
            }
        return H;
    }


private:
    int size;
    int *Arr;


     }; 

and this is my main function:

std::cout << "\n enter the size of the Array" << std::endl;
    int Size1;
    std::cin >> Size1;
    Array A1(Size1);
    std::cout << "insert the elements starting of the first element of the Array" << std::endl;
    int E1;
    for (int i = 0;i < Size1;i++) {
        std::cout << "Element number" << i + 1 << " is: " << std::endl;

        std::cin >> E1;
        A1.set(i, E1);

    }


    std::cout << "\nenter the size of the Array 2" << std::endl;
    int Size2;
    std::cin >> Size2;
    Array A2(Size2);
    std::cout << "insert the elements starting of the first element of the Array" << std::endl;
    int E2;
    for (int i = 0;i < Size2;i++) {
        std::cout << "Element number" << i + 1 << " is: " << std::endl;

        std::cin >> E2;
        A2.set(i, E2);

    }

    Array sum(2);// I am currently trying with only 2 elements Arrays just to check if its working
    sum = A1 + A2;
    for (int i = 0;i < Size2;i++) {
        std::cout << sum.get(i) << " , ";
    }


Upvotes: 2

Views: 155

Answers (1)

Yitshak Yarom
Yitshak Yarom

Reputation: 84

In addtion to the copyt constructor, please note

 Array sum(2);// I am currently trying with only 2 elements Arrays just to check if its working
    sum = A1 + A2;

This mean you need also operator= to be defined for your code to work properly.

BTW if you write: Array sum(A2 + A2); instead of those two lines, this will work also with no operator=.

Upvotes: 1

Related Questions