ADT Computing Paradigm

Lecture 6-9

In the statements above except for pa (which is only text substituted to 100) rest all consume memory and hence they are called objects/variables.

         MonkeyStruct *mnkS = (MonkeyStruct*)malloc(sizeof(MonkeyStruct));

         void *vS = (void *)mnkS;

         ManStruct * manS = (ManStruct *)vS;

Here we have 2 objects mnkS and manS. Assuming both have same structural semantics, then monkey will assume behaviour of a man.

                        #define Size 100         

int array[Size];

int top = 0;

void push(int a) { array[top++] = a:}

int pop () { return array[--top]; }

int Is_Empty () { return (top == 0) ? 1 : 0; }

int Is_Full () { return (top == Size – 1) ? 1 : 0; }

Below we discuss the issues in the above implementation and improve the code to make it better reusable.

                void push(int a) {

if Is_full() NO_PUSH; // pre-condition

array[top++] = a:

Is_Empty = FALSE //post-condition

}

Similarly for pop operation check and update the empty condition.

                        typedef struct stag{

int * v;

int MaxSize;

int * top;

} Stack;

Creat_initialize_Stack(Stack *S, int MaxSize){

v = (int *) calloc ( sizeof (int), MaxSize)};

Struct Point2D {float x, y};

Struct Point2D {float r, theta};

        Point2D {

// A 2-d point exists somewhere in the plane, …

float x;

float y;

float r;

float theta;

// ... can be created, …

Point(); // new point at (0,0)

Point(x, y); // new point with Cartesian co-ord

Point(r, theta); // new point with Polar co-ord

// ... can be moved, …

void translate(float delta_x, float delta_y);

void scaleAndRotate(float delta_r, float delta_theta);

// ... can be accessed,

float get_x (Point P); float get_y (Point P);

float get_r (Point P); float get_theta (Point P);

}

                                        e.g. define stack for int, float, double etc.

        template<class In,class Out> void copy(In from, In too_far, Out to)

        {

                while(from != too_far)

                {

                        *to = *from;

                        ++to;

                        ++from;

}

}

Here In and Out are the template parameters. to and from are current iterator pointers. copy function copies element pointed by current iterator pointers from one structure to another and increment the iterator to point the next element.This algorithm is independent of the underlying structure. ++ is implemented as operator overloading as depending on the structure it will perform increment operation.

C++ support operator overloading. Java supports inbuilt operator overloading but does not allow programmer to implement it.

complex ac[200];

void g(vector<complex>& vc, list<complex>& lc)

{

                        copy(&ac[0],&ac[200], lc.begin());        //copy from array to list of complex

                        copy(lc.begin(),lc.end,vc,begin());        //copy from list to vector

}

Here first call to copy copies the complex numbers form array of complex numbers ot list of complex numbers. Second call copies the complex numbers from list of complex numbers to vector of complex numbers. Thus the code is transparent to the underlying structure of the container.