Chapter 20. Examining Virtual Member Functions: Are They for Real?

In This Chapter

  • Discovering how polymorphism (a.k.a. late binding) works

  • Finding out how safe polymorphic nachos are

  • Overriding member functions in a subclass

  • Checking out special considerations with polymorphism

The number and type of a function's arguments are included in its full, or extended, name. This enables you to give two functions the same name as long as the extended name is different:

void someFn(int);
void someFn(char*);
void someFn(char*, double);

In all three cases, the short name for these functions is someFn() (hey! this is some fun). The extended names for all three differ: someFn(int) versus someFn(char*), and so on. C++ is left to figure out which function is meant by the arguments during the call.

Member functions can be overloaded. The number of arguments, the type of arguments, and the class name are all part of the extended name.

Inheritance introduces a whole new wrinkle, however. What if a function in a base class has the same name as a function in the subclass? Consider, for example, the following simple code snippet:

class Student
    double calcTuition();
class GraduateStudent : public Student
    double calcTuition();

int main(int argcs, char* pArgs[])
   Student s;
   GraduateStudent gs;
   s.calcTuition(); //calls Student::calcTuition()
   gs.calcTuition();//calls GraduateStudent::calcTuition()
   return 0;

As with any overloading situation, when the programmer refers to calcTuition()

Get C++ For Dummies®, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.