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?
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.
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.