Class Templates - Example 10-9 - Template function with a user defined type

// File: ex10-9.cpp

#include <iostream>
using namespace std;

template <typename T>
class thing
{
  private:
	  T x;
  public:
	  thing();
	  thing(T);
	  thing(const thing&);
	  T get() const;
	  operator T() const;
};

template <typename T>
thing<T>::thing() : x(0) {}

template <typename T>
thing<T>::thing(T n) : x(n) {}

template <typename T>
thing<T>::thing(const thing<T>& t) : x(t.x) {}

template <typename T>
T thing<T>::get() const
{
	return x;
}

template <typename T>
thing<T>::operator T() const
{
	return x;
}

template <typename T>
ostream& operator<<(ostream& s, const thing<T>& t)
{
	return s << t.get();
}

int main(void)
{
	thing<int> t1;
	cout << "t1=" << t1 << endl;

	thing<int> t2(18);
	cout << "t2=" << t2 << endl;

	thing<double> t3(1.28);
	cout << "t3=" << t3 << endl;

	thing<double> t4(t3);
	cout << "t4=" << t4 << endl;

	cout << "(t2.get() + t3.get()) = " << (t2.get() + t3.get()) << endl;
	cout << "t2 + t3 = " << t2 + t3 << endl;

	thing<char> t5('z');
	cout << "t5=" << t5 << endl;
	
	return 0;
}

/***** Output *****
t1=0
t2=18
t3=1.28
t4=1.28
(t2.get() + t3.get()) = 19.28
t2 + t3 = 19.28
t5=z
*********************/