Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

Problem 1

Suppose we make the following class definition in C++:
class Thing	
{
public:
  char u;
  int x;
  void doer() { cout << "Thing!" << endl; }
};
How many bytes should we expect an instance of Thing to take up, and what would the offsets of u and x be? [Draw a picture!]

Problem 2

Suppose we make the following class definition in C++:
class Ginth
{
public:
  char u;
  int x;
  char v;
};
How many bytes should we expect an instance of Ginth to take up, and what would the offsets of u, x and v be? [Draw a picture!]

Problem 3

Continuing from Problem 1, Suppose we put the "virtual" keyword in front of void doer() .... What would the memory layout look like then? [Draw a picture]

Problem 4

Continuing from Problem 3 (where we added the "virtual" keyword), suppose we defined a new class derived from Thing:
class Mathom : public Thing	
{
public:
  int k;
  void doer() { cout << "Mathom!" << endl; }
};
What would the memory layout look like for an instance of Mathom? [Draw a picture]

Problem 5

If p has type Thing* and points to a valid object (though we don't know whether an actual Thing or a Mathom), describe what happens when the call p->doer() is executed that allows the program to execute Thing's doer() when p points to a Thing, and Mathom's doer() when p points to a Mathom.

Problem 6

Why are polymorphic function calls (as in Problem 4) inherently slower than non-polymorphic calls?

Problem 7

Suppose we have the following declarations:
Thing A;	
Mathom B;
Thing *p;
... and suppose that at some later point p is set to point to an actual object (not A, not B, but some other Thing or Mathom). How could you tell (without calling any functions or macros) whether p was pointng to a Thing or a Mathom? (Note: A and B are still in scope!) Why would this not be possible in the scenario in which doer() was not defined as "virtual" in Thing?