You are viewing an obsolete version of the DU website which is no longer supported by the Administrators. Visit The New DU.
Democratic Underground Latest Greatest Lobby Journals Search Options Help Login
Google

Reply #8: Not quite... [View All]

Printer-friendly format Printer-friendly format
Printer-friendly format Email this thread to a friend
Printer-friendly format Bookmark this thread
This topic is archived.
Home » Discuss » The DU Lounge Donate to DU
regnaD kciN Donating Member (1000+ posts) Send PM | Profile | Ignore Wed Jan-14-04 02:10 PM
Response to Reply #5
8. Not quite...
The example you mentioned actually refers to operator overloading, not polymorphism.

Polymorphism refers to the use of virtual functions in classes derived from a master class.

Example: You have a base class titled Class1. You derive two new classes, titled Class2 and Class3, from it. Each of the three classes has a function titled Update().

You declare an array of pointers to Class1 objects, titled pClass[]. The thing is, some of the objects whose pointers you add to the array are Class2 and Class3 objects. (Since they're derived from Class1, you can do this.) Now, you create a loop like this:

for (int x = 0; x < ARRAY_SIZE; x++)
{
pClass(x)->Update();
// those should be square brackets around the x, but the forum software won't accept it!
}

What's going to get called?

If, when creating the base class, you declare Update() as a standard function, that loop will call the Class1's Update() function for each element in the array. If, however, you declare Update() a virtual function (i.e., you implement polymorphism), when the pointer is actually to a Class2 object, it will call Class2's Update() function instead. Same with Class3. The only time Class1's Update() function will be called is when the object itself is an instance of Class1, not Class2 or Class3.

(The previous time I was looking for work, when I had less experience with C++ than I do now, the above was a question in a phone interview I had with Microsoft. I didn't get it right, and wound up not being considered for the job because of it -- therefore, I made a point of memorizing it from then on. ;-) )
Printer Friendly | Permalink |  | Top
 

Home » Discuss » The DU Lounge Donate to DU

Powered by DCForum+ Version 1.1 Copyright 1997-2002 DCScripts.com
Software has been extensively modified by the DU administrators


Important Notices: By participating on this discussion board, visitors agree to abide by the rules outlined on our Rules page. Messages posted on the Democratic Underground Discussion Forums are the opinions of the individuals who post them, and do not necessarily represent the opinions of Democratic Underground, LLC.

Home  |  Discussion Forums  |  Journals |  Store  |  Donate

About DU  |  Contact Us  |  Privacy Policy

Got a message for Democratic Underground? Click here to send us a message.

© 2001 - 2011 Democratic Underground, LLC