O'Reilly logo

C++ For Dummies®, 6th Edition by Stephen R. Davis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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
{
  public:
    double calcTuition();
};
class GraduateStudent : public Student
{
  public:
    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()

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required