Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Friday, January 11, 2008

Design and Implementation Idea of POLYMORPHISM in C [ non - object oriented language] Part I

Here we were required to design a polymorphic container (say, a stack) using the features provided by a procedural/modular programming language supporting ADT implementation. (Do not use polymorphic features of the language, and do not use a kind of tag i.e. any sort of type field).


Design document and Implementation provided in subsequent posts.


xoxo
aL

Sunday, August 26, 2007

polymorphism in C

Even polymorphism can by implemented in C. Polymorphism in Java and C++ is implemented by associating with each instance a table of functions. Polymorphic method names are converted to indexes into this function table. When a method call on a particular instance is executed, the function table for that instance (actually these tables are shared among the instances of a class), used to find the proper function to execute.

Since C has the ability to store pointers for functions as a basic data type, and the ability to calla function from such a function pointer. It is straightforward (though not always fun) to explicitly duplicate the function table mechanism and implement polymorphisms by hand.

Al