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