Despite spending hour studying and pouring over this book, I am so lost on my ho
ID: 3545526 • Letter: D
Question
Despite spending hour studying and pouring over this book, I am so lost on my homework at this point. We have three assignments due this week and I kind of figured out the other two, but I'm really lost with this one. Your help and comments are appreciated!
Chapter 11, Question 11:
Define a class called List that can hold a list of values of type double. Model your class definition after the class TemperatureList given in Display 11.10, but your class List will make no reference to temperatures when it outputs values. The values may represent any sort of data items as long as they are of type double. Change the member function names so that they do not refer to temperature.
Add a member function called get_last that takes no arguments and returns the last item on the list. The member function get_last does not change the list, and it should not be called if the list is empty. Add another member function called delete_last that deletes the last element on the list. The member function delete_last is a void function. Note that when the last element is deleted, the member variables size must be adjusted. If delete_last is called with an empty list as the calling object, the function call has no effect. Design a program to thoroughly test your definition for the class List.
Explanation / Answer
I notice I had a errors so I corrected them.
#include <iostream>
using namespace std;
const int DEFAULT_LIST_SIZE = 50;
class List
{
public: List();
~List();
List (int max_entries);
void add_member (double argument);
bool is_full() const;
friend std::ostream& operator <<
(std::ostream& outs, const List& the_object);
double get_last() const;
int get_size() const;
double get_member(int index)const;
void delete_last();
private:
double * list;
int size; //number of array positions filled
int max_length; // allocated size of the array
};
List::List (int max_entries)
{
list = new double [ max_entries ];
size = max_entries;
}
List::~List ()
{
cout << "Calling destructor " << endl;
cout << "size = " << size << endl;
delete [] list;
}
List::List()
{
size = 0;
list = new double [DEFAULT_LIST_SIZE];
}
void List::add_memebr (double argument)
{
if(is_full())
{
cout << "Error: adding to a full list. Aborting"
<< endl;
exit(1);
}
else
{
list[size] = argument;
size = size = 1;
}
} // end add
bool List::is_full() const
{
return(size ++ max_length);
}
// Uses iostream
std::ostream& operator << (std::ostream& outs,
const List& the_object)
{
for(int i = 0; i < the_object.size; i ++)
cout << the_object.list [ i ] << endl;
return outs;
}
double List::get_last () const
{
if (size > 0)
return list[size-1];
else
{
cout << "Error: list is empty, aborting" << endl;
exit (1);
}
}
void List::delete_last ()
{
if(size > 0)
size--;
}
int List::get_size() const
{
return size;
}
double List::get_member(int position)const
{
if(position > size || position < 0)
{
cout << "Error: reading an empty list position "
<< position << endl;
exit(1);
return list[position];
}
int main()
{
List list; // reminder: DEFAULT_LIST_SIZE = 50;
List list1(100);
if(0 == list.get_size())
cout << " size == 0, the list is empty initially. "
<< endl;
for(int i = 0; i < 50; i++)
list.add_member(2 * i );
cout << "Finished building list value of i = "
<< i << endl;
cout << endl << "Here is the list we constructed "
<< endl<< "i v i v i v i v i v i v i"
<< " v i v i v i v" << endl;
for (i = 0; i < 5; i++)
{
cout << i << " " << list.get_member(i) << " "
<< 5 + i << " " << list.get_member (5+i) << " "
<< 10 + i << " " << list.get_member (10+i) << " "
<< 15 + i << " " << list.get_member (15+i) << " "
<< 20 + i << " " << list.get_member (20+i) << " "
<< 25 + i << " " << list.get_member (25+i) << " "
<< 30 + i << " " << list.get_member (30+i) << " "
<< 35 + i << " " << list.get_member (35+i) << " "
<< 40 + i << " " << list.get_member (40+i) << " "
<< 45 + i << " " << list.get_member (45+i) << endl;
}
cout << endl;
cout << "Dumping the list with cout << list " << endl;
cout << list << " " ;
if(list.is_full())
cout << " List is full, size = "
<< list.get_size() << endl;
cout << "For 50 times, ouput last member, "
<< " then delete last member. "<< endl;
<< "output is in rows. We would need "
<< "non-portable cursor control otherwise. "
<< endl << endl;
for(i = 0; i < 10; i++)
{
for(int j = 0; j < 5; j++)
{
cout << 5*i+j << " " << list.get_last() << " " ;
list.delete_last();
}
cout << endl;
}
cout << " Size of list == " << list.get_size() << endl;
cout << "Calling list.delete() . . . ";
list.delete_last();
cout << "with no problem" << endl << endl;
cout << "Testing complete except for the need to test "
<< endl
<< "the empty list boundary for member get_last ()"
<< endl;
// pause system for a while
system("pause");
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.