//**************************************************************
// Class: Math
//
// Purpose: Creates a given object with five integer values.
// Provides member functions to find the largest and
// smallest value in a given object.
//
// Member Functions:
//
// Math - the constructor
// Largest - Determines the largest value in this object
// Smallest - Determines the smallest value in this object
//
//**************************************************************
class Math
{
private:
int num1; // one of the numbers
int num2; // another one
int num3; // the third one
// TODO - Add two more data members of type int
int num4; // the fourth one
int num5; // the fifth one
public:
// TODO - Extend this from three to five total numbers
Math (int first, int second, int third, int fourth, int fifth); // the class constructor
int Largest (); // member function prototype to return the largest number
// TODO - Add a member function prototype called Smallest that will find
// the smallest of our 5 numbers in our object
int Smallest ();
};
// 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 three 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
// TODO - Add two more numbers here
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 five data values.
//
// TODO - Update this function to find the largest of 5 numbers
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 fourth 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
}
//
// The third member function is Smallest. It examines the data held by the
// class and returns the smallest of the five data values.
//
// TODO - Create a similar member function called "Smallest" to find the smallest
// of 5 numbers
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 smaller one
if (num4 < answer) // now look at the fourth number
answer = num4; // update the answer if we found a smaller one
if (num5 < answer) // now look at the fifth number
answer = num5; // update the answer if we found a smaller one
return answer; // return the answer to the caller
}
// A main function to verify it works
#include <iostream>
using namespace std;
int main ()
{
// make three objects to hold the numbers with the user defined data type
// ... The values for num1, num2, num3, numb4, and num5 will get "constructed"
// with Object1, Object2, and Object3 thanks to our class member function Math
// TODO - Add in two more numbers into our constructor call for a total of 5
Math Object1(10, 20, 30, 40, 50); // The object type is Math, the object is
// called Object1
// TODO - Add in two more numbers into our constructor call for a total of 5
Math Object2(5, 10, 6, 7, 11); // The object type is Math, the object is
// called Object2
// TODO - Create Object3 with 5 integer numbers that you decide
Math Object3(95, 111, 333, 555, 777);
// find the largest number in the first object and print it out
// using the cout object to print the information
int solution; // holds return value from our member function calls
solution = Object1.Largest();
cout << "Largest in Object1 is " << solution << endl;
// TODO - Repeat the previous two statements, but call the Smallest
// member function ... then print the smallest value for Object1
solution = Object1.Smallest();
cout << "Smallest in Object1 is " << solution << endl;
// now do the same for the second object
solution = Object2.Largest();
cout << "Largest in Object2 is " << solution << endl;
// TODO - Repeat the previous two statements, but call the Smallest
// member function ... than print the smallest value for Object2
solution = Object2.Smallest();
cout << "Smallest in Object2 is " << solution << endl;
// now do the same for the third object
// TODO - Repeat the previous statements peformed with Object1 and Object2,
// but find and print the the largest and smallest in our object
// called Object3
solution = Object3.Largest();
cout << "Largest in Object3 is " << solution << endl;
solution = Object3.Smallest();
cout << "Smallest in Object3 is " << solution << endl;
// all done, so return
return 0;
}