# Assi V - DALHOUSIE UNIVERSITY DEPARTMENT OF ENGINEERING...

This preview shows pages 1–3. Sign up to view the full content.

DALHOUSIE UNIVERSITY DEPARTMENT OF ENGINEERING MATHEMATICS ENGM2282 ASSIGNMENT # 5, Due Feb. 11, 2010 1. Consider the program integerlist.cpp and the class intlist which uses the integer array data to store a list of integers. The member max is the maximum number of integers which can be stored. The member count is the current number of integers stored. The method add stores an integer in the list. The friend show prints the list of integers to the screen. /* File: integerlist.cpp Implements a list of integers */ #include <iostream> #include <cstdlib> using namespace std; class intlist { private: int* data; // the list is stored in this array int max; // the maximum number of integers in the list int count; // the number of integers currently in the list // these are stored in entries data[0] to data[count-1] public: intlist(int size); // constructor void add(int x); // x is stored at index count friend void show(const intlist& p); // print the list to the screen }; int main(void) { intlist list1(10); char choice; list1.add(1); list1.add(3); list1.add(5); cout<< "list1 = "; show(list1); cout << "Type c to make a copy of the list\n"; cout << "Type a to assign the list\n"; cin >> choice; if(choice == 'c') { intlist list2(list1); cout << "list2 = "; show(list2); } else if(choice == 'a') { intlist list3(5); list3 = list1; cout << "list3 = "; show(list3); } // check that list1 is still OK cout << "list1 = "; show(list1); system("pause"); return 0; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
{ int i; for(i=0; i < p.count; i++){ cout << p.data[i] << " "; } cout << endl; } intlist::intlist(int size) { max = size; count = 0; data = new int[max]; } void intlist::add(int x) { if(count == max) { cout << "the list is full\n"; exit(1); } data[count] = x; count++; } (a) Run the program twice. In the first run choose to copy the list and in the second run choose to assign the list. For marking purposes capture your output for both runs. Does the program appear to function correctly? Explain why the class intlist has memory leaks. (b) Add the destructor to the class as shown below. class intlist { private: int* data; // the list is stored in this array int max; // the maximum number of integers in the list int count; // the number of integers currently in the list // these are stored in entries data[0] to data[count-1] public: intlist(int size); // constructor ~intlist(void); // destructor void add(int x); // x is stored at index count
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/21/2010 for the course ENGM 2282 taught by Professor Phillips during the Winter '10 term at Dalhousie.

### Page1 / 8

Assi V - DALHOUSIE UNIVERSITY DEPARTMENT OF ENGINEERING...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online