Skip to content

Instantly share code, notes, and snippets.

@IsaacAsante
Last active July 31, 2020 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IsaacAsante/e9e8832a61da88acc0e1caaaabe39c17 to your computer and use it in GitHub Desktop.
Save IsaacAsante/e9e8832a61da88acc0e1caaaabe39c17 to your computer and use it in GitHub Desktop.
Updated solution for the HackerRank challenge called C++ class templates.
/* Author: Isaac Asante
* HackerRank URL for this exercise: https://www.hackerrank.com/challenges/c-class-templates/problem
* Updated on: 25 July, 2020
*/
/* Necessary optimizations for i/o speed - Don't remove */
struct Optimizer {
Optimizer() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
};
Optimizer opt;
/*Write the class AddElements here*/
template <class T>
class AddElements {
private:
T a;
public:
AddElements(T& b) {
a = b;
}
const T& add(T& n) {
a += n;
return a;
}
~AddElements() {}
};
template<>
class AddElements<string> {
private:
string a;
public:
AddElements(string& b) {
a = b;
}
const string& concatenate(string& s) {
a.append(s);
return a;
}
~AddElements() {}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment