AnswerBun.com

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

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; 
} 

Stack Overflow Asked by Liara on February 22, 2021

2 Answers

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!

Related Questions

What will this recursive function yield?

1  Asked on December 9, 2021 by user13812739

     

How to style Rebass Switch

5  Asked on December 9, 2021 by amaster

       

Redis Cluster Mode – replicas not working

1  Asked on December 9, 2021 by ramprakash

     

Disable seeking in mediaelement.js

1  Asked on December 9, 2021 by pelirrojo

         

Table not getting created sqlite android

4  Asked on December 9, 2021 by user3844417

   

Angularjs using JWT breaks CORS to Amazon S3 on login

2  Asked on December 9, 2021 by codephobia

     

Write a value with opc ua Python

2  Asked on December 9, 2021

   

unable to install vue chart.js in vue

2  Asked on December 9, 2021 by prabina-sht

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.