fork download
  1. class Math
  2. {
  3. private:
  4. int num1; // one of the private data numbers
  5. int num2; // another one
  6. int num3; // the third one
  7. int num4; // the fourth one
  8. int num5; // the fifth one
  9. public:
  10. Math (int first, int second, int third, int fourth, int fifth); // the class constructor
  11. int Largest (); // member to return the largest number
  12. int Smallest ();// member to return the smallest number
  13.  
  14. };
  15. // definitions for the class member functions follow.
  16. // The syntax of each of the member functions in the class is in the form:
  17. //
  18. // <return type> <class name>::<function name> <function arguments>
  19. //
  20. // There are two member functions. The first is a function automatically
  21. // called when you make an instance of the class. It is called a class
  22. // constructor and its job is to initialize the data in the class to the
  23. // values provided by the caller. Note that there is NEVER a return type
  24. // for a constructor
  25. // The first member function is Math. Its job is to initialize the data
  26. // in the class to those values provided by whoever calls it. Note that
  27. // there are no defaults provided here, so all five values must be
  28. // provided
  29. Math::Math (int first, int second, int third, int fourth, int fifth)
  30. {
  31. num1 = first; // save the first int
  32. num2 = second ; // save the second int
  33. num3 = third; // save the third int
  34. num4 = fourth; // save the fourth int
  35. num5 = fifth; // save the fifth int
  36. return;
  37. }
  38. //
  39. // The second member function is Largest. It examines the data held by the
  40. // class and returns the largest of the three data values.
  41. //
  42. int Math::Largest ()
  43. {
  44. int answer; // answer will be the largest we find
  45. answer = num1; // assume the first is the largest
  46. if (num2 > answer) // if the second number is larger
  47. answer = num2; // then update the answer
  48. if (num3 > answer) // now look at the third number
  49. answer = num3; // update the answer if we found a greater one
  50. if (num4 > answer) // now look at the fouth number
  51. answer = num4; // update the answer if we found a greater one
  52. if (num5 > answer) // now look at the fifth number
  53. answer = num5; //update the answer if we found a greater one
  54. return answer; // return the answer to the caller
  55. }
  56. int Math::Smallest ()
  57. {
  58. int answer; // answer will be the smallest we find
  59. answer = num1; // assume the first is the smallest
  60. if (num2 < answer) // if the second number is smaller
  61. answer = num2; // then update the answer
  62. if (num3 < answer) // now look at the third number
  63. answer = num3; // update the answer if we found a lesser one
  64. if (num4 < answer) // now look at the fouth number
  65. answer = num4; // update the answer if we found a lesser one
  66. if (num5 < answer) // now look at the fifth number
  67. answer = num5; //update the answer if we found a lesser one
  68. return answer; // return the answer to the caller
  69. }
  70. //
  71. // A test main to show it works
  72. #include <iostream>
  73. using namespace std;
  74. int main ()
  75. {
  76. // make two objects to hold the numbers using the user defined data type
  77. // ... The value for num1, num2, num3, num4, and num5 will get "constructed" with // Object1
  78. // and Object2 thanks to our class member function Math
  79. Math Object1 (10, 20, 30, 40, 50); // The object type is Math, the object is
  80. // called Object1
  81. Math Object2 (5, 10, 6, 12, 7); // The object type is Math, the object is
  82. // called Object2
  83. // find the largest number in the first object (Object1) and print it out
  84. // use the cout object to print the information
  85. int solution;
  86. solution = Object1.Largest();
  87. cout << "Largest is " << solution << endl;
  88. // now do the same for the second object (Object2)
  89. solution = Object2.Largest();
  90. cout << "Largest is " << solution << endl;
  91. // now the same for all the lesser values
  92. solution = Object1.Smallest();
  93. cout << "Smallest is " << solution << endl;
  94. solution = Object2.Smallest();
  95. cout << "Smallest is " << solution << endl;
  96. // all done, so return
  97. return 0;
  98. }
  99.  
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
Largest is 50
Largest is 12
Smallest is 10
Smallest is 5