template <typename T > class sorted { T a, b; public: sorted(T, T); T min(); T max(); }; int main() { sorted <int> x(2, 1); sorted <double> y(5.4, 4.3); sorted <char> z(’A’, ’b’); cout << x.min() << ” ” << x.max() << endl; cout << y.min() << ” ” << y.max() << endl; cout << z.min() << ” ” << z.max() << endl; return 0; } template <typename T > sorted <T >::sorted(T a, T b) { if( a < b ) { this>a = a; this>b = b; } else { this>a = b; this>b = a; } } template <typename T > T sorted <T >::min() { return a; } template <typename T > T sorted <T >::max() { return b; }
|
#include <iostream > using namespace std; template <typename T, typename U > class data { public: data(T, U); }; int main() { data <string, int> x(”Cleveland”, 100); return 0; } template <typename T, typename U > data <T,U >::data(T name, U distance) { cout << name << ” ” << distance << endl; }
|