Test 1: Answer Sheet
CS250 First Test: Fall 1998     (Put Student ID on all pages handed in) Student ID: ___________________________________________

Honor Pledge: I pledge that I have neither given nor received unauthorized help in taking this exam. __________________________________

 

Part 1: Terminology: 30 points (2 points each)

Using the terminology table included, answers the following questions:

 

  1. 37 (Scope Resolution Operator)_The double colon ("::") which is used to associate a member function with its class. Needed to disambiguate function definitions for the compiler.
  2. 20. Objects17 class Any thing, real or abstract about which we store data and those methods (operations) that manipulate that data (Martin/Odell)
  3. 14. has-a relationship   A relationship between classes in which one class has data members which are from the other class. also known as a composition relationship
  4. 33. Member access operator  The single dot ('.') which is used to access both the data and function members of an object. general syntax: objectID.MemberName
  5. 21. Non-default constructor A constructor with parameters that is called when a newly created object has arguments.
  6. 1. abstraction   Define essential properties and behaviors while hiding the inessential details.
  7. 18, State (of object)  The set of all attributes of an object, the properties of an object -  the values of the object's data members
  8. 16. pre-condition  Says what must be true before a function is executed. Usually places restrictions on the values of the input parameters.
  9. 35. Member functions Functions defined for a class. also known as the behaviors of the class, the methods of the class or the operations of the class.
  10. 23. or 4.  prototype  Information for the compiler and user about how to use a function. Gives function name, return type, a parameter list (number and type of arguments in order)
  11. 26. Overloading Functions Results whenever the same function name is used for more than one function. The compiler resolves which function should be called by examining the function's signature: that is, the number and type of the parameters.
  12. 17. or 30. class   A set of objects defined by common attributes or properties (data members) and common behavior or operations (function members).
  13. 39. Object Oriented Analysis  Process of examining the application requirements to discover the classes and objects, their state, behaviors and relationships in the vocabulary of the problem domain.
  14. 10. attribute 5. data member   A property of an object. data whose value defines a characteristic of the object.
  15. 7. Local Variable2 call by value9 parameter  Variable whose visibility and lifetime are limited to the function in which it is defined, cannot be accessed outside the function and only is allocated when the function is invoked (Called).
 

 


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)