CSC 2210, Hasker's Section: Exam 2 Review

The exam is closed-note, closed-book. If you need to know the name of a method (other than .begin, .end) in the STL, I will give that name to you.

Exercises/Questions

  1. Most phones have a simple app that allows you to take notes. Write a class Note that captures a note (a string) and timestamp (milliseconds since 00:00 on Jan. 1 1970). It should have a message to return the note text and a print() method that writes a formatted date followed by a newline followed by the note text and another newline. Assume the library function void prDate(int ms); does the appropriate formatting (but don't write this function). The constructor for the class should set both attributes.
    • Write the header file you would use for your class.
  2. Write a class NoteBook that contains an arbitrary-sized collection of pointers to Note objects with a constructor that sets the collection to be empty, an add method that takes a pointer to a Note and adds it to the collection, and a print method that prints all notes. You can use the STL in your answer.
  3. Implement a template Queue class in C++
    • Implement this as a linked list using the class
              class Node {
                Node *nextNode;
                ItemType _item;
              public:
                Node(ItemType item, Node *next = nullptr) : nextNode(next), _item(item) { }
                ItemType item() const { return _item; }
                Node* next() { return nextNode; }
              };
      
      Note your template type parameter should be named ItemType so you do not have to rewrite this class in your solution. Note you will need a pointer to the front of the list and the back of the list; new items will be added to the back (by a method add), old items removed from the front (by a method take). Include a method empty that returns true if the Queue has no items.
    • Implement a destructor for Queue.
    • What would happen if you passed a Queue by value? That is, what would be the impact of the code
              void pr(Queue<string> todo) {
                 while ( !todo.empty() }
                   cout << todo.take() << endl;
              }
      
    • Would changing the prototype for pr to
              void pr(const Queue<string> &todo);
      
      fix this?
    • Add a method to Queue to remove all items matching a given target. For extra credit, the method should return the Node's to the heap.
  4. Write a C++ function that takes a list<string> parameter and returns a new list with every other item. The function should run in O(n) time.
  5. Using pointers, not indices, write C++ code to return a pointer to the first positive number in an array of doubles
  6. Create a class A and a subclass B, both with virtual methods xyz, and draw their vtables. Add a virtual method uvw for B and a virtual method abc for A, and add these to the vtables.
  7. Some argue all methods in C++ classes should be virtual. Why? Why not? There should be at least two reasons on the negative side.
  8. Why should inline code be in header files?
  9. Why are IEEE Std 754 floats implemented using three fields? (Note programmers can write code without being aware of these fields, but they are critical to understanding details about floating-point numbers.)

Code snippets related to these are available.