+ 1

Help me please.

#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 &q){ Queue q3; q3.queue[size] = this->queue[size] + q.queue[size]; return q3; } }; 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; }

19th Jul 2021, 9:03 AM
Румм
Румм - avatar
2 Answers
+ 2
Queue operator+(Queue &q){ Queue q3; for(int i=0;i<q.size;i++){ q3.queue[q3.size] = this->queue[q3.size] + q.queue[q3.size]; q3.size++; } return q3; } in your code you only added the two queue's value at 4th index. And also when you called print on q3 object , size was having value equal to 0 since you never used it.
19th Jul 2021, 9:24 AM
Abhay
Abhay - avatar
0
Thank you so much. But the expected output is: 42 <-2 <-8 <-1 <-3 <-66 <-128 <-5 <-
19th Jul 2021, 9:43 AM
Румм
Румм - avatar