class Math
{
private:
int num1; // one of the private data numbers
int num2; // another one
int num3; // the third one
int num4; // the fourth one
int num5; // the fifth one
public:
Math (int first, int second, int third, int fourth, int fifth); // the class constructor
int Largest (); // member to return the largest number
int Smallest ();// member to return the smallest number
};
// definitions for the class member functions follow.
// The syntax of each of the member functions in the class is in the form:
//
// <return type> <class name>::<function name> <function arguments>
//
// There are two member functions. The first is a function automatically
// called when you make an instance of the class. It is called a class
// constructor and its job is to initialize the data in the class to the
// values provided by the caller. Note that there is NEVER a return type
// for a constructor
// The first member function is Math. Its job is to initialize the data
// in the class to those values provided by whoever calls it. Note that
// there are no defaults provided here, so all five values must be
// provided
Math::Math (int first, int second, int third, int fourth, int fifth)
{
num1 = first; // save the first int
num2 = second ; // save the second int
num3 = third; // save the third int
num4 = fourth; // save the fourth int
num5 = fifth; // save the fifth int
return;
}
//
// The second member function is Largest. It examines the data held by the
// class and returns the largest of the three data values.
//
int Math::Largest ()
{
int answer; // answer will be the largest we find
answer = num1; // assume the first is the largest
if (num2 > answer) // if the second number is larger
answer = num2; // then update the answer
if (num3 > answer) // now look at the third number
answer = num3; // update the answer if we found a greater one
if (num4 > answer) // now look at the fouth number
answer = num4; // update the answer if we found a greater one
if (num5 > answer) // now look at the fifth number
answer = num5; //update the answer if we found a greater one
return answer; // return the answer to the caller
}
int Math::Smallest ()
{
int answer; // answer will be the smallest we find
answer = num1; // assume the first is the smallest
if (num2 < answer) // if the second number is smaller
answer = num2; // then update the answer
if (num3 < answer) // now look at the third number
answer = num3; // update the answer if we found a lesser one
if (num4 < answer) // now look at the fouth number
answer = num4; // update the answer if we found a lesser one
if (num5 < answer) // now look at the fifth number
answer = num5; //update the answer if we found a lesser one
return answer; // return the answer to the caller
}
//
// A test main to show it works
#include <iostream>
using namespace std;
int main ()
{
// make two objects to hold the numbers using the user defined data type
// ... The value for num1, num2, num3, num4, and num5 will get "constructed" with // Object1
// and Object2 thanks to our class member function Math
Math Object1 (10, 20, 30, 40, 50); // The object type is Math, the object is
// called Object1
Math Object2 (5, 10, 6, 12, 7); // The object type is Math, the object is
// called Object2
// find the largest number in the first object (Object1) and print it out
// use the cout object to print the information
int solution;
solution = Object1.Largest();
cout << "Largest is " << solution << endl;
// now do the same for the second object (Object2)
solution = Object2.Largest();
cout << "Largest is " << solution << endl;
// now the same for all the lesser values
solution = Object1.Smallest();
cout << "Smallest is " << solution << endl;
solution = Object2.Smallest();
cout << "Smallest is " << solution << endl;
// all done, so return
return 0;
}