temperaturelist class with dynamic array
Description
In your textbook’s display 11-10, TemperatureList class was discussed. You will implement that same class using dynamic array in this lab. The original TemperatureList class implementation is given to you for reference.
This is the interface of the original class:
class TemperatureList
{
public:
TemperatureList( );
//Initializes the object to an empty list.
void add_temperature(double temperature);
//Precondition: The list is not full.
//Postcondition: The temperature has been added to the list.
bool full( ) const;
//Returns true if the list is full; false otherwise.
friend ostream& operator <<(ostream& outs, const TemperatureList& the_object);
//Overloads the << operator so it can be used to output values of
//type TemperatureList. Temperatures are output one per line.
//Precondition: If outs is a file output stream, then outs
//has already been connected to a file.
private:
double list[MAX_LIST_SIZE]; //of temperatures in Fahrenheit
int size; //number of array positions filled
};
Requirements
Your job is to:
- Replace the array list[MAX_LIST_SIZE] with a dynamic array
- Add a max_size member variable that sets the max number of elements allowed in the array
- Add a constructor that accepts an integer parameter that specifies max_size
- Add the big three
- Change the implementation of the class to make sure no memory leak happens and the class functions as expected
- Separate the class into templist.h and templist.cpp files
- Follow the ADT (Abstract Data Type) guideline to implement the required class
- Comment well in your program and any functions you use
Tips
- In summary, you will have 8 member functions in your class:
- Default constructor
- Constructor that accepts an integer parameter to set the max_size
- Copy constructor
- Destructor
- Overload = operator
- add_temperature
- full (no change in its implementation)
- Overloading operator << (nothing shall need to change in its implementation)
- You will have 3 member variables in your class
- size – current size of your array
- max_size – max size the array will hold
- list – dynamic array/pointer to
- Starting from scratch might be easier for you than editing the original TemperatureList class.
- Watch video 12-2 for a discussion of TemperatureList implemented with a normal array.
- Watch the demos (videos 12-3 to 12-6) on List class in your lecture videos for how to implement the big three.
- Your TemperatureList class will be tested against other programs that use your class.
Downloads
- Download the main.cpp as a starter test program. Feel free to edit it.
- Download the original TemperatureList class for reference.
Demo
Running your class with the given main.cpp will produce the following output:
23.5 F
24.6 F
23.5 F
24.6 F
Now there's no temperatures in a.
How about temperatures in b?
23.5 F
24.6 F
How to Run the Project
Create a new project in your IDE. Copy the content of the downloaded main.cpp to your new project. Add two files in your project (note you will need to tell your IDE the type of file you are adding).
- templist.h
- templist.cpp
Run the project as usual.