#include #include #include void pbin(int n, int bits) { unsigned int mask = 1<<(bits-1); while (mask != 0) { if (n & mask) cout << "1"; else cout << "0"; mask >>= 1; } } void investigate(char name[], void * address, int size) { cout << name << " at address " << (unsigned int)address << " = "; pbin((unsigned int)address, 32); cout << "\n"; for (int i=0; itag; if (tag == 'C') { cat * cp = (cat *)p; cout << "bday=" << cp->bday << ", name='" << cp->name << "'\n"; } else if (tag == 'D') { dog * dp = (dog *)p; cout << "bday=" << dp->bday << ", price=" << dp->price << "\n"; } } void main() { cat * c1 = new cat; c1->tag='C'; c1->bday=20060815; strcpy(c1->name, "jim"); dog * c2 = new dog; c2->tag='D'; c2->price=199; c2->bday=20051107; void * pets[3]; pets[0] = c1; pets[1] = c2; investigate("cat c1", c1, sizeof(dog)); investigate("dog c2", c2, sizeof(cat)); investigate("pets", pets, sizeof(pets)); print(c1); print(c2); print(pets[0]); print(pets[1]); }