Tom Kelliher, CS18
Mar. 13, 1996
data.h:
#ifndef __DATA_H #define __DATA_H const int NAME_LEN = 30; void GetName(char name[]); int GetAge(void); // ... #endif
data.cc:
#include "data.h"
struct record
{
char name[NAME_LEN];
int age;
};
// static restricts scope to this file
static record employee = { "Kelliher, T. P.", 29 }; // and holding :-)
void GetName(char name[])
{
strcpy(name, employee.name);
}
int GetAge(void)
{
return employee.age;
}
main.cc:
#include <iostream.h>
#include "data.h"
int main()
{
char name[NAME_LEN];
GetName(name);
cout << name << endl;
cout << GetAge() << endl;
return 0;
}
Can my client function use record as a type?
Is this a general, convenient-to-use solution?
Imagine:
What can go wrong?
How can these problems be addressed?
A collection of data together with a set of operations on that data.
Interface/Implementation (data structure)
ADT examples:
data.h:
#ifndef __DATA_H
#define __DATA_H
const int NAME_LEN = 30;
class Record
{
public:
Record(const char inName[], int inAge);
void GetName(char outName[]);
int GetAge(void);
// ...
private:
char name[NAME_LEN];
int age;
};
#endif
data.cc:
#include <string.h>
#include "data.h"
Record::Record(const char inName[], int inAge)
{
strcpy(name, inName);
age = inAge;
}
void Record::GetName(char outName[])
{
strcpy(outName, name);
}
int Record::GetAge(void)
{
return age;
}
main.cc:
#include <iostream.h>
#include "data.h"
int main()
{
Record r("Kelliher, T. P.", 29);
Record s("Presley, E.", 39);
char name[NAME_LEN];
r.GetName(name);
cout << name << endl;
cout << r.GetAge() << endl;
return 0;
}
A list is a collection of ordered items
List Parameters???
Have we specified a data structure (implementation)?
Behavioral specification
Example:
List l; dataItem d; l.CreateList(); l.ListInsert(1, bread, success); l.ListInsert(2, butter, success); l.ListInsert(3, jam, success); l.ListInsert(4, toaster, success); l.ListRetrieve(2, d, success); l.ListDelete(2, success);
Without knowledge of the implementation:
Example: ADT Sorted List Operations: