Implementing ADTs in C++

Tom Kelliher, CS18

Apr. 24, 1996

ADT Example: Employee List

Fields:

Design goals:

Common Implementation Details

struct committeeList
{
   unsigned short committeeCode;
   struct committeeList*  next;
};

struct employeeRecord
{
   char* firstName;
   char* lastName;
   unsigned char deptCode;
   long salary;
   struct employeeRecord* manager;
   struct committeList* committees;
   struct employeeRecord* next;
};

ADT employee list operations?

Implementing ADTs in C

Create:

struct employeeRecord* employees = NULL;

Where is the ``wall?''

What information is hidden?

Kludge Information Hiding in C

Skeleton of main module:

#include "list.h"
...

main(){

create();
addEmployee(...);
...
}

Skeleton of list.h:

/* definitions for committeeList, employeeRecord structs */

int create(void);
int destroy(void);
int addEmployee(<formals>);
...

Skeleton of list.c:

#include "list.h"

static struct employeeRecord* employees;

/* definitions for ADT operations */

Three files:

Implementing ADTs in C++

Create:

EmployeeList employees;   // call constructor

Where is the ``wall?''

What information is hidden?

Information Hiding in C++

Skeleton of main module:

#include "list.h"
...

main(){

EmployeeList employees;
employees.addEmployee(...);
...
}

Skeleton of list.h:

// definitions for committeeList, employeeRecord structs

class EmployeeList
{
 public:
   EmployeeList(void);   // constructor
   ~EmployeeList(void);  // destructor
   int addEmployee(<formals>);
   ...
 private:
   employeeRecord* list;
   ... other hidden members or methods
};

Skeleton of list.cc:

#include "list.h"

EmployeeList::EmployeeList(void)
{
}

EmployeeList::~EmployeeList(void)
{
}

EmployeeList::addEmployee(<formals>)
{
}

Classes in C++

Similarity to Structures

struct foo
{
   int this;
   char that;
};

foo bar;

class Foo
{
 public:
   foo();
   ~foo();
   char moreFoo(int, int);

 private:
   int allMyFoo[FOO_AMOUNT];
   int hiddenFoo(int);
};
Foo bar;

Methods

Linking Methods to Classes

Special Methods

Member Access



Thomas P. Kelliher
Mon Apr 22 21:51:23 EDT 1996
Tom Kelliher