Hackerrank C++ Class Templates Solution

Hackerrank C++ Class Templates Solution

A class template provides a specification for generating classes based on parameters. Class templates are generally used to implement containers. A class template is instantiated by passing a given set of types to it as template arguments. Here is an example of a class, MyTemplate, that can store one element of any type and that has just one member function divideBy2, which divides its value by 2. template <class T> class MyTemplate { T element; public: MyTemplate (T arg) {element=arg;} T divideBy2 () {return element/2;} };

It is also possible to define a different implementation of a template for a specific type. This is called Template Specialization. For the template given above, we find that a different implementation for type char will be more useful, so we write a function printElement to print the char element:// class template specialization: template <> class MyTemplate <char> { char element; public: MyTemplate (char arg) {element=arg;} char printElement () { return element; } };

You 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 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 type string with a function concatenate() to concatenate the second string to the first string.

Input Format.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

The first line contains an integer . Input will consist of  lines where  is the number given in the first line of the input followed by  lines.

Each of the next  line contains the type of the elements provided and depending on the type, either two strings or two integers or two floating point numbers will be given. The type will be one of int, float or string types only. Out of the following two elements, you have to concatenate or add the second element to the first element.

Constraints.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}


, where  is any float value
, where valueint is any int value
, where  is the length of any string

The time limit for this challenge is 4 seconds.

Output Format

The code provided in the code editor will use your class template to add/append elements and give the output.

Sample Input

3
string John Doe
int 1 2
float 4.0 1.5

Sample Output

JohnDoe
3
5.5

Explanation

"Doe" when appended with "John" gives "JohnDoe". 2 added to 1 gives 3, and 1.5 added to 4.0 gives 5.5.

Solution in cpp

Approach 1.

/*Write the class AddElements here*/
template <class t>
class AddElements {
    private:
    t Element1,Element2,x;
    public:
    AddElements(t x){Element1=x;}
    t add(t y){
        return(Element1+y);}
    t concatenate(t z){return (Element1+z);}
};

Approach 2.

/*Write the class AddElements here*/
template<class T>
class AddElements{
    T element;
    public:
    AddElements(T _element){
        element=_element;
    }
    T add(T element2){
        return element+element2;
        
    }
    T concatenate(T element2){
        return element+element2;
    }
    
};

Approach 3.

/*Write the class AddElements here*/
template<class T>
class AddElements {
private:
    T first, second;
public:
    AddElements(T satu) {
        first = satu;
    }
    T add(T dua) {
        second = dua;
        return (first + dua);
    }
    T concatenate(T dua) {
        second = dua;
        return (first + second);
    }
};

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe