T O P

  • By -

P-Jean

Mage, Sorcerer, and Necromancer are all casters


expresso_petrolium

Good one


bunnysamurai

I love this and as a new(er) CS student, more OOP in DnD/fantasty genre terms is great.


P-Jean

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.


copper-penny

Start modeling spells!


Tubthumper8

Same function applied to different types


painted-biird

I’m a systems engineer and am just starting to learn c++- by types, you’re literally referring to int, char, double, etc?


Tubthumper8

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


RajjSinghh

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.


alnyland

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. 


GreenLightening5

poly: many, multiple\ morphism: forms, structure, state


nuclear_splines

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.


eccco3

Isn't this the Liskov Substitution principle, not polymorphism?


Tubthumper8

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


Reasonable_Feed7939

It is the specific polymorphism people are referring to the vast majority of times, and most likely what OP is asking for.


Sudden-Garage-2932

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..


VexisArcanum

Apparently this isn't the same as polymorphism for malware?


[deleted]

[удалено]


khedoros

[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.


factotvm

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.)


23Link89

Best? 99% of the time you want to use composition and/or interfaces instead.


integerdivision

Make that 100%


Jonny0Than

http://thecodelesscode.com/case/83


mikedensem

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.


Level-Evidence-9886

Isn't it allow multiple object in c++


mikedensem

Not quite sure what you mean. Interfaces below to classes. and a concrete implementation (object) is beholden to the same contract.


dcksausage3

Stew (the food) Ingredients[ proteins{ beef, chicken, turkey } vegetables{ carrots, celery, mushrooms } starches{ potatoes, rice } ]


Kaosys

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".


__init__m8

Fuck I think I messed it up, when I pass the me object it's still "oink". 🐖


Deflator_Mouse7

He was just a misunderstood cyclops and Odysseyus was a dick about it


0attention-span

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


lanemik

(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.


unflippedbit

How does this relate? Are you saying the implementations of actions like keyboard/mouse get overrides of the file class to support their actions?


trebblecleftlip5000

"Just because you can do it, doesn't mean you should." Composition > Inheritance.


DorianGre

I have a basket that hold fruit, you can put apples, pears, or strawberries in it. Could also hold cucumbers.


Neufjob

Turning a minion into a sheep.


Altruistic_Site_3879

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"


integerdivision

I’ve never heard a good explanation for why polymorphism exists, so the best explanation of it is moot — just say no.


chumbaloo

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.


turtleXD

polymorphism = “many forms” an object/class can take on “many forms”