tempover.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // tempover.cpp --- template overloading
  2. #include <iostream>
  3. template <typename T> // template A
  4. void ShowArray(T arr[], int n);
  5. template <typename T> // template B
  6. void ShowArray(T * arr[], int n);
  7. struct debts
  8. {
  9. char name[50];
  10. double amount;
  11. };
  12. int main()
  13. {
  14. using namespace std;
  15. int things[6] = {13, 31, 103, 301, 310, 130};
  16. struct debts mr_E[3] =
  17. {
  18. {"Ima Wolfe", 2400.0},
  19. {"Ura Foxe", 1300.0},
  20. {"Iby Stout", 1800.0}
  21. };
  22. double * pd[3];
  23. // set pointers to the amount members of the structures in mr_E
  24. for (int i = 0; i < 3; i++)
  25. pd[i] = &mr_E[i].amount;
  26. cout << "Listing Mr. E's counts of things:\n";
  27. // things is an array of int
  28. ShowArray(things, 6); // uses template A
  29. cout << "Listing Mr. E's debts:\n";
  30. // pd is an array of pointers to double
  31. ShowArray(pd, 3); // uses template B (more specialized)
  32. // cin.get();
  33. return 0;
  34. }
  35. template <typename T>
  36. void ShowArray(T arr[], int n)
  37. {
  38. using namespace std;
  39. cout << "template A\n";
  40. for (int i = 0; i < n; i++)
  41. cout << arr[i] << ' ';
  42. cout << endl;
  43. }
  44. template <typename T>
  45. void ShowArray(T * arr[], int n)
  46. {
  47. using namespace std;
  48. cout << "template B\n";
  49. for (int i = 0; i < n; i++)
  50. cout << *arr[i] << ' ';
  51. cout << endl;
  52. }