fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Math
  5. {
  6. private:
  7. int num1; // one of the private data numbers
  8. int num2; // another one
  9. int num3; // the third one
  10. int num4; // the fourth one
  11. int num5; // the fifth one
  12. public:
  13. Math (int first, int second, int third, int fourth, int fifth); // the class constructor
  14. int Largest (); // member to return the largest number
  15. int Smallest ();
  16. int Total ();
  17.  
  18. };
  19.  
  20. Math::Math (int first, int second, int third, int fourth, int fifth)
  21. {
  22. num1 = first; // save the first int
  23. num2 = second ; // save the second int
  24. num3 = third; // save the third int
  25. num4 = fourth; // save the fourth int
  26. num5 = fifth; //save the fifth int
  27. return;
  28. }
  29. //
  30. // The second member function is Largest. It examines the data held by the
  31. // class and returns the largest of the three data values.
  32. //
  33. int Math::Largest ()
  34. {
  35. int answer; // answer will be the largest we find
  36. answer = num1; // assume the first is the largest
  37. if (num2 > answer) // if the second number is larger
  38. answer = num2; // then update the answer
  39. if (num3 > answer) // now look at the third number
  40. answer = num3; // update the answer if we found a greater one
  41. if (num4 > answer)
  42. answer = num4;
  43. if (num5 > answer)
  44. answer = num5;
  45. return answer; // return the answer to the caller
  46. }
  47.  
  48. int Math::Smallest () {
  49. int answer; // answer will be the smallest we find
  50. answer = num1; // assume the first is the largest
  51. if (num2 < answer) // if the second number is larger
  52. answer = num2; // then update the answer
  53. if (num3 < answer) // now look at the third number
  54. answer = num3; // update the answer if we found a greater one
  55. if (num4 < answer)
  56. answer = num4;
  57. if (num5 < answer)
  58. answer = num5;
  59. return answer; // return the answer to the caller
  60. }
  61. int Math::Total () {
  62. int answer; // answer will be the smallest we find
  63. answer = num1 + num2 + num3 + num4 + num5; // assume the first is the largest
  64. return answer;
  65. }
  66. #include <iostream>
  67. using namespace std;
  68. int main ()
  69.  
  70. {
  71. Math Object1 (10, 20, 30, 90, 150); // The object type is Math, the object is
  72. // called Object1
  73. Math Object2 (5, 10, 6, 3, 6); // The object type is Math, the object is
  74. // called Object2
  75. int solution;
  76. solution = Object1.Largest();
  77. cout << "Largest is " << solution << endl;
  78. // now do the same for the second object (Object2)
  79. solution = Object2.Largest();
  80. cout << "Largest is " << solution << endl;
  81. solution = Object1.Smallest();
  82. cout << "Smallest is " << solution << endl;
  83. // now do the same for the second object (Object2)
  84. solution = Object2.Smallest();
  85. cout << "Smallest is " << solution << endl;
  86. solution = Object1.Total();
  87. cout << "Total is " << solution << endl;
  88. // now do the same for the second object (Object2)
  89. solution = Object2.Total();
  90. cout << "Total is " << solution << endl;
  91. return 0;
  92. }
  93.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Largest is 150
Largest is 10
Smallest is 10
Smallest is 3
Total is 300
Total is 30