Lab 10 - Template Classes

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

Take the template_example.cpp code discussed in class and copy it to your local directory as lab10.cpp. This code implements the template version of the output operator that we discussed in class. It also adds a specific instance version of the output operator for characters that was discussed in class.

Add the addition operator to the class. The specific instance version of the addition operator will take two character versions of the class and return a C++ string. The prototype for the specific instance version of the addition operator is:

    friend string operator+(const Example<char> &, const Example<char> &);
The body of this version of the addition operator will concatenate the right character onto the left character to make the returned string. Hint, to convert the character c into the string temp, use the following conversion constructor:
    string temp(1, c);

Also add a template version of the addition operator. This will return the sum of whatever data is stored in the LHS and RHS. The prototype for the template version of the addition operator is:

    template <class U> 
    friend U operator+(const Example<U> &, const Example<U> &);

Add the following main():

int main()
{
  Example<int> tInt1, tInt2;
  Example<char> tChar1, tChar2;

  tInt1.setData(15);
  tInt2.setData(30);
  
  tChar1.setData('A');
  tChar2.setData('B');

  cout << tInt1 << "+" << tInt2 << "=" << (tInt1 + tInt2) << endl;
  cout << tChar1 << "+" << tChar2 << "=" << (tChar1 + tChar2) << endl;
  return 0;
}
Email your completed lab10.cpp to me.