tempparm.cpp 939 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // tempparm.cpp – templates as parameters
  2. #include <iostream>
  3. #include "stacktp.h"
  4. template <template <typename T> class Thing>
  5. class Crab
  6. {
  7. private:
  8. Thing<int> s1;
  9. Thing<double> s2;
  10. public:
  11. Crab() {};
  12. // assumes the thing class has push() and pop() members
  13. bool push(int a, double x) { return s1.push(a) && s2.push(x); }
  14. bool pop(int & a, double & x){ return s1.pop(a) && s2.pop(x); }
  15. };
  16. int main()
  17. {
  18. using std::cout;
  19. using std::cin;
  20. using std::endl;
  21. Crab<Stack> nebula;
  22. // Stack must match template <typename T> class thing
  23. int ni;
  24. double nb;
  25. cout << "Enter int double pairs, such as 4 3.5 (0 0 to end):\n";
  26. while (cin>> ni >> nb && ni > 0 && nb > 0)
  27. {
  28. if (!nebula.push(ni, nb))
  29. break;
  30. }
  31. while (nebula.pop(ni, nb))
  32. cout << ni << ", " << nb << endl;
  33. cout << "Done.\n";
  34. // cin.get();
  35. // cin.get();
  36. return 0;
  37. }