TransWikia.com

How to overload operator+ in a class to add two arrays in c++?

Stack Overflow Asked by Liara on February 22, 2021

What is the correct syntax in c++ to use operator overloading for + to add the integer values stored in two arrays in c++?

In below code example. I managed to concatenate the size of two arrays by using:

Queue operator+(Queue &obj)
    {
        Queue total;
        total.size = this->size + obj.size;

but I don’t know what is the correct syntax for concatenate their values? The following doesn’t work.

total = this->queue + obj;
    }

The full code are as follows:

#include <iostream>
using namespace std; 

class Queue { 
    int size; 
    int* queue; 
    
    public:
    Queue() { 
        size = 0;
        queue = new int[100];
    }
    void add(int data) { 
        queue[size] = data; 
        size++;
    }
    void remove() { 
        if (size == 0) { 
            cout << "Queue is empty"<<endl; 
            return; 
        } 
        else { 
            for (int i = 0; i < size - 1; i++) { 
                queue[i] = queue[i + 1]; 
            } 
            size--; 
        } 
    } 
    void print() { 
        if (size == 0) { 
            cout << "Queue is empty"<<endl; 
            return; 
        } 
        for (int i = 0; i < size; i++) { 
            cout<<queue[i]<<" <- ";
        } 
        cout << endl;
    }
    
    Queue operator+(Queue &obj)
    {
        Queue total;
        total.size = this->size + obj.size;
       
        total = this->queue + obj;
        
        return total;
            
    }
} ;

int main() { 
    Queue q1; 
    q1.add(42); q1.add(2); q1.add(8);  q1.add(1);
    Queue q2;
    q2.add(3); q2.add(66); q2.add(128);  q2.add(5);
    Queue q3 = q1+q2;
    q1.print();
    q2.print();
    q3.print();
    
    

    return 0; 
} 

2 Answers

Here it the long answer that hopefully gives a better understanding of what is going on.

You need a constructor for your overload in addition to the original constructor.

Queue(int a, int* b)
        : size(a), queue(b) { }

This will allow you to access the array through the pointer. The original constructor initializes to 100 elements. Because of this I used loops to add the first array then continued to add the second array after as opposed to adding the values of the elements q1 and q2 together. See below please:

#include <iostream>
using namespace std;

class Queue {
    int size;
    int* queue;

public:

    Queue() {
        size = 0;
        queue = new int[100];
    }
    Queue(int a, int* b)
        : size(a), queue(b) { }

    void add(int data) {
        queue[size] = data;
        size++;
    }
    void remove() {
        if (size == 0) {
            cout << "Queue is empty" << endl;
            return;
        }
        else {
            for (int i = 0; i < size - 1; i++) {
                queue[i] = queue[i + 1];
            }
            size--;
        }
    }
    void print() {
        if (size == 0) {
            cout << "Queue is empty" << endl;
            return;
        }
        for (int i = 0; i < size; i++) {
            cout << queue[i] << " <- ";
        }
        cout << endl;
    }
    Queue operator+(Queue &obj) {
        Queue ans;
        int total_size;
        int x;

        total_size = this->size + obj.size;
    

        for (x = 0; x < this->size; x++) {
            ans.queue[x] = this->queue[x];
        
        }
        for (x = this->size; x < total_size; x++) {
            ans.queue[x] = obj.queue[x - this->size];
        
        }
        ans.size = this->size+obj.size;
        return ans;

    }


};

int main() {
    Queue q1;
    q1.add(42); q1.add(2); q1.add(8);  q1.add(1);
    Queue q2;
    q2.add(3); q2.add(66); q2.add(128);  q2.add(5);
    Queue q3 = q1 + q2;
    q3.print();

    return 0;
}

This solved the problem but I am sure there is a more elegant solution for concatenating the arrays.

Answered by Joe on February 22, 2021

Concatenating can be done by copying the arrays from the objects.

#include <algorithm> // for std::copy()

    Queue operator+(Queue &obj)
    {
        Queue total;
        total.size = this->size + obj.size;
       
        if (total.size > 100) {
            cout << "Too long"<<endl;
        } else {
            std::copy(this->queue, this->queue + this->size, total->queue);
            std::copy(obj.queue, obj.queue + obj.size, total->queue + this->size);
        }

        
        return total;
            
    }

Answered by MikeCAT on February 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP