0
What does this code means??
Hello guys i've solved this problem but i still don't understand this line of code in the 14th line. #include <iostream> #include <string> 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]; //what does it means?? count = 0; } void add(T elem) { arr[count] = elem; count++; } void get(int index) { cout << arr[index]<<endl; } }; int main() { Queue<string> q(4); q.add("James"); q.add("Andy"); q.add("Amy"); q.get(2); Queue <int> n(2); n.add(42); n.add(33); n.get(1); return 0; }
2 Answers
+ 3
Hi Anoir,
I see you have completed C++ lesson, perhaps you need to review the "Templates, Exception and Files" module, class template specifically
https://www.sololearn.com/learn/CPlusPlus/1916/
+ 1
Anoir B'f
Here we are initialising array of length 'size' where size is given.
Here T is generic type means it can be anything like string or int
You can understand through this example:.
string * arr = new string [size];
int * arr = new int[size];