Thursday, July 9, 2009

What is templates and polymorphism in c++?

A template is a mechanism for specifying the type of data to be handled by a class (or function) when we declare an object of that function (or when a call is made to that function).





Polymorphism is the ability to respond in different ways based on the context in which the the call is made.There are different kinds of polymorphism.





In c++ polymorphism is acheived using overloading and overridding.





you can go through some books on C++ to understand it more clearly.

What is templates and polymorphism in c++?
polymorphism is the abiliy for an object to take on "many faces" (poly-morphism). An example:





create a base class called Automobile with a Drive() method.


create an inherited class called Truck. Truck implements the Drive() method and can in some cases be cast as the base Automobile.





Not all polymorphism is inheritence based. You can also define an "interface" with defined methods %26amp; properties. You can create an IAutomobile interface with one method Drive(). Now create a class Trcuk which does not inherit from anything but does implement the IAutomobile interface.
Reply:A detailed tutorial for Templates %26amp; Polymorphism concepts is available on following pages:





Templates: http://www.is.pku.edu.cn/~qzy/cpp/vc-stl...





Polymorphism: http://en.wikipedia.org/wiki/Polymorphis...


No comments:

Post a Comment