affiliate marketing
Showing posts with label Abstract Data Type. Show all posts
Showing posts with label Abstract Data Type. Show all posts

Monday, 12 December 2011

4. Abstract Data Type


4.1. Alternative Implementations Of Fractions
Returning to our example of the fraction data type, how might we actually implement this datatype in C?
Implementation 1
typedef struct { int numerator,denominator; } fraction;

main()
{
  fraction f;
  f.numerator   = 1;
  f.denominator = 2;
  ...
}
Implementation 2
#define numerator   0
#define denominator 1
typedef int fraction[2];

main()
{
  fraction f;
  f[numerator]   = 1;
  f[denominator] = 2;
  ...
}
These are just 2 of many different possibilities. Obviously, these differences are in some sense extremely trivial - they do not affect the domain of values or meaning of the operations of fractions.