Project #8 Template Classes/Files Solution




You are to implement a List template class.  This class stores a list of numbers, providing information about the list items thru calls to class member functions.  You will probably want to place the entire class in a file List.h, as most compilers cannot handle the .h/.cpp file combination for a template class.


The List Class is described in the following UML class diagram


List<class T>       // T is a type which overloads the ‘==’  and ‘<<’ operators
– data:  T*             // dynamic array which stores list data

– howmany: int       // number of values in the list

– size:  int              // capacity lf list

+ List( )                 //    creates list with capacity of 10

+ List(int max)       //    creates list with capacity of max

+ bool  isFull( ) const               // returns true if thls list is full

+ bool  isEmpty( ) const                // returns true if thls list is full


+ void insert(const T& value)        // if list is not full, inserts adds this item to the list

                                       // otherwise a ListException is thrown


+ T last( ) const                //returns the last item in the list

                                      // if the list is empty an exception is thrown


+ int occurs(const T& value) const         // returns the number of times ‘value’ occurs in the list

+ void deleteAll( const T& value)          // deletes ALL occurances of value

+ bool operator==(const List<T>& operand) const  /// returns true if this object is equal to operand


friend ostream& operator<< <T>(ostream& out, const List<T>& robject)

friend istream& operator>> <T> (istream& in, List<T> & object)



You are to write an application (main program) which:

  • Asks the user how many values he/she would like in the list
  • Creates a List object with that capacity * 2
  • Prompts the user for a filename, and reads values from that file storing them in the list

If file does not exist, list remains empty

  • Prints out the list to the screen, if it is not empty

Otherwise message ‘List is empty’ is printed

  • Allows user to enter more int values if he/she wishes
  • Allows user to view the last value in the list
  • Allow the user to enter a value, and check how many times it occurs in the list
  • Allow the user to enter a value, and remove all occurrances of this from the list
  • Creates a 2nd list, and output it
  • Determines if two lists are equal
  • Outputs the values from first list to a file (this file should be usable as input for this program)
  • Catches and handles any ListException that is thrown by informing the user of

The problem


Test this program with different list values, to ensure correct operation.  Recompile your main program to work with a different type  —  and retest.  Your program should work with any type (primitive or object type) which overloads == and <<  .


** You may want to first get the application running using ‘cin’ and ‘cout’ and then adjust it to work with files.


Submit files List.h  and your application.cpp file.  Be sure that your name is included in each file and that all code is readable.  Be sure to well-test your class, as I will test it with applications other than your own.


error: Content is protected !!