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

Three Type Parameters With Explicit Arguments

easyTemplates

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <string>
3 
4template <typename A, typename B, typename C>
5class Triple {
6 A m_first;
7 B m_second;
8 C m_third;
9public:
10 Triple(A first, B second, C third) : m_first(first), m_second(second), m_third(third) {}
11 void print() const {
12 std::cout << "(" << m_first << ", " << m_second << ", " << m_third << ")\n";
13 }
14 C third() const { return m_third; }
15};
16 
17int main() {
18 Triple<int, double, char> t1(1, 2.5, 'z');
19 Triple<std::string, int, int> t2("id", 42, 7);
20 t1.print();
21 t2.print();
22 std::cout << t1.third() << "\n";
23 std::cout << t2.third() + 1 << "\n";
24 return 0;
25}

🎯 Your Answer