Class template in c++

Discussion in 'Software' started by someguy85, Jan 26, 2010.

  1. someguy85

    someguy85 Private E-2

    Hello everyone. I'm now in my third week of a data structures class and am working on a program dealing with class templates. I wrote main just as a driver to test the class, but there is something wrong with my code. I'm assuming it has to do with the first line of code in main, but I'm not sure and I have no clue how to fix it. I'll post the code from the header and driver files below. Thanks again for any help.

    Header file named aList.h is below.

    Code:
    #include <iostream>
    using namespace std;
    
    #ifndef ALIST_H
    #define ALIST_H
    
    template <typename TYPE>
    Class ArrayList
    {
    private:
    	TYPE* aList;
    	int capacity; // to hold the capacity of the array as it changes
    	int numValues; // to count how many values have been entered into array
    
    public:
    	ArrayList ( int c = 5 ); // constructor prototype
    	~ArrayList();
    	void display() const;
    	bool insert ( const TYPE& dataIn );
    	bool retrieve ( TYPE& dataOut, int index ) const;
    	bool remove ( TYPE& dataOut, int index);
    	TYPE getLargest() const;
    	TYPE getSmallest() const;
    	int getSIZE() const;
    	int getNumValues() const; // system ownes 'numValues' variable so it will remember its value
    
    };
    
    //**********************************************************************************
    
    template <typename TYPE>
    ArrayList <TYPE> :: ArrayList ( int c )
    {
    	capacity = c;
    	aList = new TYPE [ capacity ];
    	numValues = 0;
    }
    
    //**********************************************************************************
    
    template <typename TYPE>
    ArrayList <TYPE> :: ~ArrayList()
    {
    	delete [] aList; // only one that's required
    	capacity = 0;
    	numValues = 0;
    	aList = NULL;
    }
    
    //**********************************************************************************
    
    void ArrayList :: display() // is this heading proper?
    {
    	cout << "Current values in list:\n";
    	for (int count = 0; count < numValues; count++)
    	{
    		cout << *(aList + count) << "\t"; // is this statement correct?
    	}
    }
    
    //**********************************************************************************
    
    bool ArrayList <TYPE> :: insert ( const TYPE& dataIn)
    {
    	bool success = false;
    
    	if ( numValues == capacity ) // if list needs to be expanded
    	{
    		int newSize = capacity + (capacity * .5);
    		TYPE* tempArray = new TYPE[newSize]; 
    
    		for ( int count = 0; count < numValues; count++ )
    			tempArray[count] = aList[count];
    
    		delete [] aList;
    		aList = tempArray;
    		capacity = newSize;
    	}
    
    	aList[numValues] = dataIn; // places most recent value in the rear of the list
    	numValues++;
    
    		success = true;
    
    		return success;
    }
    
    //**********************************************************************************
    
    bool ArrayList <TYPE> :: retrieve ( TYPE& dataOut, int index )
    {
    	bool success = false;
    	
    	dataOut = aList[index]; // dataOut will hold the value to be retrieved/copied
    
    	bool success = true;
    
    	return success;
    }
    
    //**********************************************************************************
    
    bool ArrayList <TYPE> :: remove ( TYPE& dataOut, int index)
    {
    	bool success = false;
    
    	if ( index >= 0 && index < numValues) // if the index is valid
    	{
    		dataOut = aList[index];
    	}
    
    	for ( int count = index; count < numValues; count++)
    	{
    		aList[count] = *(aList + (count + 1) ); // is this notation correct?
    	}
    
    	bool success = true;
    	
    	return success;
    }
    
    //**********************************************************************************
    
    TYPE ArrayList <TYPE> :: getLargest()
    {
    	TYPE highest = aList[0];
    
    	for ( int count = 1; count < numValues; count++ )
    	{
    		if ( *(aList + count) > highest )
    			highest = *(aList + count);
    	}
    
    	return highest;
    }
    
    //**********************************************************************************
    
    TYPE ArrayList <TYPE> :: getSmallest()
    {
    	TYPE lowest = aList[0];
    
    	for ( int count = 1; count < numValues; count++)
    	{
    		if ( *(aList + count) < lowest )
    			lowest = *(aList + count);
    	}
    
    	return lowest;
    }
    
    //**********************************************************************************
    
    int ArrayList :: getSize()
    {
    	return capacity;
    }
    
    //**********************************************************************************
    
    int ArrayList :: getNumValues()
    {
    	return numValues
    }
    
    #endif
    
    Driver file named aListDriver.cpp is below:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    #include "aList.h"
    
    
    int main() 
    {
    	ArrayList <double> myList;
    	myList.insert ( 1.7 );
    	myList.insert ( 2.3 );
    	if ( myList.remove ( value, 0 )
    		cout << "Good Job. " << "Value is " << value << endl;
    	else 
    		cout << "Invalid subscript. " << endl;
    
    	return 0;
    }
    
     
  2. someguy85

    someguy85 Private E-2

    I finally have my program working. I'll post my most recent code for anybody else that may have similar problems. Fixing the final removal problem was incredibly simple and made me feel like a fool. All it took was simply decrementing numValues.

    aList.h

    Code:
    #include <iostream>
    using namespace std;
    
    #ifndef ALIST_H
    #define ALIST_H
    
    template <typename TYPE>
    class ArrayList
    {
    private:
    	TYPE* aList;
    	int capacity; // to hold the capacity of the array as it changes
    	int numValues; // to count how many values have been entered into array
    
    public:
    	ArrayList ( int c = 5 ); // constructor prototype
    	~ArrayList();
    	void display() const;
    	bool insert ( const TYPE& dataIn );
    	bool retrieve ( TYPE& dataOut, int index ) const;
    	bool remove ( TYPE& dataOut, int index);
    	TYPE getLargest() const;
    	TYPE getSmallest() const;
    	int getSize() const;
    	int getNumValues() const; // system ownes 'numValues' variable so it will remember its value
    
    };
    
    //**********************************************************************************
    
    template <typename TYPE>
    ArrayList <TYPE> :: ArrayList ( int c )
    {
    	capacity = c;
    	aList = new TYPE [ capacity ];
    	numValues = 0;
    }
    
    //**********************************************************************************
    
    template <typename TYPE>
    ArrayList <TYPE> :: ~ArrayList()
    {
    	delete [] aList; // only one that's required to prevent memory leak
    	capacity = 0;
    	numValues = 0;
    	aList = NULL;
    }
    
    //**********************************************************************************
    
    template <typename TYPE>
    void ArrayList <TYPE> :: display() const // is this heading proper?
    {
    	cout << "Current values in list:\n";
    	for (int count = 0; count < numValues; count++)
    	{
    		cout << aList[count] << "\t"; // is this statement correct?
    	}
    }
    
    //**********************************************************************************
    
    template <typename TYPE>
    bool ArrayList <TYPE> :: insert ( const TYPE & dataIn)
    {
    	bool success = false;
    
    	if ( numValues == capacity ) // if list needs to be expanded
    	{
    		int newSize = capacity + (capacity / 2);
    		TYPE* tempArray = new TYPE[newSize]; 
    
    		for ( int count = 0; count < numValues; count++ )
    			tempArray[count] = aList[count];
    
    		delete [] aList;
    		aList = tempArray;
    		capacity = newSize;
    	}
    
    	aList[numValues] = dataIn; // places most recent value in the rear of the list
    	numValues++;
    
    		success = true;
    
    		return success;
    }
    
    //**********************************************************************************
    template <typename TYPE>
    bool ArrayList <TYPE> :: retrieve ( TYPE& dataOut, int index ) const
    {
    	bool success = false;
    	
    	dataOut = aList[index]; // dataOut will hold the value to be retrieved/copied
    
    	success = true;
    
    	return success;
    }
    
    //**********************************************************************************
    
    template <typename TYPE>
    bool ArrayList <TYPE> :: remove ( TYPE& dataOut, int index)
    {
    	bool success = false;
    
    	if ( index >= 0 && index < numValues) // if the index is valid
    	{
    		dataOut = aList[index];
    
    		for ( int count = index; count < numValues; count++)
    		{
    			aList[count] = *(aList + (count + 1) );
    		}
    
    		numValues--;
    	}
    
    
    	success = true;
    	
    	return success;
    }
    
    //**********************************************************************************
    template <typename TYPE>
    TYPE ArrayList <TYPE> :: getLargest() const
    {
    	TYPE highest = aList[0];
    
    	for ( int count = 1; count < numValues; count++ )
    	{
    		if ( *(aList + count) > highest )
    			highest = *(aList + count);
    	}
    
    	return highest;
    }
    
    //**********************************************************************************
    template <typename TYPE>
    TYPE ArrayList <TYPE> :: getSmallest() const
    {
    	TYPE lowest = aList[0];
    
    	for ( int count = 1; count < numValues; count++)
    	{
    		if ( *(aList + count) < lowest )
    			lowest = *(aList + count);
    	}
    
    	return lowest;
    }
    
    //**********************************************************************************
    
    template <typename TYPE>
    int ArrayList <TYPE> :: getSize() const
    {
    	return capacity;
    }
    
    //**********************************************************************************
    template <typename TYPE>
    int ArrayList <TYPE> :: getNumValues() const
    {
    	return numValues;
    }
    
    
    #endif
    

    aListDriver.cpp

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    #include "aList.h"
    
    
    int main() 
    {
    	ArrayList <double> myList; // declaring myList to be of type double and TYPE to be double?
    
    	double value;
    
    	myList.insert ( 1.7 );
    	myList.insert ( 2.3 );
    	myList.insert ( -4.8 );
    	myList.insert ( 147.2 );
    	myList.insert ( 27.6 );
    	myList.insert ( 38.4 );
    
    	
    	cout << "Enter a value to be removed. ";
    	cin >> value;
    	int index;
    	cout << "What is the index of this value? ";
    	cin >> index;
    	
    	// Test remove function
    	if ( myList.remove ( value, index ) )
    		cout << "Good Job. " << "Value removed is " << value << ", at index " << index << endl;
    	else 
    		cout << "Invalid subscript. " << endl;
    	
    	// Test display function
    	myList.display();
    	
    	// Test retrieve function
    	if ( myList.retrieve ( value, index ) ) // if retrieve funcion returns true
    		cout << "\nRetrieval was successful.\n";
    	else 
    		cout << "Retrieval failed.\n";
    
    	// Test getLargest function
    	double biggest = myList.getLargest();
    	cout << "\nLargest value in list is:\t" << biggest << endl;
    	
    	// Test getSmallest function
    	double smallest = myList.getSmallest();
    	cout << "\nSmallest value in list is:\t" << smallest << endl;
    
    	// Test getSize function
    	int size = myList.getSize();
    	cout << "\nThe capacity of the list is currently set to " << size << ".\n";
    
    
    	//Test getNumValues function
    	int elems = myList.getNumValues();
    	cout <<"\nThere are currently " << elems << " value(s) in the list.\n";
    
    	return 0;
    }
    
     
  3. XTAL256

    XTAL256 Private First Class

    Also i noticed in the first post the keyword "class" was spelt with a capital C. I would suggest using a text editor with syntax highlighting if you are not using one already. It will help you pick up little syntax errors like that because the word "Class" would not have been highlighted.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds