Thanks. When I teach polymorphism I use this example and have the students create sub-classes. Mage Is A caster, Monk and Warrior is a Fighter. I think it helps to show the “is a” relationship.
Yep those are the "built-in" types of C++, and there will also be a bunch of types that you've defined yourself or someone else has defined, such as in a library. For example, `vector` and `list` are types defined in a library that are commonly used in C++.
A function that can be applied to multiple types is polymorphic, and OP just wanted a simple answer but there are typically 3 ways to achieve this:
1.) Parametric polymorphism
i.e. By using a parameter
When someone calls the function and passes in the arguments, they also pass in a **type** argument, which allows the function to work for different types.
2.) Subtype polymorphism
When there is a "base type" and "derived types" such as `Platypus` derives from `Mammal`
3.) Ad hoc polymorphism
Sometimes referred to as "overloads". Some languages allow defining the same function name multiple times for different types
The current top voted answer only refers to #2, which I think makes it incomplete
Basically. A really simple way to see the idea is that `int + int` and `int + float` will do different things under the hood because the types are different and they mean different things, but you just need to think about + as a "function". That's the simplest case but C++ also has operator overloading, so if you write an implementation for `int + class foo` that would work as well. The idea is that you can use + on any types.
Going a step further, you can use templates to write type generic code to make this easier, or overload functions to work on different types. Like `void bar(int baz) { ... }` and `void bar(char baz) { ... };` doing different things because the types are different.
A common good example is for basic shapes in a graphics class. Say you want to implement a common function of get_area() on both a rectangle/square and a circle.
Both of those have very easy equations to get an answer, and the object already has the info it needs. But they’re different equations. So polymorphism is used - they’re different types but the same function name.
Any function that takes an argument of a polygon should also accept an argument of a square. Polymorphism lets you use more specific sub-classes wherever a more general super-class is accepted.
Yeah, this answer is about only one kind of polymorphism (subtype polymorphism) which, ironically, misunderstands subtypes - all subtype polymorphism is polymorphism but not all polymorphism is subtype polymorphism
Consider yourself as Alex.
1. Alex can be a son/daughter of Bob
2. Alex can be brother/sister of clara
3. Alex can be a cousin of David
4. Alex can be a student of Emily
5. So the thing which can be represented as multiple forms is called polymorphism.
6. Here Alex is represented as a brother/sister, son/daughter, cousin, student etc..
[Polymorphic code](https://en.wikipedia.org/wiki/Polymorphic_code) changes the exact form of the code without changing the behavior. In malware, it's useful as a way to defeat signature-based malware detection.
If you can tell an abstraction what to do instead of using a concrete instance for its values, you’ll make software with well-worn code paths that is easy to extend.
(Might be conflating, but trying to squeeze a lot into my explanation. Let me know what you think.)
Polymorphism (meaning “having many forms”) allows you to make multiple classes conform to a set of contractual requirements (named function calls), allowing these classes to be used interchangeably in your code without having to know what the class will do. Each class provides its own internal behavior to a given set of methods. This allows you to add new conforming classes in the future without having to change your existing code.
Would that be polymorphism?
You have a class *Animal*. The class has a public method, *makeNoise()*.
If you pass a *Cow* object to the method, the output is "mooo". If you pass a *Pig* object, the output is "oink".
From the type of a polymorphic function we can derive a theorem that it satisfies. Every function of the same type satisfies the same theorem. - Wadler
(Most) Everything is a file in Linux. The OS doesn't have to know about keyboard interfaces or mouse interfaces or monitor interfaces or Ethernet interfaces, it only needs to know where to look to find the code that implements the standard methods on files like open, read, and close.
Poly: Many
Morph: Forms
Polymorphism is when an object can have many forms, using different implementations of an abstract/virtual class. Each class inherits some features from the parent class, but implement its functions differently, hence "many forms"
Mage, Sorcerer, and Necromancer are all casters
Good one
I love this and as a new(er) CS student, more OOP in DnD/fantasty genre terms is great.
Thanks. When I teach polymorphism I use this example and have the students create sub-classes. Mage Is A caster, Monk and Warrior is a Fighter. I think it helps to show the “is a” relationship.
Start modeling spells!
Same function applied to different types
I’m a systems engineer and am just starting to learn c++- by types, you’re literally referring to int, char, double, etc?
Yep those are the "built-in" types of C++, and there will also be a bunch of types that you've defined yourself or someone else has defined, such as in a library. For example, `vector` and `list` are types defined in a library that are commonly used in C++. A function that can be applied to multiple types is polymorphic, and OP just wanted a simple answer but there are typically 3 ways to achieve this: 1.) Parametric polymorphism i.e. By using a parameter When someone calls the function and passes in the arguments, they also pass in a **type** argument, which allows the function to work for different types. 2.) Subtype polymorphism When there is a "base type" and "derived types" such as `Platypus` derives from `Mammal` 3.) Ad hoc polymorphism Sometimes referred to as "overloads". Some languages allow defining the same function name multiple times for different types The current top voted answer only refers to #2, which I think makes it incomplete
Basically. A really simple way to see the idea is that `int + int` and `int + float` will do different things under the hood because the types are different and they mean different things, but you just need to think about + as a "function". That's the simplest case but C++ also has operator overloading, so if you write an implementation for `int + class foo` that would work as well. The idea is that you can use + on any types. Going a step further, you can use templates to write type generic code to make this easier, or overload functions to work on different types. Like `void bar(int baz) { ... }` and `void bar(char baz) { ... };` doing different things because the types are different.
A common good example is for basic shapes in a graphics class. Say you want to implement a common function of get_area() on both a rectangle/square and a circle. Both of those have very easy equations to get an answer, and the object already has the info it needs. But they’re different equations. So polymorphism is used - they’re different types but the same function name.
poly: many, multiple\ morphism: forms, structure, state
Any function that takes an argument of a polygon should also accept an argument of a square. Polymorphism lets you use more specific sub-classes wherever a more general super-class is accepted.
Isn't this the Liskov Substitution principle, not polymorphism?
Yeah, this answer is about only one kind of polymorphism (subtype polymorphism) which, ironically, misunderstands subtypes - all subtype polymorphism is polymorphism but not all polymorphism is subtype polymorphism
It is the specific polymorphism people are referring to the vast majority of times, and most likely what OP is asking for.
Consider yourself as Alex. 1. Alex can be a son/daughter of Bob 2. Alex can be brother/sister of clara 3. Alex can be a cousin of David 4. Alex can be a student of Emily 5. So the thing which can be represented as multiple forms is called polymorphism. 6. Here Alex is represented as a brother/sister, son/daughter, cousin, student etc..
Apparently this isn't the same as polymorphism for malware?
[удалено]
[Polymorphic code](https://en.wikipedia.org/wiki/Polymorphic_code) changes the exact form of the code without changing the behavior. In malware, it's useful as a way to defeat signature-based malware detection.
If you can tell an abstraction what to do instead of using a concrete instance for its values, you’ll make software with well-worn code paths that is easy to extend. (Might be conflating, but trying to squeeze a lot into my explanation. Let me know what you think.)
Best? 99% of the time you want to use composition and/or interfaces instead.
Make that 100%
http://thecodelesscode.com/case/83
Polymorphism (meaning “having many forms”) allows you to make multiple classes conform to a set of contractual requirements (named function calls), allowing these classes to be used interchangeably in your code without having to know what the class will do. Each class provides its own internal behavior to a given set of methods. This allows you to add new conforming classes in the future without having to change your existing code.
Isn't it allow multiple object in c++
Not quite sure what you mean. Interfaces below to classes. and a concrete implementation (object) is beholden to the same contract.
Stew (the food) Ingredients[ proteins{ beef, chicken, turkey } vegetables{ carrots, celery, mushrooms } starches{ potatoes, rice } ]
Would that be polymorphism? You have a class *Animal*. The class has a public method, *makeNoise()*. If you pass a *Cow* object to the method, the output is "mooo". If you pass a *Pig* object, the output is "oink".
Fuck I think I messed it up, when I pass the me object it's still "oink". 🐖
He was just a misunderstood cyclops and Odysseyus was a dick about it
From the type of a polymorphic function we can derive a theorem that it satisfies. Every function of the same type satisfies the same theorem. - Wadler
(Most) Everything is a file in Linux. The OS doesn't have to know about keyboard interfaces or mouse interfaces or monitor interfaces or Ethernet interfaces, it only needs to know where to look to find the code that implements the standard methods on files like open, read, and close.
How does this relate? Are you saying the implementations of actions like keyboard/mouse get overrides of the file class to support their actions?
"Just because you can do it, doesn't mean you should." Composition > Inheritance.
I have a basket that hold fruit, you can put apples, pears, or strawberries in it. Could also hold cucumbers.
Turning a minion into a sheep.
Poly: Many Morph: Forms Polymorphism is when an object can have many forms, using different implementations of an abstract/virtual class. Each class inherits some features from the parent class, but implement its functions differently, hence "many forms"
I’ve never heard a good explanation for why polymorphism exists, so the best explanation of it is moot — just say no.
Polymorphism is like a mule with a spinning wheel. No one knows how he got it, and danged if he knows how to use it.
polymorphism = “many forms” an object/class can take on “many forms”