Hey, what's up guys,

Can anyone explain what ADT is? How do you know when you are using it? When? Why?
I mean, in a simpler way than some books define it.

P.S. I am not even sure if my questions are valid :D

Thanks!

Recommended Answers

All 4 Replies

Just a quick explanation on Abstract Data Type(ADT)

WHO: Abstract Data Type
WHAT: Abstract Data Type are data-type that combines the common functionalities from different related objects into one package, so that those different but related object's interface can inherit from the ADT thus making it more flexible and and less coding for the programmer.
WHERE: Anywhere, when you code, in your life wherever applicable.
WHEN : Use it when you want to generalize a design. When you have many objects that have similar functionalities, you can Abstract the common functionalities into one class, and have the other objects inherit from them.
WHY You would create ADT for numerous reasons. First it generally leads to a flexible design if designed well. Second its less coding for the programmer. Third its more reuseability . Fourth, it leads to easier maintenance. And Fifth, it creates the possibility of polymorphism.

Here is a quick example :

//abstract data type
struct Object{
 virtual std::string toString()const = 0;
};
struct Int : Object{
 int val;
 std::string toString()const{ return boost::lexical_cast<std::string>(val); }
 Int(int iVal) : i(iVal){}
};
struct Float: Object{
 float val;
 std::string toString()const{ return boost::lexical_cast<std::string>(val); }
 Float(float fVal) : val(fVal){}
};
int main(){
 Int i(0);
 Float f(123.456);
 std::string strNum = i.toString() + f.toString();

}
commented: Nice answer! +15

Abstract Data Types is a way to generalize/model software so that the same interface used with different implementations and types.

Standard Template Library is a good example of Abstract Data Types. For example the interface for stack is independent of the implementation. Under the hood stack may be implemented by linked list, array or whatever is best suited for the problem.

In addition using templates same software model can be used with different data type; floats int, pointer to people objects etc. Below is an example of std::stack used with int and float.

#include <iostream>
#include <stack>


int main( int argc, char *argv[])
{
    // creat int stack
    std::stack<int> i;
    i.push(1);  i.push(3);  i.push(5);

    // create float stack
    std::stack<float> f;
    f.push(2.9884); f.push(4.8885); f.push(6.444);

    // show int stack
    while( !i.empty() )
    {
        std::cout << i.top() << std::endl;
        i.pop();
    }
    
    // show float stack
    while( !f.empty() )
    {
        std::cout << f.top() << std::endl;
        f.pop();
    }

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.