Implementing ADTs in C++

Tom Kelliher, CS23

Mar. 5, 1997

Reading:

  1. Carrano: pp. 120--136.

  2. Lippman: Ch. 5, 6.1--6.2.

ADT Example: Employee List

Members:

Design goals:

Common implementation details

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

struct employeeRecord
{
   char firstName[20];
   char lastName[30];
   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:

Compiling:

cc -o database main.c list.c

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>)
{
}

Compiling:

g++ -o database main.cc list.cc

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 Mar 3 14:30:52 EST 1997
Tom Kelliher