#include <iostream>
using namespace std;
class Math
{
private:
int num1; // one of the private data numbers
int num2; // another one
int num3; // the third one
int num4;
int num5;
int num6;
public:
Math (int first, int second, int third, int fourth, int fifth, int sixth); // 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 three values must be
// provided
Math::Math (int first, int second, int third, int fourth, int fifth, int sixth)
{
num1 = first; // save the first int
num2 = second; // save the second int
num3 = third; // save the third int
num4 = fourth;
num5 = fifth;
num6 = sixth;
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 numbers [] = {num1, num2, num3, num4, num5, num6};
int answer = num1; // answer will be the largest we find
for (int i = 0; i < 6; ++i)
{
if (numbers[i] > answer) {
answer = numbers[i];
}
}
return answer;
}
int Math::Smallest ()
{
int numbers [] = {num1, num2, num3, num4, num5, num6};
int answer = num1; // answer will be the largest we find
for (int i = 0; i < 6; ++i)
{
if (numbers[i] < answer){
answer = numbers[i];
}
}
return answer;
}
// 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, and num3 will get "constructed" with Object1
// and Object2 thanks to our class member function Math
Math Object1 (10, 20, 30, 40, 50, 60); // The object type is Math, the object is
// called Object1
Math Object2 (5, 10, 6, 8, 15, 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 solutionLargest;
int solutionSmallest;
solutionLargest = Object1.Largest();
solutionSmallest = Object1.Smallest();
cout << "Largest is " << solutionLargest << endl;
cout << "Smallest is " << solutionSmallest << endl;
// now do the same for the second object (Object2)
solutionLargest = Object2.Largest();
solutionSmallest = Object2.Smallest();
cout << "Largest is " << solutionLargest << endl;
cout << "Smallest is " << solutionSmallest << endl;
// all done, so return
return 0;
}