Source files (for downloading):
#ifndef INTLIST_H #define INTLIST_H class IntList { public: // Constructors IntList(); IntList(int size); IntList(const IntList &L); // copy constructor ~IntList(); // destructor IntList & operator=(const IntList &L); // assignment // Other public member functions void addToEnd(int k); // adds value k to the end of the list void print() const; // prints out the list (using cout) private: static const int SIZE = 10; // default initial size of array int *items; // dynamically allocated array of ints int numItems; // number of items currently in the list int arraySize; // current size of the array }; #endif
#include <iostream> #include "IntList.h" using namespace std; // Default (no-argument) constructor IntList::IntList() : items(new int[SIZE]), numItems(0), arraySize(SIZE) { } // Constructor - takes an initial size IntList::IntList(int size) : items(new int[size]), numItems(0), arraySize(size) { } // Copy constructor IntList::IntList(const IntList &L) : items(new int[L.arraySize]), numItems(L.numItems), arraySize(L.arraySize) { for (int k = 0; k < numItems; k++) items[k] = L.items[k]; } // Destructor IntList::~IntList() { delete [] items; } // Overload the assignment operator IntList & IntList::operator=(const IntList &L) { // check for self-assignment - if so, do nothing if (this == &L) return *this; else { delete [] items; // free storage pointed to by items items = new int[L.arraySize]; // allocate new array arraySize = L.arraySize; // set arraySize // copy items from L's array to the new array // (and also set numItems) for (numItems = 0; numItems < L.numItems; numItems++) items[numItems] = L.items[numItems]; } return *this; } // addToEnd - adds item k to the end of the list // If the array is full, the array is first doubled in size. void IntList::addToEnd(int k) { // check if enough room, if not, double the array if (numItems == arraySize) { int *newItems = new int[arraySize*2]; for (int i = 0; i < numItems; i++) newItems[i] = items[i]; delete [] items; items = newItems; arraySize *= 2; } // add the item items[numItems] = k; numItems++; } // print - prints out the list to the console (using cout) void IntList::print() const { for (int i = 0; i < numItems; i++) cout << items[i] << " "; cout << endl; }
#include <iostream> #include "IntList.h" using namespace std; void copyAgain(IntList L) { IntList Lnew = L; Lnew.print(); L.print(); } int main() { IntList L1; IntList L2(25); IntList *p, *q; p = new IntList; q = new IntList(25); for (int i = 0; i < 20; i++) L1.addToEnd(i+1); L1.print(); L2 = L1; L2.addToEnd(-1); IntList L3(L1); L1.print(); L2.print(); L3.print(); copyAgain(L3); // delete p; // delete q; return 0; }
# Makefile for the IntList example # (all this goes in a file named 'Makefile') main: testIntList.o IntList.o g++ testIntList.o IntList.o testIntList.o: testIntList.cpp IntList.h g++ -c testIntList.cpp IntList.o: IntList.cpp IntList.h g++ -c IntList.cpp clean: rm *.o
king06(1)% ls IntList.cpp IntList.h Makefile testIntList.cpp king06(2)% make g++ -c testIntList.cpp g++ -c IntList.cpp g++ testIntList.o IntList.o king06(3)% ls a.out IntList.h Makefile testIntList.o IntList.cpp IntList.o testIntList.cpp king06(4)% rm IntList.o rm: remove regular file `IntList.o'? y king06(5)% make g++ -c IntList.cpp g++ testIntList.o IntList.o king06(6)% make clean rm *.o king06(7)% ls a.out IntList.cpp IntList.h Makefile testIntList.cpp king06(8)% make g++ -c testIntList.cpp g++ -c IntList.cpp g++ testIntList.o IntList.o king06(9)% make IntList.o make: `IntList.o' is up to date.