17-arrays-copies.student

17-arrays-copies.student - Last time The"big instance...

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

View Full Document Right Arrow Icon
Last time: * The "big instance" problem * Dynamic objects---the heap * new/delete; new[]/delete[] Today: * Classes that can grow: dynamic members * Classes with dynamic members: destructors * Shallow vs. deep copies +++++++++++++++++++++++++++++++++++++++++++++++++ We can now build a version of IntSet that allows the client to specify how large the capacity of the set should be. The data elements will change slightly: class IntSet { int *elts; // pointer to dynamic array int sizeElts; // capacity of array int numElts; // current occupancy public: ... }; So, rather than hold an array explicitly, we have a pointer that will (eventually) point to a dynamically-created array. sizeElts tells us the size of the allocated array (which is not necessarily MAXELTS) while numElts still tells us how many elements there actually are. We'll base our changes on the unsorted implementation. The methods are mostly unchanged. There is a new default constructor: IntSet::IntSet() { // Allocate the "default-size" array elts = new int[MAXELTS]; sizeElts = MAXELTS; numElts = 0; } In addition to the default constructor, we can also write an "alternate constructor". It has the same name as the default constructor, but a different type signature: class IntSet { int *elts; // pointer to dynamic array int sizeElts; // capacity of array int numElts; // current occupancy public: IntSet::IntSet(); // default constructor // EFFECTS: create a set with MAXELTS capacity IntSet::IntSet(int size); // constructor w/ explicit capacity // REQUIRES: size > 0 // EFFECTS: create a set with size capacity ...
Background image of page 1

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

View Full Document Right Arrow Icon
}; The alternate constructor creates an array of the specified size: IntSet::IntSet(int size) { elts = new int[size]; sizeElts = size; numElts = 0; } This is called "function overloading"; two different functions with exactly the same name, but different argument type signatures. Since the compiler knows the argument types, it can pick the "right" constructor when a new object is created. For example: IntSet is1; // No arguments---calls the default constructor IntSet is2(200); // An integer argument---calls alternate Notice that the two constructors are nearly identical; the only difference is whether we use "size" or MAXELTS. Otherwise the code is duplicated. This is awful; when we find ourselves writing the same
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

17-arrays-copies.student - Last time The"big instance...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online