+ 2
Can anyone help me out ...!?
It's time to update your Queue management system. The previous version supports only integer numbers and we need to support more types, such as strings, to store customer names in the queue. Transform the given Queue class to a class template, which can work with different data types.
4 Answers
+ 2
#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;
}
};
int main() {
Queue<int> q1;
q1.add(42); q1.add(2); q1.add(8); q1.add(1);
q1.print();
Queue<string> q2;
q2.add("Dave"); q2.
+ 1
Why this is not working !
+ 1
// This part the has to be changed:
template <class T>
class Queue {
int size;
T* queue;
public:
Queue() {
size = 0;
queue = new T[100];
}
void add(T data) {
queue[size] = data;
size++;
}
0
I havent tried the code but based on the assignment. You should change the class into template class so it can work with different types.
Based on your code. You havent change the class with template but you've already tried to access it like template so the error occurs.
Example (for "add" method only):
// on the class //
// add template and change the data type
template<typename T>
class Queue
{
int size;
T* queue;
..........
// on the method //
// change the data type with template
template<typename T>
void add(T data) {
queue[size] = data;
size++;
}
I hope you can do the rest. Good luck.
See more about template at..
https://www.geeksforgeeks.org/templates-cpp/amp/