-
Adt help....
hey I'm new here and I was wondering if anyone can help me to under stand what an ADT (abstract data type) is.... this isn't for homework or anything I just want to understand it.... I've read the chapter on ADT's many times but still don't really get it....
a quick and east example would be great also... :D
Code:
cout << "test to see if I'm doing the code tags right :P" << endl;
-
there may be other uses for this, but let me give you an example -
you have a payroll program you need to write.
you will have 3 classes
class 1: a base class called person ( ADT)
class 2: hourly worker
class 3: salaried worker
classes 2 and 3 are derived from "person"
person looks like this -
Code:
class person
{
char name[20];
date birthday;
virtual float calculatePayCheck(void) const = 0; //this line makes this class pure virtual
// (abstract)
//it is the responsibility of all derived
//classes to specify the action to be taken
//when this function is called
}
since both salaried and hourly workers both get pay checks and since you may need to reference both types of employees together when creating their paychecks, you can now refer to both of them the same way. let me elaborate. you want to store all employees in an array. well you certainly can't create an array that stores both salaried and hourly workers. but you can create an array of pointers type "person". both types of employees pay checks gets calculated differently, however, you can now treat both types as if they were the same. instead of having to call Salaried::calculatePayCheck and Hourly::calculatePayCheck you just simply call calculatePayCheck and the compiler/program knows which is which.
this is a really rough explaination - my intention was to dumb it down quite a bit becuase it is a rather abstract subject :). i suggest reading up on polymorphism and then re-reading about ADT. you probably really won't understand all that well until you do some examples too.
-
In a short statement:
An ADT is a class that you use as a base class for several related classes, where it does not make sense to create an object of the base type.
In misplaced's demonstration, for example, the derived classes of HourlyEmployee and SalariedEmployee make sense. You can have a salaried employee or an hourly one, and they are logically connected (not to mention share a "lot" of functionality), but in the example, you can't have someone who is neither, so Person becomes abstract.
*edit*
If you are at all familiar with Java, then the idea is similar to that of an Interface in Java (of course, in C++, you can have functionality in your ADTs). And from an OO standpoint, ADTs are essentially interfaces.
-
ok thanks guys....
would this count as a valid ADT?
Code:
class ModuloCounter
{
public:
void Increment();
private:
unsigned int m_nCount;
}
void ModuloCounter::Increment()
{
if (m_nCount >= THE_VALUE_YOU_WANT)
{
m_nCount = 0;
}
}