6.2. Derivation with Polymorphism

[ fromfile: inheritance-intro.xml id: polymorphism ]

We can now discuss a very powerful feature of object-oriented programming: polymorphism. Example 6.6 differs from the previous example in only one important way: the use of the keyword virtual in the base class definition.

Example 6.6. src/derivation/qpoly/student.h

[ . . . . ]
class Student  {
 public:
    Student(QString nm, long id, QString major, int year = 1);
    virtual QString getClassName() const;   1
    QString toString() const; 2
    virtual ~Student() {}  3
    QString yearStr() const;
 private:
    QString m_Name;
    QString m_Major;
    long m_StudentId;
 protected:
    int m_Year;
};
// derived classes are the same as before...
[ . . . . ]

1

Note the keyword virtual here.

2

This should be virtual, too.

3

It is a good idea to make the destructor virtual, too

By simply adding the keyword virtual to at least one member function we have created a polymorphic type. When virtual is specified on a function, it becomes a method in that class and all derived classes. Example 6.7 shows the same client code again:

Example 6.7. src/derivation/qpoly/student-test.cpp

#include <QTextStream> 
#include "student.h"

static QTextStream cout(stdout, QIODevice::WriteOnly); 

void graduate(Student* student) {
    cout << "\nThe following "
         << student->getClassName()
         << " has graduated\n "
         << student->toString() << "\n";
}

int main() {
    Undergrad us("Frodo", 5562, "Ring Theory", 4);
    GradStudent gs("Bilbo", 3029, "History", 6, GradStudent::fellowship);
    cout << "Here is the data for the two students:\n";
    cout << gs.toString() << endl;
    cout << us.toString() << endl;
    cout << "\nHere is what happens when they graduate:\n";
    graduate(&us);
    graduate(&gs);
    return 0;
}

Running this version of the program produces slightly different output, as shown here

Here is the data for the two students:
[GradStudent] name: Bilbo Id: 3029 Year: gradual student Major: History
  [Support: fellowship ]

[Undergrad] name: Frodo Id: 5562 Year: senior Major: Ring Theory

Here is what happens when they graduate:

The following Undergrad has graduated
 [Undergrad] name: Frodo Id: 5562 Year: senior Major: Ring Theory

The following GradStudent has graduated
 [GradStudent] name: Bilbo Id: 3029 Year: gradual student Major: History
 [23]
  

Notice that we now see [GradStudent] and [UnderGrad] in the output, thanks to the fact that getClassName() is virtual. There is still a problem with the output of graduate() for the GradStudent, however. The Support piece is missing.

With polymorphism, indirect calls (via pointers and references) to methods are resolved at runtime. This is sometimes called dynamic, or late runtime binding. Direct calls (not through pointers or references) of methods are still resolvable by the compiler. That is called static binding or compile time binding.

In this example, when graduate() receives the address of a GradStudent object, student->toString() calls the Student version of the function. However, when the Student::toString() calls getClassName() (indirectly through this, a base class pointer), it is a virtual method call, bound at runtime.

Try adding the keyword virtual to the declaration of toString() in the Student class definition so that you can see the support data displayed properly.

In C++, dynamic binding is an option that one must switch on with the keyword virtual.



[23] What happened to the Fellowship?