Last active
July 31, 2020 09:12
-
-
Save IsaacAsante/e9e8832a61da88acc0e1caaaabe39c17 to your computer and use it in GitHub Desktop.
Updated solution for the HackerRank challenge called C++ class templates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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