CPP
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* Class Templates
You are making a queue management system and need to support different data types - for example, you should be able to queue names which are strings or integer numbers.
So, you decide to make a Queue class template, which will store an array for the queue, and have add() and get() methods.
You first create a class for integers only, to see if it works. The given code declares a Queue class with the corresponding methods for integers.
Change the class to convert it into a class template, to support any type for the queue and so that the code in main runs successfully.
Remember, the syntax to make a class template is template <class T> */
#include <iostream>
using namespace std;
//change the class to a template
template <class T>
class Queue
{
private:
T *arr;
int count;
public:
Queue(int size) {
arr = new T[size];
count = 0;
}
void add(T elem) {
arr[count] = elem;
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run