CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
C++ Code Walkthrough #88

Two Type Parameters, One Class Template

easyTemplates

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <string>
3 
4template <typename K, typename V>
5class Pair {
6 K m_key;
7 V m_value;
8public:
9 Pair(K key, V value) : m_key(key), m_value(value) {}
10 void print() const {
11 std::cout << m_key << " -> " << m_value << "\n";
12 }
13};
14 
15int main() {
16 Pair<std::string, int> age("Ana", 30);
17 Pair<int, double> price(7, 2.25);
18 Pair<char, char> letters('a', 'b');
19 age.print();
20 price.print();
21 letters.print();
22 Pair<int, int> whole(1, 2);
23 whole.print();
24 return 0;
25}

🎯 Your Answer