Real-life example of dynamic polymorphism and operator overloading

This was asked in an interview as I mentioned that OOPS is my favorite subject.

Can someone give real life examples for dynamic polymorphism and operator overloading?

I could only explain from the point of view of coding, for example, to calculate areas of different shapes (virtual functions + redefine) and add complex numbers or string concatenation (operator overloading).

+4
source share
5 answers

polymorphism.

We have cars, right. Think of an abstract car. Every car can accelerate. That would be a polymorphic function. Thus, in each (well, in most cases, we do not consider exotic material) you need to press the pedal to accelerate. However, what happens after clicking is different for different cars (read: implementation is defined).

Operator overload.

You have complicated numbers. You can add, subtract, divide (etc.) them, as you can do it with normal numbers. However, how you do it is completely different.

upd: did not see that you mentioned complex numbers in the question ((I will think about the best.

upd2: ok, you can think about the cooking process. When you cook, you need to mix some ingredients. You can add them (for example, put them on one plate), divide them (cut), propagate them (for example, mix drinks) and so on. I think that would move these operators: P

+8
source

std::cout is one of the poster children for operator overloading. While the choice of the actual operator ... is dubious ..., extensibility << massive compared to everything that came before it, and almost everything that came after it, too. Also, consider all the Matrix and Vector classes, iterators, function objects, all these classes with < overloaded so that they can be the map key or sorted as std::string .

Polymorphism is quite simple. Think about what life will be like if you did not have parametric polymorphism in the form of patterns. Holy shit, it would be so bad. Do I repeat this container code for a new type, which is a terribly bad idea, or can I go with this garbage void*, size , which cannot handle complex types and is completely unsafe? An interface like qsort is not applicable either. Parametric polymorphism allows both code reuse and type safety.

Or dynamic polymorphism. std::function cannot work without virtual functions and inheritance, and this is a pretty damn important thing. There are also built-in inheritance-based optimizations. This is useful when you need to handle different types interchangeably at runtime. In addition, there are many things that are effective dynamic polymorphisms, even if they are technically not. For example, every function that you call from the Windows API goes through a table of function pointers set by the dynamic linker. They are polymorphic with any implementation of those functions that correspond to the same interface. What would life be like without the operating system APIs? Unsuitable.

+5
source

I have an idea regarding dynamic polymorphism:

Suppose we have a Car class with several subtypes of Maruti-800 , Indica , Zen , Inova .

Suppose in a racing game we have to stop a car that reaches its final destination. At the moment, we do not know what type of car we have (be it Maruti-800 or Indica or any other type of car). So I have to click the break above the superclass (i.e. Car ) link, causing the Break action on Car to be executed at runtime.

This is a great example of dynamic polymorphism.

+1
source

Dynamic polymorphism: Sports mode on a car. When we switch it to sport mode, we still use the same accelerator pedal, applying the same pressure, but the automatic transmissions change at much higher speeds for us.

Operator Overload: Mobile Phone. Depending on which menu you open, buttons do different things. D in the game forward and in sms prints the character d.

0
source

an example of operator overloading is in the iostream <<statement or another example will be when "make" functors

dynamic polymorphism basically redefines the virtual methods of the parent, it allows you to "program on the interface" (pure virtual functions). this is contrary to static polymorphism, which forces you to use a specific type to use the "overridden" method. here is a good example of the difference.

0
source

All Articles