C++ Classes Help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*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>
using namespace 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>
using namespace 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
typedef int 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);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*----------------------------------------------------------------------
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()
using namespace 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>
using namespace 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");
}
[/code]
Ok. That's a lot of code. You're going to have to be a bit more specific about what you want looked at.
Topic archived. No new replies allowed.