In this HackerRank C++ class templates trouble in c++ programming You lot are given a main() function which takes a set of inputs. The type of input governs the kind of operation to be performed, i.e. concatenation for strings and improver for int or float. Yous need to write the form template AddElements which has a office add together() for giving the sum of int or float elements. You also demand to write a template specialization for the type string with a function concatenate() to concatenate the 2nd string to the first string.

HackerRank C++ Class Templates solution

HackerRank C++ class Templates trouble solution.

            #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <cassert> using namespace std;  #include <utility> #include <type_traits>  template <typename Type> static constexpr bool allowed_types // variable tremplate = std::is_same<Type, double>::value || std::is_same<Blazon, int>::value;  template <typename Type> class AddElements final {     static_assert(allowed_types<Type>,         "This type is non allowed. Simply (double and int are immune)");     Type _var1;  public:     explicit constexpr AddElements(const Type var1) noexcept             : _var1{ var1 } {}      constexpr Type add(const Type var2) const noexcept            { return _var1 + var2; } };  template <> class AddElements<std::cord> final {     std::string _var1;  public:     explicit AddElements(const std::string& var1) noexcept                  : _var1{ std::move(var1) } {}      std::string concatenate(const std::string& var2) const noexcept     {         return _var1 + var2;     } };  int master () {   int due north,i;   cin >> n;   for(i=0;i<n;i++) {     string blazon;     cin >> type;     if(type=="float") {         double element1,element2;         cin >> element1 >> element2;         AddElements<double> myfloat (element1);         cout << myfloat.add together(element2) << endl;     }     else if(type == "int") {         int element1, element2;         cin >> element1 >> element2;         AddElements<int> myint (element1);         cout << myint.add(element2) << endl;     }     else if(blazon == "string") {         cord element1, element2;         cin >> element1 >> element2;         AddElements<string> mystring (element1);         cout << mystring.concatenate(element2) << endl;     }   }   return 0; }                      



2d solution

            // template specialization #include <iostream> #include <string> using namespace std;  // form template: template <class T> class AddElements {     T element;   public:     AddElements (T arg) {element=arg;}     T add (T x) {return x+element;} };  // class template specialization: template <> class AddElements <string> {     cord element;   public:     AddElements (string arg) {element=arg;}     cord concatenate (string arg)     {       cord s = element+arg;       return s;     } };  int main () {   int n,i;   cin >> due north;   for(i=0;i<north;i++) {     string blazon;     cin >> blazon;     if(type=="float") {         double element1,element2;         cin >> element1 >> element2;         AddElements<double> myfloat (element1);         cout << myfloat.add(element2) << endl;     }     else if(type == "int") {         int element1, element2;         cin >> element1 >> element2;         AddElements<int> myint (element1);         cout << myint.add(element2) << endl;     }     else if(type == "string") {         string element1, element2;         cin >> element1 >> element2;         AddElements<string> mystring (element1);         cout << mystring.concatenate(element2) << endl;     }   }   render 0; }