Honor Pledge: I pledge that I have neither given nor received unauthorized help in taking this exam. __________________________________
Using the terminology table included, answers the following questions:
Part II (70 point total, 5 points each partial credit in parentheses)
You may use blank paper to answer: BUT label each problem clearly and put your student ID on every page handed in
1. Example 1: Coordinate. How many constructors does this class have? How does the
compiler know which constructor to use?
Three; Lines 3,4,5 (2)
BEST: by the function's signature (number, order and type of the
parameter/arguments)
GOOD: by the number and types of the arguments
OK - number of parameter/arguments (this is all you need for this particular example - but
does not work in all cases)
(3, -2 if only say parameters but not what about them is used by the compiler)
2. Example 1: Coordinate: rewrite << (shown below) so that it is not a friend
of the class. (you can cross out unneeded lines and add new one or write out entire
definition on a separate page).
ostream & operator << (ostream & oStream, const Coordinate & aPoint) { oStream << aPoint.getX() << " " << aPoint.getY() << " " << aPoint.getDisplay(); return oStream; } 1 point each (-1 if no parenthesis)
What changes if any would you have to make the specification (shown lines modified, added or deleted to example 1).
Remove the "friend" modifier from line 13(1) and move after line 18(1)
3. Example 1: Coordinate. Change this program so it works correctly.
#include "Coordinate.h" void main() { Coordinate point1; cout << point1.getX(); }
-2 if no parenthesis
-2 if no point1.
-2 const
-2 is output whole point
-1 if add input
4. Example 1: Coordinate. Is the function "distance" allowed to modify the
values of its arguments? NO - call by value (3)
of its parameters? Yes - but has no effect on the arguments(2)
if backwards -4
5. Example 2: FixedString. Give the prototype for a selector member function which will return the C-style string that is a copy of the value stored in the object. After what line would you put this prototype? Anywhere between 5 and 9 (1)
// Here are three ways to do this
void getString(char * cStr) const; // calling program allocates space for string and this function copies // string into that space // PRE-CONDITION: cStr is a character array big enough to contain the string // POST-CONDITION: the array cStr has a copy of this string void getString(char ** cStr) const; // this function allocates space for string on heap // POST-CONDITION: changes cStr to point to newly allocated space // which contains a copy of this string. char * getString() const; // this program allocates space for string on heap // POST-CONDITION: returns pointer to a newly allocated character array containing a // copy of this string
// although not necessary to the answer - here are the definitions of the above
void FixedString::getString(char* cStr) const { strcpy(cStr,str); } char * FixedString::getString() const { char * strPtr; strPtr = new char[MaxSize+1]; strcpy(strPtr,str); return strPtr; } void FixedString::getString(char ** cStr) const { char * strPtr; strPtr = new char[MaxSize+1]; strcpy(strPtr,str); *cStr = strPtr; }
6. Example 2: FixedString. The private data member can hold a string up to 256 characters long, but how can you tell the length of a particular FixedString Object? Use strlen - which checks for the null character '\0' to mark the end. (2)
Add a second data member called "length" which keeps the length of the FixedString (be sure to show after what line you would add this member).
int length; // (2)
// put this line between 9 and 11(1)
7. Example 2: Fixed String. Modify the default constructor (shown below) to keep this length attribute consistent.
FixedString::FixedString() { str[0] = '\0'; length = 0; // (3) }
Write the postcondition for the default constructor of FixedString shown above.
// POST-CONDITION: set string to the empty (or null) string.
// saying sets array? -1
8. Example 3: Name. Define << so that it prints the last name followed by a comma and space and then the first name with no other output. Here is the current definition (you can cross out unneeded lines and add new one or write out entire definition on a separate page).
ostream & operator <<(ostream & oStream, const Name & thisName) { oStream << thisName.lastName << ", " << thisName.firstName; // extra output (-1 endl or -2 for labels) // wrong order -3 return oStream; // -1 if missing }
9. Example 3: Name. If we changed the output type of SetFirst to FixedString (and return firstName), what would the following statements do? (type undefined, if these statements won't compile or won't execute).
Name myName; myName.SetLast(myName.SetFirst("wild")); cout << myName; // use the definition shown above in problem 8 // sets firstName and lastName in the object named myName to "wild" and prints out
First Name: wild Last Name: wild
// output format correct -2 // values incorrect -3
10. Example 3: Name. Change the last constructor (shown below) to eliminate the constructor initializer and rewrite to accomplish the same task. If not possible, say it cannot be done. (you may cross out the unneeded parts and add lines below or write out entire definition on a separate piece of paper).
Name::Name(const FixedString & first, const FixedString & last) : firstName(first), lastName(last) // must cross out ':' (1) above and line above (2) {
// firstName and lastName are already constructed with the default constructor, so firstName = first; // just use the for free assignment or the set functions! (1 point each) lastName = last;
}
// Name.setFirst -1
11. Example 4:Course. Why does this statement have double quotes and single quotes?
Course aCourse( "MTH_3020", 'B', 2 ); // this is the non-default constructor which sets the name data member // (which is of type character array) to "MTH_3020" // which is a string (character array) and needs the double quotes(3) // sets section data member (which is of type char) to a single character enclosed in // single quotes (2) // Remember that "B" is a character array containing two characters - // the character 'B' and the character '\0'
12. Example 4 and 5: If you remove the modifier private from the specifications of Registration and Course, then you could write the following
Registration R; cout << R.courses[1].name[0];
What is the type of R? Registration
R.courses? array of Course
R.courses[1]? Course
R.courses[1].name? string (array of char)
R.courses[1].name[0]? char
13. Example 4: Course. Change the specification of Course to use a FixedString object instead of a C-Style String. Be sure to tell what lines you would modify or where you would add any new lines.
// change line 12 in example 4 to the following (3)
FixedString name;
// but don't forget to include the header file before or after
line 1 (2)
#include "FixedString.h"
// changing line 6 is optional +1 extra credit
Course(const FixedString & nam, char sect, unsigned cred);
// could delete line 2 since no longer used
14. Example 5: Registration. Give the prototype of a member function of Registration which will return the i-th course. After what line would you put this prototype?
// Anywhere between lines 4 and 10 in example 5 (1)
Course getCourse(int i) const; // argument (2), return value (2)