Question: The syntax for class templates is basically the same as that for function templates: / / Class for a pair of values of type T:

The syntax for class templates is basically the same as that for function templates:
// Class for a pair of values of type T:
template
class Pair
{
public:
Pair();
Pair(T first_value, T second_value);
void set_element(int position, T value);
T_get_element(int position) const;
private:
T first;
T second;'
};
Here are the definitions for the member functions set_element and the constructor Pair with two arguments.
// Use iostream and cstdlib
template
void Pair::set_element(int position, T value)
{
if(position ==1)
first = value;
else if(position ==2)
second = value;
else
{
cout << "Error: Illegal pair position.
";
exit(1);
{
}
template
Pair::Pair(T first_value, T second_value) : first(first_value), second(second_value)
{
// empty body
}
Requirement:
1. Write the definitions for the member functions get_element for the class template Pair and the constructor Pair() with no argument. Add to the class a member function called display_pairs that displays the pairs.
2. Write a complete program that uses the class to read a pair of integers and a pair of strings and assign them to the appropriate objects, grades and names.
Here is an example of an input pair:
first pairs:
8790
second pairs:
Tom Martha

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!