[ fromfile: inheritance-intro.xml id: polymorphism ]
Example 6.6. src/derivation/qpoly/student.h
Adding the keyword virtual
to at least one member function creates a polymorphic type.
virtual
functions are called methods.
Example 6.7. src/derivation/qpoly/student-test.cpp
#include <QTextStream> #include "student.h" static QTextStream cout(stdout); void finish(Student* student) { cout << "\nThe following " << student->getClassName() << " has applied for graduation.\n " << student->toString() << "\n"; } int main() { Undergrad us("Frodo Baggins", 5562, "Ring Theory", 4, 1220); GradStudent gs("Bilbo Baggins", 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 finish their studies:\n"; finish(&us); finish(&gs); return 0; }
When you run this slightly changed program you get the following output.
Here is the data for the two students: [GradStudent] name: Bilbo Baggins; Id: 3029; Year: gradual student; Major: History [Support: fellowship ] [Undergrad] name: Frodo Baggins; Id: 5562; Year: senior; Major: Ring Theory [SAT: 1220 ] Here is what happens when they finish their studies: The following Undergrad has applied for graduation. [Undergrad] name: Frodo Baggins; Id: 5562; Year: senior; Major: Ring Theory [28] The following GradStudent has applied for graduation. [GradStudent] name: Bilbo Baggins; Id: 3029; Year: gradual student; Major: History[29]
With polymorphism, indirect calls (via pointers and references) to methods are resolved at runtime.
This is called dynamic, or runtime binding.
Direct calls (not through pointers or references) of methods are resolved by the compiler.
That is called static binding or compile time binding.
In C++, dynamic binding is an option that one must switch on with the keyword virtual
.
Virtual calls from Constructors | |
---|---|
Because " |
Virtual Destructors | |
---|---|
In general, if a class has one or more |
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |