Lab 9 - Template Classes

The purpose of this lab is to add a friend function to a template class.

Take the template_example.cpp code discussed in class and copy it to your local directory as lab9.cpp. This code implements the template version of the output operator that we discussed at the end of class on Monday. It also adds a new feature called a specific-instance output operator that I will explain at the start of lab on Tuesday.

Add the input operator to the class. The prototype for the input operator is:

template <class T>
istream& operator >> (istream &, Example<T>&);
Do not make Example a const since the input operator has to write to it. For the body of the input operator, simply read from the input stream into the data member variable of Example.

Replace main() with the following:

int main()
{
  Example<int> tInt;
  Example<double> tDouble;

  cout << "Enter an integer: ";
  cin >> tInt;

  cout << "Enter a double: ";
  cin >> tDouble;

  cout << "You entered the following: " << endl;
  cout << tInt << endl;
  cout << tDouble << endl;
  return 0;
}
Email your completed lab9.cpp to me.