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;
  11. int num5;
  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. float Average();
  17. int Total();
  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. int Math::Largest()
  31. {
  32. int answer = num1; // answer will be the largest we find
  33. // assume the first is the largest
  34.  
  35. if (num2 > answer) // if the second number is larger
  36. answer = num2; // then update the answer
  37.  
  38. if (num3 > answer) // now look at the third number
  39. answer = num3; // update the answer if we found a greater one
  40.  
  41. if (num4 > answer)
  42. answer = num4;
  43.  
  44. if (num5 > answer)
  45. answer = num5;
  46.  
  47. return answer; // return the answer to the caller
  48. }
  49.  
  50. int Math::Smallest()
  51. {
  52. int answer = num1; // answer will be the smallest we find
  53. // assume the first is the smallest
  54.  
  55. if (num2 < answer) // if the second number is smaller
  56. answer = num2; // then update the answer
  57.  
  58. if (num3 < answer) // now look at the third number
  59. answer = num3; // update the answer if we found a lesser one
  60.  
  61. if (num4 < answer)
  62. answer = num4;
  63.  
  64. if (num5 < answer)
  65. answer = num5;
  66.  
  67. return answer; // return the answer to the caller
  68. }
  69.  
  70. float Math::Average()
  71. {
  72. float answer = ((num1 + num2 + num3 + num4 + num5) / 5);
  73. return answer;
  74. }
  75.  
  76. int Math::Total()
  77. {
  78. int answer = num1 + num2 + num3 + num4 + num5;
  79. return answer;
  80. }
  81.  
  82. int main() {
  83. Math Object1 (10, 20, 30, 40, 50); // The object type is Math, the object is called Object1
  84. Math Object2 (5, 10, 6, 15, 14); // The object type is Math, the object is called Object2
  85.  
  86. // find the largest number in the first object (Object1) and print it out
  87. // use the cout object to print the information
  88.  
  89. int large;
  90. large = Object1.Largest();
  91.  
  92. int small = Object1.Smallest();
  93. int average = Object1.Average();
  94. int total = Object1.Total();
  95.  
  96. cout << "Largest is " << large << endl;
  97. cout << "Smallest is " << small << endl;
  98. cout << "Average is " << average << endl;
  99. cout << "Total is " << total << endl;
  100.  
  101.  
  102. // now do the same for the second object (Object2)
  103. large = Object2.Largest();
  104. small = Object2.Smallest();
  105. average = Object2.Average();
  106. total = Object2.Total();
  107.  
  108. cout << "Largest is " << large << endl;
  109. cout << "Smallest is " << small << endl;
  110. cout << "Average is " << average << endl;
  111. cout << "Total is " << total << endl;
  112.  
  113. // all done, so return
  114. return 0;
  115. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Largest is 50
Smallest is 10
Average is 30
Total is 150
Largest is 15
Smallest is 5
Average is 10
Total is 50