C++ Class Templates in C++ - Hacker Rank Solution


C++ Class Templates in C++ - Hacker Rank Solution


Problem


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.

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:

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 :

The first line contains an integer n. Input will consist of n+1 lines where n is the number given in the first line of the input followed by n lines.
Each of the next n 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 :

  • 1 <= n <= 5*10^5
  • 1.0 <= value float <= 10.0, where value float is any float value.
  • 1 <= value int <= 10^5, where value int is any int value.
  • 0 <= len string <= 10, where len string 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 :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// C++ Class Templates in C++ - Hacker Rank Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;

/* C++ Class Templates in C++ - Hacker Rank Solution START */
// class 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> {
    string element;
  public:
    AddElements (string arg) {element=arg;}
    string concatenate (string arg)
    {
      string s = element+arg;
      return s;
    }
};
/* C++ Class Templates in C++ - Hacker Rank Solution END */

int main () {
  int n,i;
  cin >> n;
  for(i=0;i<n;i++) {
    string type;
    cin >> type;
    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;
    }
  }
  return 0;
}

Solution 2 :-

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//C++ Class Templates in C++ - Hacker Rank Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;

/*Write the class AddElements here*/
/* C++ Class Templates in C++ - Hacker Rank Solution START */
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:
        
};

/* C++ Class Templates in C++ - Hacker Rank Solution END */

int main () 
{
  int n,i;
  cin >> n;
  for(i=0;i<n;i++) 
  {
    string type;
    cin >> type;
    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;
    }
  }
  return 0;
}

Note :- This problem solution is get affected by the execution time of progrm. if the execution time take delay then the cases of this solution get wrong(faild). if any have idea about this solution then comment the solution.





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.


Next Post Previous Post
10 Comments
  • Anonymous
    Anonymous Tuesday, June 23, 2020

    it's work
    template
    class AddElements{
    T x;
    public:
    AddElements(T a){x=a;}
    T add(T b){return x+b;}
    };

    template<>
    class AddElements{
    string x;
    public:
    AddElements(string a){x=a;}
    string concatenate(string b){return x+b;}
    };

    • CodeWorld19
      CodeWorld19 Tuesday, June 23, 2020

      The both solutions are same only variable and parameters name are different. It's not affecte the output of program

  • Anonymous
    Anonymous Sunday, July 05, 2020

    Finally I have got correct solutions thanks for this.

  • Unknown
    Unknown Tuesday, January 12, 2021

    https://www.hackerrank.com/challenges/c-class-templates/problem
    for both cases TLE happens

  • Unknown
    Unknown Monday, May 24, 2021

    #define cin ios_base::sync_with_stdio(false);cin.tie(NULL); cin
    attach this method

  • Uday kiran
    Uday kiran Tuesday, October 12, 2021

    If only the 1st template is used , the time limit exceeds. Can someone explain this please?

  • Raunak raj
    Raunak raj Monday, February 14, 2022

    This comment has been removed by the author.

  • RAMNEVAS CHAURASIYA
    RAMNEVAS CHAURASIYA Thursday, March 17, 2022

    template
    class AddElements{
    T a;
    public:
    AddElements (T x){a=x;}
    T add (T y){return a+y;}

    T concatenate (T arg){
    return a+arg;}
    };

    • RAMNEVAS CHAURASIYA
      RAMNEVAS CHAURASIYA Thursday, March 17, 2022

      This comment has been removed by the author.

  • Mr Aman Yadav
    Mr Aman Yadav Wednesday, June 29, 2022

    int start_up() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
    }

    int static r = start_up();

    #define endl '\n';

Add Comment
comment url