In this HackerRank C++ Class Tempates problem in c++, You are given a main() function that takes a set of inputs. The type of input governs the kind of operation to be performed, i.e. concatenation for strings and addition for int or float. You need to write the class template AddElements which has a function add() for giving the sum of int or float elements. You also need to write a template specialization for the typed string with a function concatenate() to concatenate the second string to the first string.

HackerRank C++ Class Templates problem solution

HackerRank C++ Class Templates problem solution

template <class T>
    class AddElements{
        T element1;
        public:
          AddElements(T e):element1(e){}
          T add (T e2){
              return element1 + e2;
          }  
};

template <>
class AddElements <string> {
    string element;
  public:
    AddElements (string arg) {element=arg;}
    string concatenate (string e)
    {
      return element + e;
    }
};