what is the purpose for using ADT

i have been reading some books that they used/ include ADT in their class. in the ADT class there was many virtual function and some pure virtual function (with =0), this is the part i don't really understand, they use pure virtual function in ADT class. After they implement to other class, they function with same name as the virtual function which will override them, for other usage.. so what's the purpose of virtual function and pure virtual function? what's the purpose using ADT?



(sorry for my broken english....)
Virtual functions are used to implement polymorphism.
Polymorphism is the ability of a type of class to behave as if it's another type of class.
http://www.cplusplus.com/doc/tutorial/polymorphism/

Pure virtual functions are used when a default implementation for the function isn't needed or wanted.
In that case the base class becomes an abstract type that cannot be instantiated as an object.

Silly example here: http://ideone.com/2exxa
Notice how we keep all objects as Animal's in the same place, and yet they behave differently because of polymorphism.

ADT should mean Abstract Data Type in your case, and roughly, is a definition of a way to store and work with information. Do not confuse it with Abstract Type, which is a class containing at least one pure virtual function.
http://en.wikipedia.org/wiki/Abstract_data_type
http://en.wikipedia.org/wiki/Abstract_type
ADTs are most useful with polymorphism where multiple concrete classes are derived from a single ADT. Perhaps an example will make this clearer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Shape
{
public:
    Shape ();
    virtual ~Shape ();
    double Area (void) = 0;
};

class Square : public Shape
{  double s;
public:
    Square (double side)
    { s = side; }
    ~Square ();
    double Area (void)
    { return s * s; }
};        

class Circle : public Shape
{  double r;
public:
    Circle (double radius)
    { r = radius; }
    ~Circle ();
    double Area (void)
    { return 3.14159 * r * r; }
}

...
Circle c(1);
Square * sq(2);
Shape * s1 = &c;
Shape * s2 = &sq;

cout << s1->Area() << endl;
cout << s2->Area() << endl;      


Note that the code refers to the Area function of Shape. It doesn't matter whether the shape is a circle or a square, the correct Area function will be called.
@Catfish2
Pure virtual functions are used when a default implementation for the function isn't needed or wanted.
In that case the base class becomes an abstract type that cannot be instantiated as an object.


You are wrong. Pure virtual functions may have a default implementation.
You are wrong. Pure virtual functions may have a default implementation.

What do you mean by "default implementation"?
I meant a definition, which can't be provided where the pure virtual function was declared.

Edit: nasty stuff... http://www.gotw.ca/gotw/031.htm
Last edited on
Yes I alco mean the definition. A definition may be provided for a pure virtual function. For example

strcut A
{
virtual ~A() = 0;
};

A::~A() {}
Last edited on
Topic archived. No new replies allowed.