tempmemb.cpp 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // tempmemb.cpp -- template members
  2. #include <iostream>
  3. using std::cout;
  4. using std::endl;
  5. template <typename T>
  6. class beta
  7. {
  8. private:
  9. template <typename V> // nested template class member
  10. class hold
  11. {
  12. private:
  13. V val;
  14. public:
  15. hold(V v = 0) : val(v) {}
  16. void show() const { cout << val << endl; }
  17. V Value() const { return val; }
  18. };
  19. hold<T> q; // template object
  20. hold<int> n; // template object
  21. public:
  22. beta( T t, int i) : q(t), n(i) {}
  23. template<typename U> // template method
  24. U blab(U u, T t) { return (n.Value() + q.Value()) * u / t; }
  25. void Show() const { q.show(); n.show();}
  26. };
  27. int main()
  28. {
  29. beta<double> guy(3.5, 3);
  30. cout << "T was set to double\n";
  31. guy.Show();
  32. cout << "V was set to T, which is double, then V was set to int\n";
  33. cout << guy.blab(10, 2.3) << endl;
  34. cout << "U was set to int\n";
  35. cout << guy.blab(10.0, 2.3) << endl;
  36. cout << "U was set to double\n";
  37. cout << "Done\n";
  38. // std::cin.get();
  39. return 0;
  40. }