C++ | Can't understand Polymorphism
On 4/20/2019 at 4:11 PM, Hi P said:My issue is that I don't understand the point, the purpose, of doing all that when I can simply do something like the following:
So both do the exact same thing, but the Polymorphism thing is just a bit longer....
Could someone please help me understand this?
Thank you very much
What the example is showing, and I think it's a weak example, is polymorphism allows you to cast the class of an object to an ancestor or a "sibling" class.
What the example isn't really explaining is all this does is preserve the method signature (that is the method name, return type, and parameters) throughout the parent class and its descendants. The example is fine if attack method of Entity is fixed for all children classes, but if Hero or Goblin implements attack differently through overriding, then you'll have different results if you call attack from an object casted as Entity or one casted as Hero or Goblin. So basically, while this is helpful to teach what polymorphism is, it doesn't teach you where it could be useful.
As an example of where it could be useful, in GUI programming, you want something that handles events, like pressing a button or changing which radio button got selected. All of these GUI widgets are classes, like Button, RadioButton, Checkbox, etc. It would be a pain in the butt to create a method signature for every GUI widget type for the same events (e.g., onClick(Button button, event e) and onClick(Checkbox checkbox, event e)). Instead what you can do is make sure all of your GUI classes descend from a common ancestor, then construct a method signature that uses this common ancestor (e.g., onClick(WidgetBase widget, event e)). Since the GUI classes are a descendant of this ancestor, you can cast them to that ancestor and use the same method signature regardless of what GUI widget class you're using.
As an example, this is how an event handler is done in C#:
private void KeyButton_Click(object sender, EventArgs e) { Button keyButton = ((Button)sender); string keyLetter = keyButton.Name.Substring(0, 1); keyLetter = plugBoard.GetRewiredLetter(keyLetter); encodeKeyPress(keyLetter); }
This event handler ties into a series of buttons that represent a keyboard of sorts to process a key press. When one of the buttons is pressed, it calls this function, which casts itself as the generic "object" class as the first argument. Then in the event handler itself, we "uncast" the button object back to the Button class and work with it from there.
If polymorphism was not used, the first argument, object sender, would be Button sender instead. Now imagine doing this for every GUI widget you have in this frame work. The benefit to using polymorphism for this, aside from the obvious fewer lines of code you would need, is it's less work if you decide to add or remove a GUI widget. If you added or removed a GUI widget without using this method, you would have to scan through your entire framework to make sure all of the event handler declarations were updated appropriately.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now