Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real life example of Dynamic Polymorphism & Operator Overloading

Tags:

c++

oop

This was asked to me in an Interview as I mentioned OOPS is my favorite subject.

Can anyone give some real life examples for Dynamic Polymorphism and Operator Overloading ?

I could only explain in terms of coding , like calculating areas of different shapes (virtual functions + overriding ) and adding of complex numbers or concatenation of strings (operator overloading) .

like image 903
h4ck3d Avatar asked Aug 14 '12 13:08

h4ck3d


2 Answers

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

Polymorphism is pretty simple. Think what life would be like if you did not have parametric polymorphism in the form of templates. Holy shit, it would be so bad. Do I either repeat this container's code for a new type, which is a hideously bad idea, or do I go with that void*, size rubbish that can't handle complex types and is completely type unsafe? The interface of stuff like qsort is just unusable too. Parametric polymorphism allows both code re-use and type-safety.

Or dynamic polymorphism. std::function couldn't work without virtual functions and inheritance, and it's a pretty damn important thing. There are also embedded-end optimizations based on inheritance. It's useful whenever you need to treat different types interchangably at run-time. In addition, there are many things which are effectively dynamic polymorphism, even if they are technically not. For example, every function you call from the Windows API goes through a table of function pointers fixed up by the dynamic linker. They are polymorphic with any implementation of those functions that conforms to the same interface. What would life be like without operating system APIs? Unlivable.

like image 44
Puppy Avatar answered Sep 28 '22 15:09

Puppy


Polymorphism.

We have cars, right. Think of an abstract car. Every car can accelerate. That would be a polymorphic function. So in each (well, most of, we don't count exotic stuff) car you need to press down the pedal to accelerate. However, what happens after you press it is different for different cars (read: implementation defined).

Operator overloading.

You have complex numbers. You can add, subtract, divide (etc) them as you can do it with normal numbers. However, the way how you do it is quite different.

upd: didn't see you've mentioned complex numbers in the question(( I'll think of a better one.

upd2: well you can think of the process of cooking. When you are cooking, you need to mix some ingredients. You can add them (like put them in the same plate), divide them (cut), multiply them (like mixing drinks) and so on. I guess that would go for overloading those operators :P

like image 105
SingerOfTheFall Avatar answered Sep 28 '22 15:09

SingerOfTheFall