fork download
  1. /*****************************************************************
  2.  // Attached: Lab11.cpp
  3.  // File: Lab11.cpp
  4.  // Programmer: Elaine Torrez
  5.  // Class: CMPR 121
  6.  *****************************************************************/
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. // Template function prototypes
  12. template <class T>
  13. void getDimensions(T& side1, T& side2);
  14.  
  15. template <class T>
  16. T calcArea(T side1, T side2);
  17.  
  18. int main()
  19. {
  20. // Integer variables
  21. int side1;
  22. int side2;
  23.  
  24. // Float variables
  25. float width;
  26. float length;
  27.  
  28. // Integer section
  29. cout << "Enter the integer dimensions of a rectangle:" << endl;
  30.  
  31. getDimensions(side1, side2);
  32.  
  33. cout << "The area equals: "
  34. << calcArea(side1, side2) << endl << endl;
  35.  
  36. // Float section
  37. cout << "Enter the float dimensions of a rectangle:" << endl;
  38.  
  39. getDimensions(width, length);
  40.  
  41. cout << "The area equals: "
  42. << calcArea(width, length) << endl;
  43.  
  44. return 0;
  45. }
  46.  
  47. /*****************************************************************
  48.  * getDimensions
  49.  * This function gets two dimensions from the user.
  50.  *****************************************************************/
  51. template <class T>
  52. void getDimensions(T& side1, T& side2)
  53. {
  54. cout << "Enter the first dimension: ";
  55. cin >> side1;
  56.  
  57. cout << "Enter the second dimension: ";
  58. cin >> side2;
  59. }
  60.  
  61. /*****************************************************************
  62.  * calcArea
  63.  * This function calculates and returns the area.
  64.  *****************************************************************/
  65. template <class T>
  66. T calcArea(T side1, T side2)
  67. {
  68. return side1 * side2;
  69. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Enter the integer dimensions of a rectangle:
Enter the first dimension: Enter the second dimension: The area equals: 1361838400

Enter the float dimensions of a rectangle:
Enter the first dimension: Enter the second dimension: The area equals: 1.5039e-09