5

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

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
h4ck3d
  • 6,134
  • 15
  • 51
  • 74
  • 3
    You create a `Fraction` class that has overloaded operators for `+`,`-`,`*`, and `/`. – Hunter McMillen Aug 14 '12 at 13:11
  • That is a good time to provide an example that is related to what the company you are interviewing for do. Or some project you have worked in the past, in this case you can bring experiences as of what issues you encountered and how you dealt with them. – David Rodríguez - dribeas Aug 14 '12 at 13:53

5 Answers5

8

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

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • a car wouldn't change it's type every time you start the engine, so i would rather use something statically linked in this case – Gir Aug 14 '12 at 13:17
  • 2
    @Gir, you can have a hybrid car that can start either on the electric engine, or on the normal one depending on circumstances :P – SingerOfTheFall Aug 14 '12 at 13:18
5

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.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 1
    I sometimes wonder whether this `os << x << y << z` would ever have existed had we just had proper variadics in time. The extensibility could have been equivalent, while not introducing funny syntax. Also, it would have slightly more natural to support reverting streams to their original states after doing some operations that manipulated flags, locale options etc.... Hmmm – sehe Aug 18 '12 at 01:29
  • Even with variadic templates, you can't replace `<<` chaining. – Puppy Aug 18 '12 at 02:41
  • 2
    What do you mean? You can still implement `os.writeln("Hello ", 42, " world").writeln("and chain if you insist") << "even mixed if you're obtuse" << std::endl;` – sehe Aug 18 '12 at 11:00
1

I have an idea regarding dynamic polymorphism:

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

Suppose in a car racing game we have to stop a car that reaches its final destination. At this time we don't know which type of car we have (whether it is Maruti-800 or Indica or any other type of car). So I have to press the break on the super class reference (i.e. Car) by calling the action Break on Car at runtime.

This is the great example of the dynamic polymorphism.

Ben Reich
  • 16,222
  • 2
  • 38
  • 59
0

Dynamic Polymorphism: Sports mode on automatic car. When we switch it to sports mode, we still use the same accelerator pedal applying the same pressure, but the automatic gears are changed at much higher revs for us.

Operator Overloading: Mobile phone. Depending on which menu you have open, buttons do different things. D in a game is forwards and in a sms it prints the character d.

Science_Fiction
  • 3,403
  • 23
  • 27
0

an example of operator overloading is in iostream operator<< or another example would be when 'making' functors

dynamic polymorphism is basically overriding virtual methods of a parent, it allows you to 'program to an interface' (pure virtual functions). this is opposed to static polymorphism which forces you to use a concrete type in order to use the 'overridden' method. here is a nice example of the difference.

Community
  • 1
  • 1
T I
  • 9,785
  • 4
  • 29
  • 51
  • I need a real-life / practical example . Even I know in technical terms what these concepts are. – h4ck3d Aug 14 '12 at 18:10
  • [QWidget](http://qt.gitorious.org/qt/qt/blobs/529b30b96435491fc6994515862f44d24efc754f/src/gui/kernel/qwidget.h) `QWidget *foo = 0; foo = x == y ? new MyBtn : new MyLbl;` – T I Aug 14 '12 at 20:08
  • Static polymorphism doesn't force the use of a particular type. That would be a *monomorphism* instead. Static polymorphic also allows you to program to interfaces, which is exactly the reason people are trying to formalize those kinds of interfaces into the language as "concepts". – R. Martinho Fernandes Aug 18 '12 at 01:31