fork download
  1. //**************************************************************
  2. // Class: Math
  3. //
  4. // Purpose: Creates a given object with five integer values.
  5. // Provides member functions to find the largest and
  6. // smallest value in a given object.
  7. //
  8. // Member Functions:
  9. //
  10. // Math - the constructor
  11. // Largest - Determines the largest value in this object
  12. // Smallest - Determines the smallest value in this object
  13. //
  14. //**************************************************************
  15. class Math
  16. {
  17. private:
  18.  
  19. int num1; // one of the numbers
  20. int num2; // another one
  21. int num3; // the third one
  22.  
  23. // TODO - Add two more data members of type int
  24. int num4; // the fourth one
  25. int num5; // the fifth one
  26.  
  27. public:
  28.  
  29. // TODO - Extend this from three to five total numbers
  30. Math (int first, int second, int third, int fourth, int fifth); // the class constructor
  31.  
  32. int Largest (); // member function prototype to return the largest number
  33.  
  34. // TODO - Add a member function prototype called Smallest that will find
  35. // the smallest of our 5 numbers in our object
  36. int Smallest ();
  37. };
  38.  
  39.  
  40. // definitions for the class member functions follow.
  41.  
  42. // The syntax of each of the member functions in the class is in the form|
  43. // <return type> <class name>::<function name> <function arguments>
  44. //
  45. // There are three member functions. The first is a function automatically called
  46. // when you make an instance of the class. It is called a class constructor and
  47. // its job is to initialize the data in the class to the values provided by the
  48. // caller. Note that there is NEVER a return type for a constructor
  49.  
  50. // The first member function is Math. Its job is to initialize the data in the class
  51. // to those values provided by whoever calls it. Note that there are no defaults
  52. // provided here, so all five values must be provided.
  53.  
  54. Math::Math (int first, int second, int third, int fourth, int fifth)
  55. {
  56. num1 = first; // save the first int
  57. num2 = second; // save the second int
  58. num3 = third; // save the third int
  59.  
  60.  
  61. // TODO - Add two more numbers here
  62. num4 = fourth; // save the fourth int
  63. num5 = fifth; // save the fifth int
  64.  
  65. return;
  66. }
  67.  
  68. //
  69. // The second member function is Largest. It examines the data held by the
  70. // class and returns the largest of the five data values.
  71. //
  72.  
  73. // TODO - Update this function to find the largest of 5 numbers
  74. int Math::Largest ()
  75. {
  76. int answer; // answer will be the largest we find
  77.  
  78. answer = num1; // assume the first is the largest
  79.  
  80. if (num2 > answer) // if the second number is larger
  81. answer = num2; // then update the answer
  82.  
  83. if (num3 > answer) // now look at the third number
  84. answer = num3; // update the answer if we found a greater one
  85.  
  86. if (num4 > answer) // now look at the fourth number
  87. answer = num4; // update the answer if we found a greater one
  88.  
  89. if (num5 > answer) // now look at the fifth number
  90. answer = num5; // update the answer if we found a greater one
  91.  
  92. return answer; // return the answer to the caller
  93. }
  94.  
  95. //
  96. // The third member function is Smallest. It examines the data held by the
  97. // class and returns the smallest of the five data values.
  98. //
  99. // TODO - Create a similar member function called "Smallest" to find the smallest
  100. // of 5 numbers
  101. int Math::Smallest ()
  102. {
  103. int answer; // answer will be the smallest we find
  104.  
  105. answer = num1; // assume the first is the smallest
  106.  
  107. if (num2 < answer) // if the second number is smaller
  108. answer = num2; // then update the answer
  109.  
  110. if (num3 < answer) // now look at the third number
  111. answer = num3; // update the answer if we found a smaller one
  112.  
  113. if (num4 < answer) // now look at the fourth number
  114. answer = num4; // update the answer if we found a smaller one
  115.  
  116. if (num5 < answer) // now look at the fifth number
  117. answer = num5; // update the answer if we found a smaller one
  118.  
  119. return answer; // return the answer to the caller
  120. }
  121.  
  122. // A main function to verify it works
  123.  
  124. #include <iostream>
  125. using namespace std;
  126.  
  127. int main ()
  128. {
  129. // make three objects to hold the numbers with the user defined data type
  130. // ... The values for num1, num2, num3, numb4, and num5 will get "constructed"
  131. // with Object1, Object2, and Object3 thanks to our class member function Math
  132.  
  133. // TODO - Add in two more numbers into our constructor call for a total of 5
  134. Math Object1(10, 20, 30, 40, 50); // The object type is Math, the object is
  135. // called Object1
  136.  
  137. // TODO - Add in two more numbers into our constructor call for a total of 5
  138. Math Object2(5, 10, 6, 7, 11); // The object type is Math, the object is
  139. // called Object2
  140.  
  141. // TODO - Create Object3 with 5 integer numbers that you decide
  142. Math Object3(95, 111, 333, 555, 777);
  143.  
  144. // find the largest number in the first object and print it out
  145. // using the cout object to print the information
  146.  
  147. int solution; // holds return value from our member function calls
  148.  
  149. solution = Object1.Largest();
  150. cout << "Largest in Object1 is " << solution << endl;
  151.  
  152. // TODO - Repeat the previous two statements, but call the Smallest
  153. // member function ... then print the smallest value for Object1
  154. solution = Object1.Smallest();
  155. cout << "Smallest in Object1 is " << solution << endl;
  156.  
  157. // now do the same for the second object
  158. solution = Object2.Largest();
  159. cout << "Largest in Object2 is " << solution << endl;
  160.  
  161. // TODO - Repeat the previous two statements, but call the Smallest
  162. // member function ... than print the smallest value for Object2
  163. solution = Object2.Smallest();
  164. cout << "Smallest in Object2 is " << solution << endl;
  165. // now do the same for the third object
  166.  
  167. // TODO - Repeat the previous statements peformed with Object1 and Object2,
  168. // but find and print the the largest and smallest in our object
  169. // called Object3
  170. solution = Object3.Largest();
  171. cout << "Largest in Object3 is " << solution << endl;
  172.  
  173. solution = Object3.Smallest();
  174. cout << "Smallest in Object3 is " << solution << endl;
  175.  
  176. // all done, so return
  177. return 0;
  178. }
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
Largest in Object1 is 50
Smallest in Object1 is 10
Largest in Object2 is 11
Smallest in Object2 is 5
Largest in Object3 is 777
Smallest in Object3 is 95