How to implement and link multiple template classes in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to implement and link multiple template classes in C++?

How to write a code for queue and stack template classes that uses a doubly linkedlist template class as its base? Eg: inside the stack class push,pop operations happen using linkedlist.add() and linkedlist.del() classes that used the functionality given in the linkedlist template. (P.s multiple inheritance not to be used) Please tell me how to go with the program!

18th Nov 2020, 11:30 AM
Gayathri Veeraraghavan
Gayathri Veeraraghavan - avatar
1 Answer
+ 3
I would use composition. For example: template<typename T> class LinkedList{...}; template<typename T> class Stack{ public: void push( T value ){ m_linkedList.add( value ); } private: LinkedList<T> m_linkedList; }; And the same for your Queue. You could also use inheritance but I like to avoid it when I can.
18th Nov 2020, 12:00 PM
Dennis
Dennis - avatar