Can someone tell me about the benefits of polymorphism in C++?

I understand how it works, but I don't know what makes it useful.You still have to define all the separate functions, you still need to create an instance of each object, so why not just call the function from that object?I don't understand why taking this extra step is beneficial.

They do the same thing.I can't tell which one uses more memory and confusion.

Both examples do the same thing, but in different ways.The first example uses static binding while the second uses dynamic binding.

The decision as to which function should be called is made at run-time depending on the type of object pointed by the Base class pointer.

The objects of the concrete Shape classes Line, Triangle and so on are stored in an array of pointers.

If you need to add another concrete shape class say Rhombus, the calling code doesn't have to change much because it refers to all concrete shapes with a pointer to Base class Shape.The Base class pointer needs to point to the new concrete class.

The method to call will be decided at run-time depending on what object the base class pointer points to.

The famous SOLID design principles can be applied to the above example.

You want someone to show up for work.You don't know if they need a car, a bus, or a walk.You just want them to show up.You just tell them to show up and do their job.Without polymorphism, you have to figure out how to get them to work.

Some people take a Segway to work.Without polymorphism, every piece of code that tells someone to come to work has to learn a new way to get there and how to tell them to do it.If you put that code in one place, in the implementation of the Segway-rider, all the code that tells people to go to work tells them to take their Segways, even though it has no idea that this is what it's doing.

There are a lot of real-world programming analogies.You need to tell someone that there's a problem.They might prefer email or an instant message.Maybe it's an email.It is possible to add a new notification mechanism without changing any of the code used in it.

If you have a list of objects which share a common ancestor, you can do something with them.I learned the concept from using shapes and the draw method.They all have the same shape and can all be drawn.Your example doesn't do anything useful.

The.NET Stream class is an example of useful polymorphism.There are many implementations such as "FileStream" and "MemoryStream".Any of the other stream types can be used with little or no modification.

There are many examples of nice uses of polymorphism.An example is a class that represents GUI.The base class would have something.

That is a virtual function.All the classes that inherit the Base will need to implement it.All the widgets in a GUI need to draw themselves, right?You need a base class with all of the functions that are common for all GUI widgets to be defined as pure virtuals because in any child you will do that.

You don't need to worry about checking what kind of widget you are drawing in your code.The object and not you are responsible for knowing how to draw it.You can do that in your main loop.

The above would draw all thewidgets no matter what type they are.

I might have an abstract class hierarchy like this.I can derive from car to implement concrete types.Common code is implemented in the abstract base classes.I may have built some other code with the car in mind.I can pass my cars to this client code without changing them.

IInternalCombustionEngine and a class coupled with this could be an interface, so stay with me.I can implement this interface on classes.E.G.

I can now state that FordCortina is a vehicle, a car, and also a passenger vehicle.This is very powerful.

The poly is more than one.Polymorphism is not relevant if there is more than one derived function.

I have two functions that are derived.The mode variable is used to pick one of them.The function doesn't know which one was selected.The correct version of function is called.

Most of your code doesn't need to know which class it's derived from.There is a single point in the code where the specific selection of which class to instantiate can be found.The code is cleaner and easier to maintain.

Polymorphism is one of the principles.There are several behavior that you can choose with polymorphism.If you have more implementation in your sample, you can choose one by parameters in the runtime.There is help for decelerating layers of application.If you use this structers in your sample of third part, you will see the Parent interface only and not the implementation of it in the runtime.Dependency Injection pattern can be seen for better desing.

There is one more point to be added.It is necessary to implement run-time plug-ins.At run-time, it is possible to add function to a program.The derived classes can be used as shared object libraries.If a new shared object appears, the run time system will link it in and start to call it.This can be done in other languages.

The educate method is used in my school class.Only people who can learn can use this method.They have different learning styles.Someone mugs it up, someone grasps it, etc.

I have boys, girls, dogs, and cats around the school.I would have to write different methods for different objects if the school wanted to educate them.

The different people implement the interface.The School class doesn't have to worry about what it learns.

I wrote the cats and dogs because they might want to go to a different school.They can be educated if it is a certain type of school.

Related Posts:

  1. Concrete Calculator - How much concrete do I need?
  2. How much does a concrete slab cost per foot?
  3. PDF Average Weight (psf) of Completed Walls using Concrete. Material WeightsDensity Of Concrete - How Much Does Concrete weigh?Nominal Weight of Standard Concrete...
  4. What is concrete class in C++ with Example program?