+ 2

Help me with "Queue Management part 3" solution in C++

3rd Jan 2021, 12:04 PM
Anjali Arakka
Anjali Arakka - avatar
16 Réponses
+ 19
#include <iostream> using namespace std; class Queue { protected: int size; protected: 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: void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<endl; } 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; }
2nd Feb 2021, 5:36 AM
Namrata Dattani
Namrata Dattani - avatar
11th Jan 2021, 2:40 AM
Dhanush
Dhanush - avatar
+ 5
#include <iostream> using namespace std; class Queue { protected: int size; protected: 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; } }; class Queue2 : public Queue { public: void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<endl; } 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; }
5th Feb 2021, 12:06 PM
Md Momin Uddin Hridoy
Md Momin Uddin Hridoy - avatar
+ 3
#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{ int size; int *queue; public: Queue2(){ size=0; queue=new int[100]; } void print(){ if(size==0){ cout<<"Queue is empty"<<endl; return; } for(int i=0;i<size;i++){ cout<<queue[i]<<endl; } } void add(int data){ queue[size]=data; size++; } }; 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; }
4th Aug 2021, 9:27 PM
Mohammad Matin Kateb
Mohammad Matin Kateb - avatar
+ 2
Do not forget to change the access specifier of the Queue members, as they won't be inherited if private.
30th Mar 2022, 6:50 AM
Paek Se
Paek Se - avatar
3rd Jan 2021, 2:22 PM
Anjali Arakka
Anjali Arakka - avatar
+ 1
You forgot to change the access modifier for size and queue as noted. By default they're automatically private if not specified.
11th Jan 2021, 11:53 AM
R A
R A - avatar
0
Please show your attempt here so that community can guide you. No one will provide you entire code. Also, you've posted same question twice se please delete it.
3rd Jan 2021, 12:07 PM
Hardik Sharma
Hardik Sharma - avatar
0
This is my attempt of the same. Try providing correction in the code.
3rd Jan 2021, 2:28 PM
Anjali Arakka
Anjali Arakka - avatar
0
Hi! You solved This part?
10th Jan 2021, 11:19 PM
Olexandr Bondar
Olexandr Bondar - avatar
0
👍
11th Jan 2021, 12:11 PM
Dhanush
Dhanush - avatar
0
Good job fixing your code Danush!
11th Jan 2021, 5:21 PM
R A
R A - avatar
0
Thank you...😊
12th Jan 2021, 1:35 AM
Dhanush
Dhanush - 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 &that) { Queue result; for (int i=0; i<this->size; i++) { result.add(this->queue[i]); } for (int i=0; i<that.si
11th Mar 2021, 10:45 PM
Moath Mohammed Hazzam Alshaabani
Moath Mohammed Hazzam Alshaabani - avatar
0
Please help me Queue Management part 1 in C++
2nd May 2022, 10:56 PM
Veronica Sabah
0
short and good! #include <iostream> using namespace std; class Queue {protected: int size; protected: 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;}}; class Queue2 : public Queue {public: void print() {if (size == 0) {cout << "Queue is empty"<<endl; return;} for (int i = 0; i < size; i++) {cout<<queue[i]<<endl;} 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;}
6th Oct 2022, 11:50 AM
Marcel Krättli