10.9 Polymorphism


10.9 Polymorphism

New programmers may find the topic of polymorphism intimidating because it sounds quite complicated; however, this is not the case. Polymorphism simply means that derived classes are "type compatible" with their base (ancestor) classes. To illustrate, let's consider the frog example again.

We have two classes, Frog and FlyingFrog. Frog is the base class and contains the crucial behavior applicable to all types of Frogs. We could easily create a new Frog type called FireFrog, a frog that breathes fire. This FireFrog would be derived from Frog and would also inherit all the behaviors from Frog, just like FlyingFrog. So, in this sense, the FlyingFrog and FireFrog classes contain only that which distinguishes them from standard Frogs.

We decide to create a program that holds an array of Frogs of all kinds, both FlyingFrogs and FireFrogs, and the program needs to make every one of those Frogs eat at 12:00 p.m. It could do this simply by keeping a list of only pointers to Frog classes (not pointers to FlyingFrog or FireFrog). Remember, all Frogs can eat because this behavior is defined in Frog. Frog is the base class of all derived Frog types, and polymorphism says that all derived classes are type compatible with their base classes. This means any derived class (like FlyingFrog) will be compatible with any object that is declared as being any of its ancestor types also (such as Frog). Consider the following code:

      FlyingFrog *FlyFrog = new FlyingFrog();      Frog *Frg1 = FlyFrog;      Frg1->Eat();    //Polymorphism Will Work 

10.9.1 Polymorphism and Virtual Functions

If a base class such as Frog contains virtual functions, it is said to be an abstract base class. Programmers cannot declare instances of abstract base classes; these kinds of classes are designed to be descended from only, and only instances of descendant classes can be declared. Virtual functions take advantage of polymorphism and allow programmers to define a common base class from which many different descendants will spring. The virtual function is like any normal function except it has no function body. It's simply a blank function declaration, like a placeholder. Including such functions in base classes makes the classes abstract. That is, you cannot instantiate any instances of them, and must derive new classes from them.

As an example, let's take the eat function. All Frogs might be able to eat, but the process of eating-the way a Frog may eat-could differ from Frog species to Frog species, such as FlyingFrog to FireFrog. A FlyingFrog might catch its prey and take it home, while a FireFrog may incinerate its prey and eat the remains. Both can eat, but both eat differently. By making the eat method a virtual function, it means the eat method can still be declared as part of Frog, but when this method is called, the actual definition of the function will differ, depending on which descendant class is handling the function. A virtual function can be declared as follows:

      //This class cannot be instantiated      class Frog      {         int Color;         int Height;         int Width;      public:         void Jump();         void Sleep();         virtual void Eat() = 0;         //virtual function         int operator+(Frog param);      //Defines Add Operator         int operator-(Frog param);      //Defines Add Operator      }; 

Note 

Virtual functions are prefixed with the virtual keyword.

Then descendant classes can define each of their variations:

      class FlyingFrog : public Frog      {         public:            void Eat() {std::cout <<"Fly";}      };      class FireFrog : public Frog      {         public:            void Eat() {std::cout <<"Fire";}      }; 

And finally, a program may declare one instance of each class, one of FlyingFrog and one of FireFrog. The properties and methods of Frog, which are inherited by FlyingFrog and FireFrog, can be accessed through standard Frog pointers too.

      FlyingFrog *FlyFrog = new FlyingFrog();      FireFrog *TheFireFrog = new FireFrog();      Frog *Frg1 = FlyFrog;      Frg1->Eat(); //Calls FlyFrog->Eat();      Frog *Frg1 = TheFireFrog;      Frg1->Eat(); //Calls TheFireFrog->Eat(); 




Introduction to Game Programming with C++
Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn

Similar book on Amazon
Beginning C++ Through Game Programming
Beginning C++ Through Game Programming
Beginning Game Programming
Beginning Game Programming
The Ultimate Guide to Video Game Writing and Design
The Ultimate Guide to Video Game Writing and Design

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