Name: ____________________________________________________ Alpha: _____________________
Describe help received: _________________________________________________________________
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!]
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!]
void doer() ....
What would the memory layout look like then? [Draw a picture]
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]
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.
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?