Question: Class Templates You are making a queue management system and need to support different data types - for example, you should be able to queue
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
#include
using namespace std;
//change the class to a template
class Queue
{
private:
int *arr;
int count;
public:
Queue(int size) {
arr = new int[size];
count = 0;
}
void add(int elem) {
arr[count] = elem;
count++;
}
void get(int index) {
cout << arr[index]< } }; int main() { Queue q.add("James"); q.add("Andy"); q.add("Amy"); q.get(2); Queue n.add(42); n.add(33); n.get(1); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
