- 2
Help with “Queue management part 3” and “Queue management part 4”
Please share the solution to these two problems (complete code)
6 ответов
+ 2
People should not share complete codes. You won't learn correctly that way.
+ 2
E.g. in part3 write a Queue2 class that is derived from the Queue class and overrides the print() method.
0
post your attempts first and we will help
0
for part 3 just look how to inherit Queue2 from Queue, at lesson 74.1 Abstract Classes there are some good examples about it. Then just change the void print() func a bit and don't forget to change access specifier for some variables. Without posting the code this is all i can write ı guess.
0
for part 4 you really have to understand template. 79.1 Class Templates lesson would be a great way to understand what is going on. With the help of a compiler you really can manage to do that. There are 4 parts to change in code so good luck.
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)
{ 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;
}