+ 1
I need help with the last project on C++ course
5 Answers
+ 1
size type should be always int so make
int size;
And queue must be of type T
So change to queue = new T[100];
And remaining are fine I think.
0
What help? post your try along description..
0
I've to transform Queue class to admit more than 1 data type. I'm using template for it, send you capture
0
#include <iostream>
using namespace std;
template<class T>
class Queue {
T size;
T* queue;
public:
Queue() {
size = 0;
queue = new int[100];
}
void add(T 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;
}
};
int main() {
Queue<int> q1;
q1.add(42); q1.add(2); q1.add(8); q1.add(1);
q1.print();
Queue<string> q2;
0
Thanks son much bro