Your Queue class is up and working in a customer service company. The company opens up a new branch and asks you to make another | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Your Queue class is up and working in a customer service company. The company opens up a new branch and asks you to make another

Could someone help me create a c++ code for this question

21st Apr 2021, 11:01 PM
Joseph Lwambi
Joseph Lwambi - avatar
16 Answers
+ 1
Thanks so much... The fix actually works on the program that i was stuck... Thanks for the help...
22nd Apr 2021, 12:07 AM
Joseph Lwambi
Joseph Lwambi - avatar
0
Hi! First you should try to do it yourself
21st Apr 2021, 11:27 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
I tried but am stuck somewhere
21st Apr 2021, 11:28 PM
Joseph Lwambi
Joseph Lwambi - avatar
0
In this case, please show us your code attempt
21st Apr 2021, 11:29 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
#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 res; for(int i=0;i<this->size;i++) { res.add(this->queue[i]); } for(int i=0;i<obj.size;i++) { res.add(obj.queue[i]); } return res; } }; //your code goes here class Queue2 : public Queue { public: Queue2(){ }; void print(){ if(size==0){
21st Apr 2021, 11:30 PM
Joseph Lwambi
Joseph Lwambi - avatar
0
cout<<"Queue is empty"<<endl; return; } for(int i=0; i<size; i++){ cout<<queue[i]<<"\n"; } cout<<endl; } }; int main() { Queue q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); q1.print(); Queue2 q2; q2.add(3); q2.add(66); q2.add(128); q2.add(5);q2.add(111);q2.add(77890); q2.print(); return 0; }
21st Apr 2021, 11:31 PM
Joseph Lwambi
Joseph Lwambi - avatar
0
That is the whole code...
21st Apr 2021, 11:32 PM
Joseph Lwambi
Joseph Lwambi - avatar
0
it is advisable to place such a large code in your code platform for convenience and give a link to it in this question
21st Apr 2021, 11:33 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
Lemme try this
21st Apr 2021, 11:34 PM
Joseph Lwambi
Joseph Lwambi - avatar
21st Apr 2021, 11:36 PM
Joseph Lwambi
Joseph Lwambi - avatar
0
Done
21st Apr 2021, 11:36 PM
Joseph Lwambi
Joseph Lwambi - avatar
0
Am not sure if i have gotten what you said exactly... Could you try to show me what you mean and where
21st Apr 2021, 11:38 PM
Joseph Lwambi
Joseph Lwambi - avatar
0
Okay, I read the assignment. Here's the task. You want to inherit Queue2 from Queue. You did that. So disregard my previous post. The ONLY task is to override the print() method so that it outputs in a slightly different way. At first glance, the code you have should work. What is the error you are having?
21st Apr 2021, 11:54 PM
Jerry Hobby
Jerry Hobby - avatar
0
'int Queue::size' is declared private within its context
21st Apr 2021, 11:57 PM
Joseph Lwambi
Joseph Lwambi - avatar
0
I don't know if this is the desired solution, but to fix that problem, I changed the declaration in the "class Queue()" to public. It says private. I changed it to public. Like this: class Queue { public: int size; int* queue;
22nd Apr 2021, 12:03 AM
Jerry Hobby
Jerry Hobby - avatar
0
Okay... Lemme check
22nd Apr 2021, 12:05 AM
Joseph Lwambi
Joseph Lwambi - avatar