fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Math
  6. {
  7. private:
  8. int num1; // one of the private data numbers
  9. int num2; // another one
  10. int num3; // the third one
  11. int num4; // the fourth number
  12. int num5; //the fifth number
  13. public:
  14. Math (int first, int second, int third, int fourth, int fifth); // the class constructor
  15. int Largest(); // member to return the largest number
  16. int Smallest();
  17. float Average();
  18. int Total();
  19. };
  20.  
  21. Math::Math (int first, int second, int third, int fourth, int fifth)
  22. {
  23. num1 = first; // save the first int
  24. num2 = second ; // save the second int
  25. num3 = third; // save the third int
  26. num4 = fourth;
  27. num5 = fifth;
  28. return;
  29. }
  30.  
  31. int Math::Largest ()
  32. {
  33. return max({num1, num2, num3, num4, num5});
  34. }
  35.  
  36. int Math::Smallest ()
  37. {
  38. return min({num1, num2, num3, num4, num5});
  39. }
  40. int Math::Total()
  41. {
  42. return num1+num2+num3+num4+num5;
  43. }
  44. float Math::Average()
  45. {
  46. return (num1+num2+num3+num4+num5)/5.0;
  47. }
  48. //
  49. // A test main to show it works
  50.  
  51. int main ()
  52. {
  53. // make two objects to hold the numbers using the user defined data type
  54. // ... The value for num1, num2, and num3 will get "constructed" with Object1
  55. // and Object2 thanks to our class member function Math
  56. Math Object1 (10, 20, 30, 11, 15); // The object type is Math, the object is
  57. // called Object1
  58. Math Object2 (5, 10, 6, 9, 8); // The object type is Math, the object is
  59. // called Object2
  60. // find the largest number in the first object (Object1) and print it out
  61. // use the cout object to print the information
  62. int solution;
  63. float sol2;
  64. sol2 = Object1.Average();
  65. cout << "Largest is " << sol2 << endl;
  66. // now do the same for the second object (Object2)
  67. solution = Object2.Largest();
  68. cout << "Largest is " << solution << endl;
  69. // all done, so return
  70. return 0;
  71. }
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Largest is 17.2
Largest is 10