The Pair class template Practice creating a class template by writing a template
ID: 3869156 • Letter: T
Question
The Pair class template
Practice creating a class template by writing a template for a class named "Pair". This class will represent a pair of data members of a type that is parameterized in the template definition. For example, you could have a Pair of integers, a Pair of doubles, etc.
Use this driver to test your program:
// your file prologue goes here
#include <iostream>
#include "pair.h"
using namespace std;
int main( )
{
Pair<char> letters('a', 'd');
cout << " The first letter is: " << letters.getFirst( );
cout << " The second letter is: " << letters.getSecond( );
cout << endl;
system("Pause");
return 0;
}
Explanation / Answer
template<class T, class U>
struct pair
{
using first_type = T;
using second_type = U;
T first;
U second;
};
template<>
struct pair<int,string>
{
using first_type = int;
using second_type = string;
int first;
string second;
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.