Expedia Interview Question
SDE-2sTeam: LSB
Country: United States
Interview Type: Phone Interview
class Animals {
public:
virtual void eat () = 0;
};
class dog : public Animals {
public:
void eat () { do stuff; }
};
class cat : public Animals{
public:
void eat () { do stuff; }
};
int main ()
{
dog d;cat g;
Animals *p;
p = &d;
p->eat () // dog eat wiil be called,
p = &g;
p->eat(); // cat eat will be called
Virtual function lookup tables stored within an object. When you invoke a virtual member function in your code, the compiler sets a function identifier (really its an offset into this lookup table) that says which function you should invoke. Now if a subclass want to override a given function, it overwrites that entry in the lookup table for the subclass and viola, when time to invoke the overridden function, you jump to the correct one.
- Anonymous August 04, 2014