Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.0 kB
1
Indexable
#include <iostream>
using namespace std;


template <typename T>
T add(T a, T b)
{
    return a + b;
}


template <>
string add<string>(string a, string b)
{
    return a + b;
}


template <typename T>
class Pair {
private:
    T first;
    T second;
public:
    Pair(T f, T s) : first(f), second(s) {}
    T getFirst() const
    {
        return first;
    }

    T getSecond() const 
    {
        return second;
    }
};

int main() {
  
    int num1 = 5, num2 = 10;
    cout << "Sum of numbers: " << add(num1, num2) << endl;

    string str1 = "Hello, ";
    string str2 = "World!";
    cout << "Concatenated string: " << add(str1, str2) << endl;

    
    Pair<int> intPair(2, 3);
    cout << "Pair of integers: (" << intPair.getFirst() << ", " << intPair.getSecond() << ")" << endl;

    Pair<double> doublePair(3.14, 2.71);
    cout << "Pair of doubles: (" << doublePair.getFirst() << ", " << doublePair.getSecond() << ")" << endl;

    return 0;
}