Virtual Functions: C Example
In C, imagine that you somehow had a linked list of employees -- both managers and non-managers (this would already require ugly typecasting). How would you print them in C?
for (emp = employee_list; emp != NULL; emp = emp->next) {
if (TypeOf(emp) == TYPE_EMPLOYEE))
else if (TypeOf(emp) == TYPE_MANAGER))
else if (TypeOf(emp) == TYPE_SUMMER_STUDENT))
print_summer_student(emp);
printf(“Degenerate list! Invalid employee type!\n”);
This method is clumsy, ugly, bug-prone, and causes tooth decay.