Thread: Abstract Data Type

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    Abstract Data Type

    This is a programming idea / style that we are going over in my Pascal class. Basically put you use only a set number of subroutines that actually knows the internal workings of your type (ie a struct (record in pacal), array, linked list, etc). All other subroutines use this set of routines to put data in and get data out.

    The idea behind it is that if you needed to change how the data was being stored you just have to change these routines without having to change the entire program.

    The real life example we were given was that of a TV and it's remote. The user knows how to use the remote to change the channel but has no idea how the TV actually changes the channel. So that if you were to change the TV you wouldn't have to change anything with the user interface (the buttons on the remote).

    Without getting into ADT vs OOP what are your thoughts on this style?

  2. #2
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    This sounds like the "get/set" routines that our Java instructor has us use.

    Example (in C++):
    Code:
    class Television
    {
    private:
      int channel;
    
    public:
      void setChannel(int value) { channel = value; };
      int getChannel() { return channel; };
    };
    And then any modifications to channel are done through those two routines. That way if we want to impose a limit, we don't have to change a lot of code. Is that what you mean?

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I don't know about Pascal, but in C++, it's easy to accomplish (and usually the goal anyway). Just look at the STL. It is possible in C, but not so pretty, to say the least:


    [code]
    struct typeinfo
    {
    void * (*increment)(void*);
    void * (*decrement)(void*);
    void (*copy)(void*, void*);
    int (*compare)(void *, void*);
    };



    struct typeinfo *
    typeinfo_init(
    struct typeinfo * info,
    void * (*increment)(void*),
    void * (*decrement)(void*),
    int (*compare)(void *, void*),
    void (*copy)(void*, void*)
    )
    {
    info->increment = increment;
    info->decrement = decrement;
    info->compare = compare;
    info->copy = copy;
    return info;
    }




    struct type
    {
    void * begin;
    void * end;
    };




    struct type *
    type_init(struct type * data, void * begin, void * end)
    {
    data->begin = begin;
    data->end = end;
    return data;
    }




    unsigned
    copy(struct type * dest, struct type * src, struct typeinfo * specific)
    {
    void * db = dest->begin, * sb = src->begin;

    unsigned result = 0;

    while(db != dest->end && sb != src->end)
    {
    specific->copy(db, sb);

    db = specific->increment(db);

    sb = specific->increment(sb);

    ++result;
    }

    return result;
    }





    void *
    find(struct type * data, void * what, struct typeinfo * specific)
    {
    void * db = data->begin;

    while(db != data->end)
    {
    if(0 == specific->compare(db, what))
    return db;

    db = specific->increment(db);
    }

    return 0;
    }





    void *
    char_inc(void * p)
    {
    return ++(char*)p;
    }


    void *
    char_dec(void * p)
    {
    return --(char*)p;
    }


    int
    char_comp(void * a, void * b)
    {
    return *(char*)a - *(char*)b;
    }


    void
    char_copy(void * a, void * b)
    {
    *(char*)a = *(char*)b;
    }
    [code]




    And a sample proggie:

    Code:
    int
     main()
    {
     char src[] = "Success.";
     
     char dest[100] = "Failed.";
     
     char c = 'c';
     
     struct typeinfo char_type;
     
     struct type s, d;
     
     typeinfo_init(&char_type, char_inc, char_dec, char_comp, char_copy);
     
     type_init(&s, &src[0], &src[strlen(src)]);
     
     type_init(&d, &dest[0], &dest[100]);
    
     copy(&d, &s, &char_type);
     
     char * found = find(&d, &c, &char_type);
     
     printf("dest: %s\nfound: %s\n", dest, (found ? found : "(null)"));
     
     return 0;
    }



    I think that's what make C++ such a powerful language - the fact that the same functionality can be accomplished with so much less effort.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You two are missing the question.. What do you think of using that style? I know HOW to do it.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Thantos
    You two are missing the question.. What do you think of using that style? I know HOW to do it.
    Let's not start this debate. You either like OO or you dislike OO. There are people in either camp.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I think FILE and its family of functions are good examples of using ADT in C. You don't need to know all of the complexities under the hood -- you just use the functions (correctly) and things are easy -- leave the work of getting the job done to the implementor.

    The time_t stuff is similar; but using them can sometimes leave you wanting more information. Whether or not it is successful depends on how the ADT is put together, documented, understood, and used.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Originally posted by Thantos
    You two are missing the question.. What do you think of using that style? I know HOW to do it.

    The answer depends on the language, in my opinion. If all I had was assembly to work with, I doubt that I would have much interest in that style. On the other hand, using C++, you're pretty much wasting a good algorithm if you don't use it!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Let's not start this debate. You either like OO or you dislike OO. There are people in either camp.
    We aren't talking about OOP.
    The answer depends on the language
    Agreed. To help show what I was talking about I was attempting to convert our last homework assignment which was all about ADT from Pascal to C and it is a pain in the ass, due to C not having pass by reference in a nice and easy way.

    For me it help illustrate how seperating the user interface routines from the "work horse" routine can be benifitual.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Thantos
    We aren't talking about OOP.

    Agreed. To help show what I was talking about I was attempting to convert our last homework assignment which was all about ADT from Pascal to C and it is a pain in the ass, due to C not having pass by reference in a nice and easy way.

    For me it help illustrate how seperating the user interface routines from the "work horse" routine can be benifitual.
    You're not? How aren't you talking OO? Let me refresh you:

    The real life example we were given was that of a TV and it's remote. The user knows how to use the remote to change the channel but has no idea how the TV actually changes the channel. So that if you were to change the TV you wouldn't have to change anything with the user interface (the buttons on the remote).
    How again is this not OO?

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Originally posted by Thantos
    You two are missing the question.. What do you think of using that style? I know HOW to do it.
    I use it whenever I create a class. Especially since Eclipse (www.eclipse.org) can automatically generate them.

  11. #11
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    i really enjoy using ADT's, they make coding so much easier and your program look so much cleaner
    "Assumptions are the mother of all **** ups!"

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    It is a very good idea, and if you have done 2 minutes of C programming you'd notice it is more prevelant than you might have thought. a FILE* is an opaque type taht you can only manipulate via functions such as fopen/fclose/fread/fwrite.

    This is not necesary an ADT, but more of an OOP style. Although this paradigm is applied to ADT's constantly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM