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.
Draft!
- Note 6: C++ class introduction, std::string
- Not: C-style strings
- CPP,
#include
, #ifndef
magic
- Memory maps
- declaring vs. defining variables, classes, methods, functions
- review, but important here as well: scope (where accessible) vs
lifetime (how long an identifier holds its value)
- Declare identifier before can use it
- Separating method definitions from declarations
- Separate compilation with #include, linkers
- Note 7: Pointers and Classes
- addresses, pointers, dereference, heap, stack, dangling pointer,
memory leak
- classes as the key feature of C++
- creating your own built-in types through operator overloading
- No expectation to define operators, but should understand them
- Stack vs. heap declaration of objects
- inline methods in classes,
virtual
, virtual functions,
: public
const
methods
- default constructor
- subclass
- destructor,
delete
- Implementing growable arrays
- Using pointers with domain classes (classes that are a necessary
part of the problem, as opposed to classes that are part of a
particular solution)
- Note 8: Floating-Point
- 32-bit ieee 754 format, including special bit patterns
- Be able to converting from the bit pattern to a value; you
will not be tested on doing it the other way, but you should
know the steps
- not comparing with == except against 0.0
- Understanding why addition is not associative for floats - that is,
(a+b)+c != a+(b+c) in some cases
- Rational for knowing floating-point representation
- cost of floating-point operations - where are the loops?
- single vs. double precision
- understanding types: set of values, bit patterns, abstract data
types; why are these important?
Note 9: Inheritance
- virtual methods, inheritance polymorphism,
override
- more on declaration vs. definition
- pointers, destructors,
delete
- abstract methods, classes,
= 0
- vtables
- pass by
const&
- namespaces
- organizing C++ projects, especially
#include
s
- Templates
template
functions, type parameters, consistency of types
- template classes, specializing by type and size
- Java generics can hold objects of different types - unchecked containers
- STL:
list
, vector
, []
vs .at
for vector
- Key container operations:
.begin
, .end
- other containers:
map
, stack
<algorithm>
- duck typing in Python, static vs. dynamic typing, C++ STL
- NOTE: some topics may be added
Exercises/Questions
- 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.
- 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.
- 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.
- 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.
- Using pointers, not indices, write C++ code to return a pointer
to the first positive number in an array of doubles
- 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.
- Some argue all methods in C++ classes should be virtual. Why? Why
not? There should be at least two reasons on the negative side.
- Why should
inline
code be in header files?
- 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.