Thread: How do classes execute code?

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    18

    How do classes execute code?

    Code:
    #include <iostream>
    using namespace std;
    class numericArray
    {
    	private:
    	
    
    		double *array; 
    		double average;
    		int pick, high, low;
    	
    	public:
    			
    		   
    		numericArray(int, int );
    		
    	
    		~numericArray()
    		{delete [] array;
    		cout << "bye";}
    	
    
    		void setValue(double [], int);
    		void setAverage(double [], int);
    		void setHiLow(double [], int);
    		
    		
    		double getValue()
    		{cout << "hi\n";
    		return array[pick];}
    		
    		double getHigh()
    		{ cout << "high index = " << high << endl;
    		return array[1];}
    		
    		double getLow()
    		{cout << "\n\nlow blow\n";
    		return array[low];}
    		
    		double getAverage()
    		{cout << "This is the average " << average << endl;
    		return average;}
    };
    numericArray::numericArray(int ray, int v)
    		{
    			array = new double[ray];
    			pick = v;
    			for(int v = 0; v < ray; v++)
    				array[v] = 0;
    		}
    void numericArray::setValue(double array[], int u)
    {
    	double p;
    	cout << "Please enter the value you wish to store in this subscript: ";
    	cin  >> p;
    	array[u] = p;
    	
    }
    
    void numericArray::setAverage(double array[], int u)
    {
    	int _Average = 0;
    	for(int c = 0; c < u; c++)
    		{
          _Average += array[c];
    		}
    		average = _Average;
    }
    
    void numericArray::setHiLow(double array[], int u)
    {
    	int greatest = 0, lowest = 0;
    	for(int x = 0; x < (u - 1); x++)
    		{
    		if(array[x + 1] > array[x])
    		greatest = x + 1;
    		else greatest = x;
    		
    		if(array[x+1] < array[x])
    		lowest = x + 1;
    		else lowest = x;
    		} 
    	cout << array[u];
    	high = greatest;
    	low = lowest;
    }
    
    int main()
    {
    	int amount,num;
    	
    	cout <<	"You are going to need to enter the amount of numbers\n\n"
    		  		"you'd like to enter. Then I'll show you the highest,\n\n"
    		  		"lowest, and the average of the numbers you entered. \n\n"
    		  		"Also you can pick which subscript(which place its held at\n\n"
    		  		"in memory)\n";
    	
    	system("pause");
    	system("cls");
    		  
    	cout << "Please enter the amount of numbers you'd like to store:  ";
    
    	cin  >> amount;
    
    	while(amount < 0)
    		{
    		cout << "You cannot enter a negative value.\n"
    		     << "Enter a positive number:  ";
    		cin  >> amount;
    		cout << "\n";
    		}
    	
    	system("pause");
    	system("cls");
    	
    	cout << "Ok, now enter which subscript(what number inbetween \n\n"
    		  << amount - 1 << " and 0 : ";
       cin  >> num;
    		  
    	while (num < 0 || num > amount)
    		{
    		cout << "Invalid input!\a\a"  << "Enter number between 0 and " 
    		     << amount - 1 <<  ":  \n";
    		cin  >> num;
    		cout << "\n";
    		}
    		
    	numericArray crap(amount, num);	
    	cout << "The object was created..." << endl;
    	cout << crap.getHigh();
    	return 0;
    	}
    The trouble im having is void getHiLow() function is the only one executing, but setValue(), and setAverage() aren't executing, and I need them to before getHiLow(). How does the class start running after the constructor is finished doing its job? Can anybody please explain to me why the heck the other ones are doing their jobs?

    p.s incase your wondering, i put the cout statements in some of the functions to see if they ran or not.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am not seeing why they should execute...

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Post the smallest code snippet that exhibits the problem.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Functions within classes (aka member functions or methods) are called by referencing an object of that class. In your case
    Code:
    crap.getHigh();
    will call the "getHigh" method of the numericArray class. None of the other methods will be called (aside from the constructor).

    As a side note (free advice as part of the service here):
    Code:
    numericArray::numericArray(int ray, int v)
    		{
    			array = new double[ray];
    			pick = v;
    			for(int v = 0; v < ray; v++)
    				array[v] = 0;
    		}
    You are overlaying the parameter v with a int v inside the for-loop. It's advisable to use different names to avoid confusion.

    Code:
    void numericArray::setHiLow(double array[], int u)
    {
    	int greatest = 0, lowest = 0;
    	for(int x = 0; x < (u - 1); x++)
    		{
    		if(array[x + 1] > array[x])
    		greatest = x + 1;
    		else greatest = x;
    		
    		if(array[x+1] < array[x])
    		lowest = x + 1;
    		else lowest = x;
    		} 
    	cout << array[u];
    	high = greatest;
    	low = lowest;
    }
    This code is completely broken - you need to remember the highest & lowest values (or the place) and compare your highest/lowest value with the current location in the array - if it's higher/lower, then you've got a "new highest", or "new lowest", so remember this one instead. The code as provided changes greatest/lowest every time you look at a value, whether you actually have a "new highest(lowest)" or not.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    I see what i was doing wrong. I wasnt performing crap.setValue and the others. Was thinking those would automatically execute. Working on fixing the loop now. Thanks for the advice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-20-2009, 07:35 AM
  2. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 01:46 AM
  3. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  4. Need help on code to execute the program
    By hibmem10 in forum C++ Programming
    Replies: 6
    Last Post: 12-24-2005, 01:42 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM