/*Need Help with C++ classes. The suggestions from my instructor are:
Here are some comments:
1. All programs for this course that use a class should be “set up for separate compilation”.
Special note: a class template is different from a class, and cannot be set up for separate compilation.
2. Your resize() function has one nested if-else-if statement. This does not match the pseudocode included with the assignment. The pseudocode had 2 separate if statements (with no else).
pseudocode
/*-- DList.cpp-----------------------------------------------------------
This file implements List member functions.
-------------------------------------------------------------------------*/
#include <cassert>
#include <new>
usingnamespace std;
#include "DList.h"
//--- Definition of class constructor
List::List(int maxSize)
: mySize(0), myCapacity(maxSize)
{
myArray = new ElementType[maxSize]; // program will abort if
// allocation fails
}
//--- Definition of class destructor
List::~List()
{
delete [] myArray;
}
//--- Definition of copy constructor
List::List(const List & origList)
: mySize(origList.mySize), myCapacity(origList.myCapacity)
{
//--- Get new array for copy
myArray = new ElementType[myCapacity]; // program will abort if
// allocation fails
//--- Copy origList's elements into this new array
for(int i = 0; i < mySize; i++)
myArray[i] = origList.myArray[i];
}
//--- Definition of assignment operator
const List & List::operator=(const List & rightHandSide)
{
if (this != &rightHandSide) // check that not self-assignment
{
//-- Allocate a new array if necessary
if (myCapacity != rightHandSide.myCapacity)
{
delete[] myArray;
myCapacity = rightHandSide.myCapacity;
myArray = new ElementType[myCapacity]; // program will abort if
// allocation fails
}
//--- Copy rightHandSide's list elements into this new array
mySize = rightHandSide.mySize;
for(int i = 0; i < mySize; i++)
myArray[i] = rightHandSide.myArray[i];
}
return *this;
}
//--- Definition of empty()
bool List::empty() const
{
return mySize == 0;
}
//--- Definition of display()
void List::display(ostream & out) const
{
for (int i = 0; i < mySize; i++)
out << myArray[i] << " ";
}
//--- Definition of output operator
ostream & operator<< (ostream & out, const List & aList)
{
aList.display(out);
return out;
}
//--- Definition of insert()
void List::insert(ElementType item, int pos)
{
assert( mySize < myCapacity ); // check if there is room for item
assert( pos >= 0 && pos <= mySize ); // is insert position legal?
// First shift array elements right to make room for item
for(int i = mySize; i > pos; i--)
myArray[i] = myArray[i - 1];
// Now insert item at position pos and increase list size
myArray[pos] = item;
mySize++;
}
//--- Definition of erase()
void List::erase(int pos)
{
assert( !empty() );
assert( pos >= 0 && pos < mySize ); // is delete position legal?
// Shift array elements left to close the gap
for(int i = pos; i < mySize; i++)
myArray[i] = myArray[i + 1];
// Decrease list size
mySize--;
}
3. It looks like you used the DList.cpp file from the author’s website as a starting point for your program. I provided a slightly modified version of that file that I wanted you to start with (see link in assignment).
4. You must include preconditions and postconditions for every class member function that you add to the class or modify.
*/
/*-- DList.cpp-----------------------------------------------------------
This file implements List member functions.
-------------------------------------------------------------------------*/
#include <cassert>
#include <new>
usingnamespace std;
#include "DList.h"
//--- Definition of class constructor
List::List(int maxSize)
: mySize(0), myCapacity(maxSize)
{
myArray = new ElementType[maxSize]; // program will abort if
// allocation fails
}
//--- Definition of class destructor
List::~List()
{
delete [] myArray;
}
//--- Definition of copy constructor
List::List(const List & origList)
: mySize(origList.mySize), myCapacity(origList.myCapacity)
{
//--- Get new array for copy
myArray = new ElementType[myCapacity]; // program will abort if
// allocation fails
//--- Copy origList's elements into this new array
for(int i = 0; i < mySize; i++)
myArray[i] = origList.myArray[i];
}
//--- Definition of assignment operator
const List & List::operator=(const List & rightHandSide)
{
if (this != &rightHandSide) // check that not self-assignment
{
//-- Allocate a new array if necessary
if (myCapacity != rightHandSide.myCapacity)
{
delete[] myArray;
myCapacity = rightHandSide.myCapacity;
myArray = new ElementType[myCapacity]; // program will abort if
// allocation fails
}
//--- Copy rightHandSide's list elements into this new array
mySize = rightHandSide.mySize;
for(int i = 0; i < mySize; i++)
myArray[i] = rightHandSide.myArray[i];
}
return *this;
}
//--- Definition of empty()
bool List::empty() const
{
return mySize == 0;
}
//--- Definition of display()
void List::display(ostream & out) const
{
for (int i = 0; i < mySize; i++)
out << myArray[i] << " ";
}
//--- Definition of output operator
ostream & operator<< (ostream & out, const List & aList)
{
aList.display(out);
return out;
}
//--- Definition of insert()
void List::insert(ElementType item, int pos)
{
assert( mySize < myCapacity ); // check if there is room for item
assert( pos >= 0 && pos <= mySize ); // is insert position legal?
// First shift array elements right to make room for item
for(int i = mySize; i > pos; i--)
myArray[i] = myArray[i - 1];
// Now insert item at position pos and increase list size
myArray[pos] = item;
mySize++;
}
//--- Definition of erase()
void List::erase(int pos)
{
assert( !empty() );
assert( pos >= 0 && pos < mySize ); // is delete position legal?
// Shift array elements left to close the gap
for(int i = pos; i < mySize; i++)
myArray[i] = myArray[i + 1];
// Decrease list size
mySize--;
}
/*-- DList.h --------------------------------------------------------------
This header file defines the data type List for processing lists.
Basic operations are:
Constructor
Destructor
Copy constructor
Assignment operator
empty: Check if list is empty
insert: Insert an item
erase: Remove an item
display: Output the list
<< : Output operator
-------------------------------------------------------------------------*/
#include <iostream>
#ifndef DLIST
#define DLIST
typedefint ElementType;
class List
{
public:
/******** Function Members ********/
/***** Class constructor *****/
List(int maxSize = 1024);
/*----------------------------------------------------------------------
Construct a List object.
Precondition: maxSize is a positive integer with default value 1024.
Postcondition: An empty List object is constructed; myCapacity ==
maxSize (default value 1024); myArray points to a dynamic
array with myCapacity as its capacity; and mySize is 0.
-----------------------------------------------------------------------*/
/***** Class destructor *****/
~List();
/*----------------------------------------------------------------------
Destroys a List object.
Precondition: The life of a List object is over.
Postcondition: The memory dynamically allocated by the constructor
for the array pointed to by myArray has been returned to the heap.
-----------------------------------------------------------------------*/
/***** Copy constructor *****/
List(const List & origList);
/*----------------------------------------------------------------------
Construct a copy of a List object.
Precondition: A copy of origList is needed; origList is a const
reference parameter.
Postcondition: A copy of origList has been constructed.
-----------------------------------------------------------------------*/
/***** Assignment operator *****/
const List & operator=(const List & rightHandSide);
/*----------------------------------------------------------------------
Assign a copy of a List object to the current object.
Precondition: none
Postcondition: A copy of rightHandSide has been assigned to this
object. A const reference to this list is returned.
-----------------------------------------------------------------------*/
/***** empty operation *****/
bool empty() const;
//--- See Figure 6.1 for documentation
/***** insert and erase *****/
//if mySize==myCapacity then resize to 2*myCapacity
void insert(ElementType item, int pos);
//--- See Figure 6.1 for documentation (replace CAPACITY with myCapacity)
void erase(int pos);
//--- See Figure 6.1 for documentation
/***** output *****/
void display(ostream & out) const;
//--- See Figure 6.1 for documentation
/***** resize operation *****/
void resize (int newCapacity);
/*----------------------------------------------------------------------
Change the capacity of a list
Precondition: none
Postcondition: The list's current capacity is changed to newCapacity
(but not less than the number of items already in the list).
-----------------------------------------------------------------------*/
private:
/******** Data Members ********/
int mySize; // current size of list
int myCapacity; // capacity of array
ElementType * myArray; // pointer to dynamic array
}; //--- end of List class
//------ Prototype of output operator
ostream & operator<< (ostream & out, const List & aList);
#endif
//-------main.cpp------------------------------------------------------------
#include <cctype> // Provides toupper
#include <iostream> // Provides cout and cin
#include <cstdlib> // Provides EXIT_SUCCESS
#include <time.h> // Provides random seed for srand()
usingnamespace std;
#include "DList.h"
#include "DList.cpp"
// PROTOTYPES:
void print_menu( );
// Postcondition: A menu of choices has been printed.
int get_number( );
// Postcondition: The user has been prompted to enter an integer number. The
// number has been read, echoed to the screen, and returned by the function.
int main( )
{
List test; // A List to perform tests on
char choice; // Command entered by the user
cout << "I have initialized an empty list of integers." << endl;
do
{
print_menu( );
cout << "Enter choice: ";
cin >> choice;
choice = toupper(choice);
switch (choice)
{
case'A': // add code to add n integers to end of list
{
int n = get_number();
srand(time(0));
for (int i = 0; i < n; ++i)
{
test.insert((rand() % (n+1)),i);
}
break;
}
case'C': // add code to change the capacity of the list
{
int n = get_number();
test.resize(n);
break;
}
case'E': // add code to print result of empty( )
{
cout << (test.empty()
? "List is empty\n"
: "List isn't empty\n");
break;
}
case'P': // add code to print the list
{
cout << "List: " << test << endl;
break;
}
case'I': // add code to input an item and position, and insert item into the list
{
cout << "Please enter an item and position to insert: ";
int item = get_number();
int pos = get_number();
test.insert(item,pos);
break;
}
case'R': // add code to input a position, and erase the item at that position
{
cout << "Enter position to erase: ";
int pos = get_number();
test.erase(pos);
break;
}
case'Q': cout << "Test program ended." << endl;
break;
default: cout << choice << " is invalid." << endl;
}
}
while ((choice != 'Q'));
return EXIT_SUCCESS;
}
void print_menu( )
{
cout << endl;
cout << "The following choices are available: " << endl;
cout << " A Add a number of random integers to end of list" << endl;
cout << " C Change the capacity of the list using resize( )" << endl;
cout << " E Print the result from the empty( ) function" << endl;
cout << " P Print a copy of the entire list" << endl;
cout << " I Insert a new number with the insert(...) function" << endl;
cout << " R Remove a number with the erase( ) function" << endl;
cout << " Q Quit this test program" << endl;
}
char get_user_command( )
// Library facilities used: iostream
{
char command;
cout << "Enter choice: ";
cin >> command; // Input of characters skips blanks and newline character
return command;
}
int get_number( )
// Library facilities used: iostream
{
int result;
cout << "Please enter an integer number for the list: ";
cin >> result;
cout << result << " has been read." << endl;
system ("pause");
return result;
}
//---------listtester.cpp----------------------------------------------------
//* listtester.cpp is a program for testing class List. */
#include <iostream>
usingnamespace std;
#include "DList.h"
#include "DList.cpp"
// f() is a function with
void f(List aList) // List value parameter
{ // to test the copy constructor
for (int i = 1; i < 5; i++)
{
aList.insert(100*i, i); // insert into the copy
cout << aList << endl; // output the copy
}
}
int main()
{
// Test the class constructor
List intList;
cout << "Constructing intList\n";
// Test empty() and output of empty list
if (intList.empty())
cout << "Empty List: \n"
<< intList << endl; // Test output of empty list
// Test insert()
for (int i = 0; i < 9; i++)
{
intList.insert(i, i/2); // -- Insert i at position i/2
//Test output
cout << intList << endl;
}
cout << "List empty? " << (intList.empty() ? "Yes" : "No") << endl;
// Test destructor
{
List anotherList;
for (int i = 0; i < 10; i++)
anotherList.insert(100*i, i);
cout << "\nHere's another list:\n" << anotherList << endl;
cout << "Now destroying this list\n";
}
// Test the copy constructor
cout << "\n\n";
f(intList);
cout << "\n\nOriginal list:"; // Output the original to make
cout << intList<< endl; // sure it hasn't been changed
// Test erase
int index;
while (!intList.empty())
{
cout << "Give an index of list element to remove: ";
cin >> index;
intList.erase(index);
cout << intList << endl;
}
cout << "List is empty" << endl;
system ("pause");
}