C++ Class and Turbo Projects Lab

Tom Kelliher, CS18

Mar. 18, 1996

In this lab, you will create a class whose declaration and definition reside in separate files and a client program which resides in a third file. You will use Turbo C++'s project facility to tie the files together.

Define a class that will hold the set of integers from 0 to 31. An element can be set with the set member function and cleared with the clear member function. It is not an error to set an element that's already set or clear an element that's already clear. The function test is used to tell whether an element is set.

set.h:

#ifndef __SET_H
#define __SET_H

class smallSet
{
 private:
   char members[32];

 public:
   smallSet();
   void set(int item);
   void clear(int item);
   int test(int item);
};

#endif

set.cpp:

#include "set.h"
#include <assert.h>

smallSet::smallSet()
{
   int i;

   for (i = 0; i < 32; ++i)
      members[i] = 0;
}

void smallSet::set(int item)
{
   assert (0 <= item && item < 32);
   members[item] = 1;
}

// You implement clear and test.

main.cpp:

#include "set.h"
#include <iostream.h>

int main()
{
   smallSet aSet;

   aSet.set(3);   // aSet contains { 3 }
   aSet.set(5);   // aSet contains { 3, 5 }
   aSet.set(5);   // aSet contains { 3, 5 }

   cout << aSet.test(3) << endl;   // Prints 1
   cout << aSet.test(0) << endl;   // Prints 0

   a_set.clear(5);   // aSet contains { 3 }

   return 0;
}

Once you have entered the three files, you are ready to construct the project:

  1. Choose ``Project|Open Project.''
  2. From the bottom of the screen choose ``Add,'' to add source files.
  3. Add your two source files to the project. Choose ``Done.''
  4. To build the program, choose ``Compile|Make.''


Thomas P. Kelliher
Thu Mar 14 23:28:29 EST 1996
Tom Kelliher