Oop C++

Discussion in 'Mac Programming' started by BigPrince, May 6, 2007.

  1. BigPrince macrumors 68020

    Joined:
    Dec 27, 2006
    #1
    I need to convert this program from Function Decomposition to OOP. If you don't have the time, I don't blame you. if you do, I owe you big time. If someone could explain to a dummy exactly what OOP is supposed to do even better.

    I am a total noob. Any help would be appreciated. here is my program in functional decompostion:

    Code:
    
    #include <iostream>                      //Prototyping and global declarations
    using namespace std ;
    
    void display(float[], int) ;           
    int average(float, int, float[]) ;
    void search(float[], int) ;
    void sort(int, int, int, int, float[], int) ;
    int largest(int, float[]) ;
    int smallest(float[]) ;
    void load(float[], int&) ;
    void Get_Choice(int&) ;
    void Process_Choice(int, int, int&, int, int, int, int, int) ;
    
    
    //Purpose: This fucntion is used to inform the user of his or her choices 
    //         and alllow them to slect a choice.
    //Pre: Function is waiting for user input
    //Post: Function processes users input and then waits for further commands
    	void Get_Choice(int& choice)  
    	{
    		cout << endl ;
    		cout << endl ;
    
    		cout << "Welcome to Oliver's Number Array Program " << endl ;
    
    		cout << endl ;
    
    		cout << "Hello, Please choose one of the following choices" << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 1 load the array from keyboard " << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 2 display the array  " << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 3 find the average of the array " << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 4 search the array " << endl ;
    
    		cout << endl ;
    	
    		cout << "Choice 5 sort the array" << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 6 find the largest value " << endl ;
    	
    		cout << endl ;
    
    		cout << "Choice 7 find the smallest value " << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 8 quit " << endl ;
    	
    		cin >> choice ;
    
    		cout << endl ;
    		cout << endl ;
    		cout << endl ;
    	}
    
    //Purpose: This function process the choice inputted from Get_Choice function
    //Pre: Function is waiting for choice selection
    //Post: Function process choice and calls the correct function and then waits 
    //      for further user input. 
    
    	void Process_Choice(int choice, float array[], int& count, float sum, int arrayLength, int passCount, int searchIndex, int minIndex) 
    	{
    		switch(choice)
    		{
    		case 1: load(array, count);
    		
    			break ;
    	
    		case 2: display(array, count) ;
    	
    			break ;
    	
    		case 3: average(sum, count, array) ;
    			cout << endl ;
    
    			cout << "The average of the array is " << average(sum, count, array) << endl ;;
    		
    			break ;
    	
    		case 4: search(array, count) ;
    			
    			break ;
    	
    		case 5: sort(arrayLength, passCount, searchIndex, minIndex, array, count) ;
    		
    			break ;
    	
    		case 6: largest(count, array) ;
    	
    			break ;
    	
    		case 7: smallest(array) ;
    	
    			break ;
    
    		}
    	}
    
    //Purpose: This is the main where we call Get_Choice and Process_Choice
    	void main()
    	{
    		float array[50] ;        //Declaration and instatiation of variables
    		int count = 0 ;
    		float sum = 0 ;
    		int arrayLength = 0 ;
    		int passCount = 0 ;
    		int searchIndex = 0 ;
    		int minIndex = 0 ;
    		int choice ;
    
    		do
    		{
    			Get_Choice(choice) ;
    			Process_Choice(choice, array, count, sum, arrayLength, passCount, searchIndex, minIndex) ;
    		}
    		while(choice != 8) ;
    	}
    
    //Purpose: This function loads the values inputted into the array
    //Pre: Waiting to be called to fill array
    //Post: Array is filled with values
    
    	void load(float array[], int&count) 
    	{
    		cout << endl ;
    		cout << "Enter the value, press enter, and repeat as needed. When done press CTRL-Z" << endl ;
    
    		while(cin)
    		{
    			cin >> array[count] ;
    			count++ ;
    		}
    		cin.clear() ;
    		count-- ;
    	}
    
    //Purpose: This function is called to display the array
    //Pre: Waiting to be called to read and display the array
    //Post: Function reads and displays array
    
    	void display(float array[], int count)
    	{
    		cout << endl ;
    		cout << "The array will now be displayed to you " << endl ;
    
    		cout << "Index" << "                   " << "ArrayElement" << endl ;
    
    		for (int L = 0 ; L < count ; L++)
    			
    			cout << L << "." << "			" << array[L] << endl ;
    	}
    
    //Purpose: This function calculates the average of the array 
    //Pre: Waiting to be called to read values from array and compute average
    //Post: Average of the array is calculated and sent back to main
    
    	int average(float sum, int count, float array[])
    		{
    
    		
    		if (count > 0)
    		{	
    
    		for (int L = 0 ; L < count ; L++)
    
    			sum = sum + array[L] ;
    		}
    		else 
    			cout << "No Average" << endl ;
    
    		 return sum / count ;
    	}
    
    //Purpose: To give the user an opportunity to search the array
    //Pre: Waiting to be called to read and compare values of the array
    //Post: Array is searched and values are compared to see if the target is in array
    
    	void search(float array[], int count)
    	{
    		cout << endl ;
    		cout << "You will now be given an opportunity to search the Array" << endl ;
    
    		float target ;
    		int t = 0 ;
    
    		cout << "Enter a number to search for " ;
    
    		cin >> target ;
    		
    		while(t < count && array[t] != target)
    			t++ ;
    
    		if (t != count)
    
    			cout << target << " is in the array " << endl ;
    
    		else
    
    			cout << target << " is not in the array" << endl ;
    	}
    
    //Purpose: This is a selection sort used to sort the array in ascending order 
    //Pre: Waiting to be called to sort array in asecending order
    //Post: Array is sorted in ascending order
    
    	void sort(int arrayLength, int passCount, int searchIndex, int minIndex, float array[], int count)
    	{
    		cout << endl ;
    		cout << "The Array will now be sorted" << endl ;
    
    		float temp ;
    		arrayLength = count ;
    
    	for (passCount = 0 ; passCount < arrayLength ; passCount++)
    		{
    			minIndex = passCount ;
    
    		for (searchIndex = passCount + 1 ; searchIndex < arrayLength ; searchIndex++)
    		{
    			if (array[searchIndex] < (array[minIndex]))
    				minIndex = searchIndex ;
    		}
    
    		temp = array[minIndex] ;
    		array[minIndex] = array[passCount] ;
    		array[passCount] = temp ;
    		}
    	}
    		
    	
    
    //Purpose: This function finds the largest value in a sorted array
    //Pre: Waiting to be called to display the largest value in array
    //Post: Largest value in array is found and displayed to user
    
    	int largest(int count, float array[])
    
    	{
    		cout << endl ;
    		cout << "The largest value in the array is" << endl ;
    		
    		cout << array[count-1] << endl ;
    
    		return array[count-1] ;
    
    	}
    
    //Purpose: This function inds the smallest value in a sorted array
    //Pre: Waiting to be called to display the smallest value in array
    //Post: Smallest value is found and displayed to user.
    
    	int smallest(float array[])
    	{
    		cout << endl ;
    		cout << "The smallest value in the array is" << endl ;
    
    		cout << array[0] << endl ;
    
    		return array[0] ;
    	
    	}
    
    
    
    

    Here is my pathetic attempt at trying to make it OOP. I am still trying to learn it. Took me long enough to get Function Decomposition.

    Code:
    #include <iostream>                      //Prototyping and global declarations
    using namespace std ;
    enum RelationType {LESS, EQUAL, GREATER} ;
    
    class Numbers
    {
    public:
    	
    	void display(float array[], int count) ;           
    	
    	int average(float sum, int count, float array[]) ;
    	
    	void search(float array[], int count) ;
    	
    	void sort(int arrayLength, int passCount, int searchIndex, int minIndex, float array[], int count) ;
    	
    	int largest(int count, float array[]) ;
    	
    	int smallest(float array[]) ;
    	
    	void load(float array[], int& count) ;
    
    	void Get_Choice(int& choice);
    		
    	void Process_Choice(int choice, float array[], int& count, float sum, int arrayLength, int passCount, int searchIndex, int minIndex) ;
    
    	
    private:
    	float array[50] ;
    	int count ;
    } ;
    
    Code:
    
    #include <iostream>                      //Prototyping and global declarations
    #include "2num.h"
    using namespace std ;
    
    
    
    //Purpose: This fucntion is used to inform the user of his or her choices 
    //         and alllow them to slect a choice.
    //Pre: Function is waiting for user input
    //Post: Function processes users input and then waits for further commands
    void Numbers::Get_Choice(int& choice)  
    	{
    		cout << endl ;
    		cout << endl ;
    
    		cout << "Welcome to Oliver's Number Array Program " << endl ;
    
    		cout << endl ;
    
    		cout << "Hello, Please choose one of the following choices" << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 1 load the array from keyboard " << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 2 display the array  " << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 3 find the average of the array " << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 4 search the array " << endl ;
    
    		cout << endl ;
    	
    		cout << "Choice 5 sort the array" << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 6 find the largest value " << endl ;
    	
    		cout << endl ;
    
    		cout << "Choice 7 find the smallest value " << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 8 quit " << endl ;
    	
    		cin >> choice ;
    
    		cout << endl ;
    		cout << endl ;
    		cout << endl ;
    	}
    
    //Purpose: This function process the choice inputted from Get_Choice function
    //Pre: Function is waiting for choice selection
    //Post: Function process choice and calls the correct function and then waits 
    //      for further user input. 
    
    	void Numbers::Process_Choice(int choice, float array[], int& count, float sum, int arrayLength, int passCount, int searchIndex, int minIndex) 
    	{
    		switch(choice)
    		{
    		case 1: load(array, count);
    		
    			break ;
    	
    		case 2: display(array, count) ;
    	
    			break ;
    	
    		case 3: average(sum, count, array) ;
    			cout << endl ;
    
    			cout << "The average of the array is " << average(sum, count, array) << endl ;;
    		
    			break ;
    	
    		case 4: search(array, count) ;
    			
    			break ;
    	
    		case 5: sort(arrayLength, passCount, searchIndex, minIndex, array, count) ;
    		
    			break ;
    	
    		case 6: largest(count, array) ;
    	
    			break ;
    	
    		case 7: smallest(array) ;
    	
    			break ;
    
    		}
    	}
    
    //Purpose: This is the main where we call Get_Choice and Process_Choice
    	void main()
    	{
    		float array[50] ;        //Declaration and instatiation of variables
    		int count = 0 ;
    		float sum = 0 ;
    		int arrayLength = 0 ;
    		int passCount = 0 ;
    		int searchIndex = 0 ;
    		int minIndex = 0 ;
    		int choice ;
    
    		do
    		{
    			Numbers::Get_Choice(choice) ;
    			Numbers::Process_Choice(choice, array, count, sum, arrayLength, passCount, searchIndex, minIndex) ;
    		}
    		while(choice != 8) ;
    	}
    
    //Purpose: This function loads the values inputted into the array
    //Pre: Waiting to be called to fill array
    //Post: Array is filled with values
    
    	void Numbers::load(float array[], int&count) 
    	{
    		cout << endl ;
    		cout << "Enter the value, press enter, and repeat as needed. When done press CTRL-Z" << endl ;
    
    		while(cin)
    		{
    			cin >> array[count] ;
    			count++ ;
    		}
    		cin.clear() ;
    		count-- ;
    	}
    
    //Purpose: This function is called to display the array
    //Pre: Waiting to be called to read and display the array
    //Post: Function reads and displays array
    
    	void Numbers::display(float array[], int count)
    	{
    		cout << endl ;
    		cout << "The array will now be displayed to you " << endl ;
    
    		cout << "Index" << "                   " << "ArrayElement" << endl ;
    
    		for (int L = 0 ; L < count ; L++)
    			
    			cout << L << "." << "			" << array[L] << endl ;
    	}
    
    //Purpose: This function calculates the average of the array 
    //Pre: Waiting to be called to read values from array and compute average
    //Post: Average of the array is calculated and sent back to main
    
    	int Numbers::average(float sum, int count, float array[])
    		{
    
    		
    		if (count > 0)
    		{	
    
    		for (int L = 0 ; L < count ; L++)
    
    			sum = sum + array[L] ;
    		}
    		else 
    			cout << "No Average" << endl ;
    
    		 return sum / count ;
    	}
    
    //Purpose: To give the user an opportunity to search the array
    //Pre: Waiting to be called to read and compare values of the array
    //Post: Array is searched and values are compared to see if the target is in array
    
    	void Numbers::search(float array[], int count)
    	{
    		cout << endl ;
    		cout << "You will now be given an opportunity to search the Array" << endl ;
    
    		float target ;
    		int t = 0 ;
    
    		cout << "Enter a number to search for " ;
    
    		cin >> target ;
    		
    		while(t < count && array[t] != target)
    			t++ ;
    
    		if (t != count)
    
    			cout << target << " is in the array " << endl ;
    
    		else
    
    			cout << target << " is not in the array" << endl ;
    	}
    
    //Purpose: This is a selection sort used to sort the array in ascending order 
    //Pre: Waiting to be called to sort array in asecending order
    //Post: Array is sorted in ascending order
    
    	void Numbers::sort(int arrayLength, int passCount, int searchIndex, int minIndex, float array[], int count)
    	{
    		cout << endl ;
    		cout << "The Array will now be sorted" << endl ;
    
    		float temp ;
    		arrayLength = count ;
    
    	for (passCount = 0 ; passCount < arrayLength ; passCount++)
    		{
    			minIndex = passCount ;
    
    		for (searchIndex = passCount + 1 ; searchIndex < arrayLength ; searchIndex++)
    		{
    			if (array[searchIndex] < (array[minIndex]))
    				minIndex = searchIndex ;
    		}
    
    		temp = array[minIndex] ;
    		array[minIndex] = array[passCount] ;
    		array[passCount] = temp ;
    		}
    	}
    		
    	
    
    //Purpose: This function finds the largest value in a sorted array
    //Pre: Waiting to be called to display the largest value in array
    //Post: Largest value in array is found and displayed to user
    
    	int Numbers::largest(int count, float array[])
    
    	{
    		cout << endl ;
    		cout << "The largest value in the array is" << endl ;
    		
    		cout << array[count-1] << endl ;
    
    		return array[count-1] ;
    
    	}
    
    //Purpose: This function inds the smallest value in a sorted array
    //Pre: Waiting to be called to display the smallest value in array
    //Post: Smallest value is found and displayed to user.
    
    	int Numbers::smallest(float array[])
    	{
    		cout << endl ;
    		cout << "The smallest value in the array is" << endl ;
    
    		cout << array[0] << endl ;
    
    		return array[0] ;
    	
    	}
    
    
    
    
    :(
     
  2. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #2
    Well, you are on the right track, but have a little ways to go. Here are a few tips:

    1. You do not pass array and count into the member functions Numbers::display, Numbers::average, ..., Numbers::load. Why? A Numbers object will already have its own member variables for this. Note that you won't have to change your implementation of those member functions once you remove the array and count parameters because they have the same names. Member functions automatically have access to member variables.

    2. In Numbers::average and Numbers::sort and Numbers::process_Choice you are passing in values as parameters that really shouldn't be. For example, in your declaration of Numbers::average you pass in sum as a parameter. But in the function implementation you use sum as a local variable. So, you should declare sum as a local variable and not a parameter. I know you can get it to work the way you have it, but it is not good. For one thing it isn't object oriented because you are exposing a portion of the implementation details outside the function.

    So, combining 1. and 2. above, the average function would be declared as:
    Code:
    class Numbers
    {
    public:
    ...
    	float average() ;
    ...
    };
    
    and it would be implemented as:
    Code:
    	float Numbers::average()
    	{
    		float sum = 0.0;
    
    		if (count > 0)
    		{	
    			for (int L = 0 ; L < count ; L++)
    				sum = sum + array[L] ;
    		}
    		else 
    			cout << "No Average" << endl ;
    
    		 return sum / count ;
    	}
    
    Notice how I declare (and initialize) sum as a local variable.
    Also notice how I use count and array within the function. The function is accessing the average and count member variables you declared for the Numbers class in the private: section.

    Note that I changed the return type of average from int to float, which seems more appropiate.

    3. You should create a constructor for your Numbers class. It just needs to set count to zero.

    4. In your main function you should declare a Numbers instance and use it. Currently you are attempting to call the instance member functions as class member functions. Your main function might look something like this:
    Code:
    	void main()
    	{
    		Numbers numsObject;
    		int choice ;
    
    		do
    		{
    			numsObject.Get_Choice(choice) ;
    			numsObject.Process_Choice(choice) ;
    		}
    		while(choice != 8) ;
    	}
    
    5. I think it's arguable that Get_Choice() and Process_Choice() should be member functions of Numbers, but I'd iron out all the other stuff before worrying about that, if at all.

    That should be enough to get you started... :)
     
  3. BigPrince thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #3
    I really appreciate your time, I printed this out and will look over this closely.

    Thanks.
     
  4. BigPrince thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #4
    could you elaborate a little on this line of code:

    Code:
    Numbers numsObject;
    
    Looks important but I don't rememeber what that does or means.
     
  5. Mr.Texor macrumors regular

    Joined:
    Apr 20, 2007
    #5
    it initializes numsObject of the type Numbers, which is the class you just defined
     
  6. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #6
    Yes. It is really the same thing as declaring a variable of any type. For example, in your main function, you declare choice as a variable of type int:
    Code:
    int choice ;
    This is the same thing, but you are declaring numsObject as a variable of type Numbers. Generally you can declare a variable like this:

    SomeType varibleName;

    substituting, obviously, the type and variable name you want to use.
     
  7. BigPrince thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #7
    ahh. I have also just learned that I did not create my class correctly. I need a .h and a .cpp for my class and then a driver program (another cpp). I have taken out Get_Choice, Process_Choice, and main and put into a "test driver."


    thanks for the help.
     
  8. BigPrince thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #8
    i am in my driver statement, how do I call the functions from my class?

    I tried to do it with Numbers numObject and using numObject.function however it says

    "already defined in source1.cpp"


    edit:
    I am down to one error:

    Numbers numObject ; redefition. I have it defined in the .cpp part of my class and in the .cpp test driver, however its the same declaration
     
  9. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #9
    That sounds right, so can you post the whole file? (The one with main in it).
    Somehow you are implementing the same function twice. Maybe some old code it floating around, or maybe you did something like including one .cpp inside another (you only include .h files in other files, not .cpp files).

    edit: in response to your edit, you should only have the "Numbers numObject;" in your driver code, not in the class .cpp
     
  10. BigPrince thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #10
    OMFG I am crying right now I am so happy.

    It was because I included the .cpp instead of the .h. I will post the code momenetarly. Let me collect my thoughts.
     
  11. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #11
    lol, we are cross-posting and even cross-editing!
     
  12. BigPrince thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #12
    Hey. Thank you for all your input/support. I also had some in real world help as well. But with you all combined I got it. I spent quite a bit of time on this. Here it is. I know its not the most mac friendly program, but unforantly I am coding at a school that uses MS.

    here is the code, I still have to comment it out:

    .h (I believe this is called the specification file.)

    Code:
    #include <iostream>                      
    using namespace std ;
    
    
    
    class Numbers
    {
    public:
    	
    	void display() ;           
    	
    	float average() ;
    	
    	void search() ;
    	
    	void sort() ;
    	
    	int largest() ;
    	
    	int smallest() ;
    	
    	void load() ;
    	
    private:
    	float array[50] ;
    	int count ;
    
    
    } ;
    Here is the .cpp, the other part of the class I created, which I believe is called the implementation file and completes the creation of my object.

    Code:
    //This program takes values from the keyboard and puts them in an Array.
    //While the data is the arrary it is able to perform the following 
    //operations to it: display, average, search, sort via selection sort,
    //and look for the largest and smallest value.
    
    #include <iostream>                      
    #include "2num.h"
    using namespace std ;
    
    
    
    //Purpose: This function loads the values inputted into the array
    //Pre: Waiting to be called to fill array
    //Post: Array is filled with values
    
    	void  Numbers::load() 
    	{
    		cout << endl ;
    		cout << "Enter the value, press enter, and repeat as needed. When done press CTRL-Z" << endl ;
    
    		while(cin)
    		{
    			cin >> array[count] ;
    			count++ ;
    		}
    		cin.clear() ;
    		count-- ;
    	}
    
    //Purpose: This function is called to display the array
    //Pre: Waiting to be called to read and display the array
    //Post: Function reads and displays array
    
    	void  Numbers::display()
    	{
    		cout << endl ;
    		cout << "The array will now be displayed to you " << endl ;
    
    		cout << "Index" << "                   " << "ArrayElement" << endl ;
    
    		for (int L = 0 ; L < count ; L++)
    			
    			cout << L << "." << "			" << array[L] << endl ;
    	}
    
    //Purpose: This function calculates the average of the array 
    //Pre: Waiting to be called to read values from array and compute average
    //Post: Average of the array is calculated and sent back to main
    
    	float  Numbers::average()
    		{
    		float sum = 0.0 ;
    		
    		if (count > 0)
    		{	
    
    		for (int L = 0 ; L < count ; L++)
    
    			sum = sum + array[L] ;
    		}
    		else 
    			cout << "No Average" << endl ;
    
    		 return sum / count ;
    	}
    
    //Purpose: To give the user an opportunity to search the array
    //Pre: Waiting to be called to read and compare values of the array
    //Post: Array is searched and values are compared to see if the target is in array
    
    	void  Numbers::search()
    	{
    		cout << endl ;
    		cout << "You will now be given an opportunity to search the Array" << endl ;
    
    		float target ;
    		int t = 0 ;
    
    		cout << "Enter a number to search for " ;
    
    		cin >> target ;
    		
    		while(t < count && array[t] != target)
    			t++ ;
    
    		if (t != count)
    
    			cout << target << " is in the array " << endl ;
    
    		else
    
    			cout << target << " is not in the array" << endl ;
    	}
    
    //Purpose: This is a selection sort used to sort the array in ascending order 
    //Pre: Waiting to be called to sort array in asecending order
    //Post: Array is sorted in ascending order
    
    	void  Numbers::sort()
    	{
    
    		cout << endl ;
    		cout << "The Array will now be sorted" << endl ;
    
    		float temp ;
    		int arrayLength = 0 ;
    		int passCount = 0 ;
    		int searchIndex = 0 ;
    		int minIndex = 0 ;
    		arrayLength = count ;
    
    	for (passCount = 0 ; passCount < arrayLength ; passCount++)
    
    		{
    			minIndex = passCount ;
    
    		for (searchIndex = passCount + 1 ; searchIndex < arrayLength ; searchIndex++)
    		{
    			if (array[searchIndex] < (array[minIndex]))
    				minIndex = searchIndex ;
    		}
    
    		temp = array[minIndex] ;
    		array[minIndex] = array[passCount] ;
    		array[passCount] = temp ;
    		}
    	}
    		
    	
    
    //Purpose: This function finds the largest value in a sorted array
    //Pre: Waiting to be called to display the largest value in array
    //Post: Largest value in array is found and displayed to user
    
    	int  Numbers::largest()
    
    	{
    		cout << endl ;
    		cout << "The largest value in the array is" << endl ;
    		
    		cout << array[count-1] << endl ;
    
    		return array[count-1] ;
    
    	}
    
    //Purpose: This function inds the smallest value in a sorted array
    //Pre: Waiting to be called to display the smallest value in array
    //Post: Smallest value is found and displayed to user.
    
    	int  Numbers::smallest()
    	{
    		cout << endl ;
    		cout << "The smallest value in the array is" << endl ;
    
    		cout << array[0] << endl ;
    
    		return array[0] ;
    	
    	}
    
    
    


    Here is now what is referred to what I believe is a "test driver."

    Code:
    #include <iostream>                      //Prototyping and global declarations
    #include "2num.h"
    using namespace std ;
    
    Numbers numObject ;
    
    //Purpose: This fucntion is used to inform the user of his or her choices 
    //         and alllow them to slect a choice.
    //Pre: Function is waiting for user input
    //Post: Function processes users input and then waits for further commands
    
    void Get_Choice(int& choice)  
    	{
    		cout << endl ;
    		cout << endl ;
    
    		cout << "Welcome to Oliver's Number Array Program " << endl ;
    
    		cout << endl ;
    
    		cout << "Hello, Please choose one of the following choices" << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 1 load the array from keyboard " << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 2 display the array  " << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 3 find the average of the array " << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 4 search the array " << endl ;
    
    		cout << endl ;
    	
    		cout << "Choice 5 sort the array" << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 6 find the largest value " << endl ;
    	
    		cout << endl ;
    
    		cout << "Choice 7 find the smallest value " << endl ;
    
    		cout << endl ;
    
    		cout << "Choice 9 quit " << endl ;
    	
    		cin >> choice ;
    
    		cout << endl ;
    		cout << endl ;
    		cout << endl ;
    	}
    
    //Purpose: This function process the choice inputted from Get_Choice function
    //Pre: Function is waiting for choice selection
    //Post: Function process choice and calls the correct function and then waits 
    //      for further user input. 
    
    	void Process_Choice(int choice) 
    	{
    		
    		switch(choice)
    		{
    		case 1: numObject.load() ;
    		
    			break ;
    	
    		case 2: numObject.display() ;
    	
    			break ;
    	
    		case 3: numObject.average() ;
    			
    			cout << endl ;
    
    			cout << "The average of the array is " << numObject.average() << endl ;;
    		
    			break ;
    	
    		case 4: numObject.search() ;
    			
    			break ;
    	
    		case 5: numObject.sort() ;
    		
    			break ;
    	
    		case 6: numObject.largest() ;
    	
    			break ;
    	
    		case 7: numObject.smallest() ;
    	
    			break ;
    
    		}
    	}
    
    //Purpose: This is the main where we call Get_Choice and Process_Choice
    	void main()
    	{
    		int choice = 0 ;
    		
    		do
    		{
    			Get_Choice(choice) ;
    			Process_Choice(choice) ;
    		}
    		while(choice != 8) ;
    	}
    I still have to understand some of the theory behind it and why it is better then functional decomposition, but at least I can get it. Now my next challenege will be getting input from a file. And then I have another late assigment to work on which is even harder, but things are slowly starting to fall into place for me. Thank you all for your help and one day when I am a good programming I remember how people helped me and will contribute to others as you have helped me.
     
  13. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #13
    You're welcome, it is fun to help.

    I think it will start to make sense as you do more of it. And, it is a little hard to see why OOP is helpful from this project because it is fairly small. As projects get larger and more complex OOP helps keep things organized and manageable. Even here you can see some benefit. Notice how much simpler your calls to the Numbers functions are than the earlier version. In a larger project you might be calling these functions a lot so that alone makes it easier. Imagine a program where you had different sets of numbers to deal with and the benefits are even more.

    Anyway, here are just a few more details to consider:

    I don't see where count is being initialized but it should be. At the very least you should set it 0 at the start of Numbers::load. If your program is functioning properly it's only because you are getting lucky in that count just happens to have the value zero. That isn't guaranteed in C++ though so you should initialize it.

    The proper C++ way to initialize member variables is in a constructor. I assume you are doing this for a course you are taking, and if you haven't covered constructors yet, then it might be weird to turn in an assignment using them. If you don't want to use a constructor, just make an "initialize" function that sets count to zero. Call it on numsObject in main before you use it. If you have covered constructors, then you should definitely create one. The nice thing about that it that it will be called automatically on numsObject for you when you declare it.
     
  14. BigPrince thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #14
    What was frusterating was all the effort I put into understanding Functional Decomposition and then I find out its not really used as much as OOP and that OOP is better.


    edit: I tried intialzing count in the load function and it stopped working. For now I will err on the side of luck.
     
  15. Mr.Texor macrumors regular

    Joined:
    Apr 20, 2007
    #15
    Learning about several different programming paradigms is very important. Although OOP is very popular right now, there are times when you want to use another "way" of doing things. Also by learning one paradigm's strength and weakness you can understand why another paradigm is better at certain situations. also.. OOP is not always better. :cool:
     
  16. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #16
    I don't think your work on functional decomposition is a waste, though, because you need to break things down in a similar way to design good objects.

    In regard to initializing count, I probably misunderstood something. One thought:
    By "initialize" I mean just assigning a value not redeclaring count. That is, the first line of Numbers::load() would be:
    Code:
        count = 0;
    not
    Code:
        int count = 0;
    The second, incorrect, statement declares a local variable called count. Changes to count in the rest of Numbers::load() would then affect the local variable count, not the member variable count.
     
  17. GeeYouEye macrumors 68000

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #17
    Actually, if you remove the content coupling, functional decomposition essentially becomes functional programming, which is in some ways superior to object-oriented. But you're several classes too early for that. :)
     

Share This Page