








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Fall 2005;
Typology: Study notes
1 / 14
This page cannot be seen from the preview
Don't miss anything!









7:00pm – 8:15pm Monday, October 3
class Triple { public:
Triple(); // initializes object to (0, 0, 0)
Triple(int theX, int theY, int theZ); // initializes object to // (theX, theY, theZ) int getX(); // returns x value
int getY(); // returns y value
int getZ(); // returns z value
void initVals(int theX, int theY, int theZ); // assigns object to // (theX, theY, theZ) private:
int x; int y; int z; };
On the next page, there are three code snipppets. For each snippet, answer the question above the snippet.
Consider the following class:
class Values { public: // no-argument constructor Values();
// assignment operator
// ANSWER: Values const & operator=(Values const & origVal);
private: Array<String> firstNames; Array
The no-argument constructor of this class should initialize the object so that each array is of size 10 and every pointer points to an object of the appropriate type, rather than NULL or garbage. Non-pointer values can be left as garbage. We don’t care about the indices of the arrays as long as there are 10 cells in each array. Write the declaration for the assignment operator above, and write the definition for both the no-argument constructor and the assignment operator (below and/or on the next page), as they would appear in the .cpp file. (Do not worry about #include statements.)
(Dynamic Memory, continued)
Values::Values() { firstNames = new Array<String>(1, 10); for (int i = 1; i <= 10; ++i) (firstNames)[i] = new String(); numbers.setBounds(1, 10); lastNames.setBounds(1, 10); for (int i = 1; i <= 10; ++i) lastNames = new String(); collectionName = new String(); }
Values const & Values::operator=(Values const & origVal) { if (this != &origVal) { for (int i = firstNames->lower(); i <= firstNames->upper(); ++i) delete (*firstNames)[i]; delete firstNames;
for (int i = lastNames.lower(); i <= lastNames.upper(); ++i) delete lastNames[i];
delete collectionName;
firstNames = new Array<String>(origVal.firstNames->lower(), origVal.firstNames->upper()); for (int i = firstNames->lower(); i <= firstNames->upper(); ++i) (firstNames)[i] = new String((((origVal.firstNames))[i]));
numbers = origVal.numbers; lastNames.setBounds(origVal.lastNames.lower(), origVal.lastNames.upper()); for (int i = lastNames.lower(); i <= lastNames.upper(); ++i) lastNames[i] = new String(*((origVal.lastNames)[i]));
collectionName = new String(*(origVal.collectionName)); } return *this; }
(a) Convert the following class declaration, into the declaration of a template class Pair which holds both an int, and some type associated with that int (not necessarily a String, as below, but any type). (You do not have to reproduce the comments.)
class NamedInt { public: // initializes member variables to parameter values NamedInt(int theValue, String theName);
// returns value of integer member variable int getValue();
// returns value of String member variable String getName();
private: int value; String name; };
template
int getValue();
Etype getItem();
private: int value; Etype item; };
(b) Write the copy constructor definition for this template class, as if the copy constructor had been declared in the class declaration and you were writing part of the template definition file.
template
Consider the following class:
class Bottle { public: // parameter capacity is in liters, and becomes capacity of bottle; // bottle will also be completely full (i.e. contents equals capacity) Bottle(double theCapacity);
virtual ~Bottle() {}
// fills bottle back to completely full void refill();
// parameter sipSize is some amount of liquid (in liters). If // sipSize <= contents, reduce contents by parameter amount // and return true; else return false. boolean drink(double sipSize);
double getCapacity(); // returns capacity of bottle (in liters) double getContents(); // returns amount of liquid in bottle (in liters)
private: double capacity; // number of liters the bottle can hold double contents; // number of liters the bottle currently holds };
You want to write a class SpigotBottle that is a derived class of Bottle. A SpigotBottle has a button-activated valve in it; when you press the button, a pre-set amount of liquid flows out of the bottle.
(Inheritance, continued)
class SpigotBottle : public Bottle { public: SpigotBottle(double capacity, double theFlow); double useSpigot(); private: double valveFlow; };
SpigotBottle::SpigotBottle(double capacity, double theFlow) : Bottle(capacity) { valveFlow = theFlow; }
double SpigotBottle::useSpigot() { double whatIsLeft = getContents(); if (valveFlow <= whatIsLeft) { drink(valveFlow); return valveFlow; } else { drink(whatIsLeft); return whatIsLeft; } }
(scratch paper, page 1)
(scratch paper, page 2)