You are viewing a single comment's thread. Return to all comments →
Sharing my solution here :)
template <class T> class AddElements { public: T element; AddElements(T i) {element = i;} T add(T i) {return element+i;} private: }; template <> class AddElements <string> { public: string element; AddElements(string i) {element = i;} string concatenate(string element2) {return element+element2;} private: };
I thought you're suppose to write a add() function with no input arguments?
Why? Under any condition there is always an input argument for add.
I think the specification was that it doesn't take in the first element as an input, but you'd still need to take in the second as done above.
why AddElements {} is defined again. writing concatenate in first class will work efficiently
Because 2 types of i are different. Any thoughts of fixing that?
what does template specialization do? i dont quite get it
too many lines. this works as well:
template <class T> class AddElements { public: T element; AddElements(T i) { element = i; } T add(T i) { return element+i; } T concatenate(T i) { return element+i; } };
thanks for sharing,helped a lot.
why making data member public?
Test Case 8 and 9 did not pass for me..Anyone else?
Time limit tast case failed
I have written similar code but mine is showing "Terminated due to Timeout"
Your got is correct but it takes more than 4 seconds as mention in the problem......so u have to find the better solution
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
C++ Class Templates
You are viewing a single comment's thread. Return to all comments →
Sharing my solution here :)
I thought you're suppose to write a add() function with no input arguments?
Why? Under any condition there is always an input argument for add.
I think the specification was that it doesn't take in the first element as an input, but you'd still need to take in the second as done above.
why AddElements {} is defined again. writing concatenate in first class will work efficiently
Because 2 types of i are different. Any thoughts of fixing that?
what does template specialization do? i dont quite get it
too many lines. this works as well:
thanks for sharing,helped a lot.
why making data member public?
Test Case 8 and 9 did not pass for me..Anyone else?
Time limit tast case failed
I have written similar code but mine is showing "Terminated due to Timeout"
Your got is correct but it takes more than 4 seconds as mention in the problem......so u have to find the better solution