CSCI 241 Midterm 2 Review Sheet

Function overloading

Operator Overloading

Dynamic memory

Consequences of using dynamic memory in a class

Two-dimensional arrays

Some specific key words and terms

const

static

References

friend

Exceptions

Examples of kinds of coding questions that might appear


Sources of information


What is not on this test?

There is nothing on the test about stacks or queues, as these were not used in the homework that was due before the test.

Likewise, there is nothing on the test about templates or recursion.


A few practice questions

  1. What are the return types and arguments of operators ++, [], +=, +, <, >>, and ==? Which of these can be const methods? Which is normally a friend function? Which ones have to be written twice?

  2. Suppose I have a variable K of type Pile *. In my program, I have a line
             K = new Pile[15];
    and later I have a line
             delete K;
    What is wrong with this? What would be a better way to do it?

    Suppose instead I simply

             K = 0;
    Is that any better?

  3. I want to have a class called Pair which provides ordered pairs (A, B) of elements of type float. The data elements of class Pair are two items of type float.
    1. Can I use the default copy constructor, destructor and assignment operator for the Pair class?

    2. Write the code for the equality operator == for the Pair class,

    3. Suppose I want to have a method called MakeMirror which returns a Pair containing the same two elements in the opposite order. (If the current instance contains (A, B), then MakeMirror() gives us (B, A).) Write the prototype statement for MakeMirror.

    4. Suppose I want to have a bool-valued function called IsMirror which will return true if two Pair objects contain the same elements in the opposite order. Write the prototype for IsMirror as a friend function.

  4. Which of the following is not always provided as a default for every class (even though the default version may not be what we want)?
         A.  basic constructor
         B.  assignment operator
         C.  operator []
         D.  copy constructor
         E.  destructor
  5. Suppose we have the following class:
             class Bundle
             {
              private:
               float Height;
               int * List;  // dynamically allocated array of int
               int Size;    // size of list
              public:
               Bundle();
               Bundle(Bundle & Other);
               // more stuff
             };

    We would also like to have an assignment operator. Here is some code for it. Fill in the blanks.

             ____________  Bundle::operator =(const Bundle & Other)
             {
              if (_________________________)
    
               {
                List = __________________;
    
                delete __________________;
    
                for (int I = 0; I < ____________; I++)
                  {
    
                   ________________________;
                  }
                Height = Other.Height; 
               }
    
              return _____________;
             }

  6. For the Bundle class as described above, write the destructor.

  7. For the Bundle class as described above, write the copy constructor.

  8. Suppose we are overloading the insertion operator (output) << for a class called Blob. Can the function we write to do this be a member of the Blob class? If it can't, why not?

  9. Write the code needed to declare a 2-dimensional array of integers with 4 rows and 8 columns. Now write the code needed to fill it with the values 1 through 32 (1-8 in row 1, 9-16 in row 2, etc). Now write the code needed to print it out sideways, that is, as 8 rows and 4 columns.

  10. When would you want to have a static data member of a class? Suppose it is public; how can we set its value? Suppose instead it is private and we have static access method for it. Now what code would we need to set the value?

  11. Suppose class Thing has a const method called XYZ. In the code for XYZ, we use another method called UVW. Does UVW have to be a const method?