Algorithm


Problem Name: C++ Class Template Specialization

Problem Link: hhttps://www.hackerrank.com/challenges/cpp-class-template-specialization/problem?isFullScreen=true

In this HackerRank Functions in C++ programming problem solution,

You are given a main function which reads the enumeration values for two different types as input, then prints out the corresponding enumeration names. Write a class template that can provide the names of the enumeration values for both types. If the enumeration value is not valid, then print unknown.

Input Format

The first line contains

, the number of test cases.
Each of the subsequent lines contains two space-separated integers. The first integer is a color value, , and the second integer is a fruit value f.

Constraints

  • 1 <= t <= 100
  • -2 * 10*9 <= c <= 2 * 10**9
  • -2 * 10*9 <= f <= 2 * 10**9

Output Format

The locked stub code in your editor prints t lines containing the color name and the fruit name corresponding to the input enumeration index.

Sample Input

2
1 0
3 3

Sample Output

green apple
unknown unknown

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;

template < class T>
    class AddElements{
        private:
            T a;
        public:
            AddElements(T arg){
                a = arg;
            }
            T add(T b){
                return a += b;
            }
            T concatenate(T b){
                return a += b;
            }
    };

int main(void)
{
    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;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
2 1 0 3 3

Output

x
+
cmd
green apple unknown unknown
Advertisements

Demonstration


Previous
[Solved] Operator Overloading in C++ solution in Hackerrank - Hacerrank solution C++
Next
[Solved] C++ Variadics in C++ solution in Hackerrank - Hacerrank solution C++