next up previous
Next: From Modula-2 ADT Up: From Modula-2 ADT's to Previous: Some notes

Examples of using ADT's

The biggest difference between Modula-2 ADT's and C++ classes is that in Modula-2, the instance of the ADT that's being processed is passed as parameter to some function, while in C++ the functions work on the member variables of the object of which the function is being called. Modula-2:

MODULE StackTest;

FROM SimpleIO IMPORT
    WriteInteger, WriteString, WriteLn;

FROM IntStacks IMPORT
    IntStack,
    Create, Terminate,
    Push, Pop, Empty;

VAR
    MyStack: IntStack;
    Elt: INTEGER;

BEGIN
    IF Create(MyStack) THEN
        Push(MyStack, 27);
        Push(MyStack, 3);
        Pop(MyStack, Elt);
        WriteInteger(Elt, 4);
        WriteLn;
        Pop(MyStack, Elt);
        WriteInteger(Elt, 4);
        WriteLn;
        IF Empty(MyStack) THEN
            WriteString("Stack is now empty.");
            WriteLn;
        END;
        Terminate(MyStack)
    END
END StackTest.

C++:

#include "intstack.h"
#include <iostream.h>

int main()
{
    intstack mystack;           // automatic creation
    int elt;

    mystack.push(27);
    mystack.push(3);
    mystack.pop(elt);
    cout << elt << endl;
    mystack.pop(elt);
    cout << elt << endl;
    if (mystack.empty())
        cout << "Stack is now empty." << endl;

    return 0;
                                // mystack will be automatically destroyed
}



SE Praktikum
Tue Aug 13 13:10:22 MET DST 1996