Run-Time Polymorphism


Previously you learned that polymorphism is used by programmers to define a standard interface that enables application programmers to invoke common behaviors by calling the same method name . You also learned that late binding provides flexibility for a program to respond to events occurring while the program executes.

Run-time polymorphism is a way for a programmer to take advantage of the benefits offered by polymorphism and late binding. Run-time polymorphism uses virtual functions to create a standard interface and to call the underlying functions. Those function definitions are bound to function calls during run time.

The term virtual function is one of those computer terms that is baffling the first few times you hear it used. Let s pick apart the term and review an example to clear up any confusion you might have.

Virtual means that something appears to be real, but isn t real. For example, a flight simulator lets you fly a virtual airplane. The airplane isn t really there, but you have the feeling you are flying a real airplane.

In the case of a virtual function, the computer is tricked into thinking a function is defined, but the function doesn t have to be defined at that moment. Instead, the virtual function can be a placeholder for the real function. The real function is defined when the program is running.

Run-Time Polymorphism in Action

Examining an example is the best way to understand how run-time polymorphism works. The following example is very similar to the previous C++ example. Both programs write and display information about a student. The previous example is a C++ program that uses overloaded methods to implement polymorphism. The following example is a C++ program that uses virtual functions to implement polymorphism.

Three classes are defined in this example: Student , UndergradStudent , and GraduateStudent . The Student class is the base class that is inherited by the other classes in the program. A base class is a class that is inherited by another class, which is called a derived class.

The Student class defines an attribute called m_ID that is used to store the student s ID. It also defines a constructor that receives the student ID in the argument list and assigns the student ID to the m_ID attributes. The constructor is called whenever an instance of the class is declared. The last two statements in the Student class definition define two virtual functions: Display() and Write() .

The declaration of a virtual function in C++ consists of the keyword virtual , the function signature (name and argument list), and a return value. In Java, methods are virtual by default, unless you use the final keyword. Virtual functions may be actual functions or merely placeholders for real functions that derived classes must provide.

If you define a virtual function without a body, that means the derived class must provide it (it has no choice, and the program will not compile otherwise ). Classes with such functions are called abstract classes , because they aren t complete classes and are more a guideline for creating actual classes. (For example, an abstract class might state you must create the Display() method. ) In C++, you can create a virtual function without a body by appending =0 after its signature (also known as a pure virtual function). You use the abstract keyword in Java to create a virtual function without a body.

The UndergradStudent class and GraduateStudent class are practically the same except the Display() function identifies the student as either an undergraduate or graduate student when student information is shown on the screen. Both classes define a Write() function and a Display() function. The Write() function copies student information received in the argument list to attributes of the class. The Display() function displays the contents of those attributes and the student ID attribute in the Student class.

The main() function is where all the action takes place. The first statement declares a pointer that points to an instance of the Student class. The next two statements declare an instance of the UndergradStudent class and the GraduateStudent class. Notice that the student ID is passed to the constructor of each instance. Each constructor calls the constructor of the Student class, which assigns the student ID to the m_ID attribute.

Run-time polymorphism is implemented in the next three statements, beginning with the assignment of the address of uStudent to the pointer p . The pointer is then used with the pointer-to-member (->) operator to point to the function Write() and then Display() . After attributes of the undergraduate student are displayed, the program assigns the address of gStudent to the pointer p and then proceeds to call the Write() and Display() functions.

Here is the output of the next program:

Undergraduate Student: 10 Bob Smith 1

Graduate Student: 23 Mary Jones 1

 #include <iostream> 
#include <string>
using namespace std;
class Student {
protected:
int m_ID;
public:
Student (int i) {
m_ID = i;
}
virtual void display() = 0;
virtual void write( int, char[], char[]) = 0;
};
class UndergradStudent : public Student {
protected:
int m_Graduation;
char m_First[80];
char m_Last[80];
public:
UndergradStudent(int i) : Student (i) { }
void write( int Grad, char Fname[], char Lname[]) {
m_Graduation = Grad;
strcpy(m_First,Fname);
strcpy(m_Last, Lname);
}
void display() {
cout << "Undergraduate Student: "<< m_ID << " " << m_First
<<" " << m_Last << " " << m_Graduation<< endl;
}
};
class GraduateStudent : public Student {
protected:
int m_Graduation;
char m_First[80];
char m_Last[80];
public:
GraduateStudent(int i) : Student(i) { }
void write( int Grad, char Fname[], char Lname[]) {
m_Graduation = Grad;
strcpy(m_First,Fname);
strcpy(m_Last, Lname);
}
void display() {
cout << "Graduate Student: "<< m_ID << " " << m_First <<" " <<
m_Last << " " << m_Graduation<< endl;
}
};
int main()
{
Student * p;
UndergradStudent uStudent(10);
GraduateStudent gStudent(23);
p = &uStudent;
p->write(1,"Bob","Smith") ;
p->display();
p = &gStudent;
p->write(1,"Mary","Jones") ;
p->display();
return 0;
}



OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net