CS115 Lab: Abstract Data Types (ADT's)
      Implemented as CLASSES in C++

Highlights of this lab:

In this lab,  you will:

Reference: See the programming example on "Classes" in the C++ Syntax pages.

Lab Exercise:

Click the little computer above for a detailed description.
NOTE: Your lab instructor will tell you what will be marked for this lab.

Data Abstraction.

Your class textbook gives a comprehensive explanation of the purpose of data abstraction. To put it simply, data abstraction is just a way of thinking about how you would solve a problem without worrying about the computer language tools you need to solve this problem. You focus on the problem and what, in general terms what you would need to implement your solution. Don't be concerned about what data types do (or don't) exist in the computer language. When you are ready to implement your ADT, consider the following:

These two things constitute an Abstract Data Type, or an ADT.

The operations for an ADT fall into 4 categories:

Constructor Creates data for the ADT.
Transformer / Modifier / Setter Modifies data in the ADT.
Observer / Accessor / Getter Allows you to "look but not touch" the data in the ADT.
Iterator Provides the ability to move through components in an ADT, one at a time.

The following is an example of a set of specifications for an ADT. This includes a description of the data and the operations on these values.

Data or Fields
        name    -    a character array of 20 elements (represents student name)
        id      -    an integer  (represents student #)
        marks   -    an array of integers (for storing marks)
	setName	-    a transformer type operation
	setID   -    a transformer type operation
	setMarks-    a transformer type operation 
	reporter-    an observer type operation

Theory and abstraction are fine, but sometimes it's helpful to relate this to some practical knowledge in order to put the concepts into place. With that in mind, we'll introduce the term classes here, which is how ADT's are implemented in C++. Here is a comparison between classes, which you may not have read much about yet, and structures, which you explored in a previous lab.

Declaring and using classes is similar to declaring and using structures in C++. First you declare the class (or structure), and then you declare the instances of the class (or structure).

For example, here are comparitive examples of how you declare and instantiate a class and a structure.

class   Computer struct   STUDENT
{ {
  // By default: private
  // data
  // operations
  // By default: public
  // data
  // operations (not recommended)
}; };
// Instantiate a class // Instantiate a structure
Computer   pc; STUDENT   stu;

There, now you've got the formal definition of an ADT and had a look at how they are implemented in C++ as classes. In the next section, you'll examine class members in more detail.

Definitions in an ADT.

Before we proceed, let's just get our terminology in order:

class An implementation of an ADT.
class member A datum associated with the class, or
a function used in the class to implement one of the operations associated with the class.
      These functions are also referred to as methods of the class.
class object An instance of the class.

Class members are either public, meaning that they are accessible from outside the class, or private (the default), meaning that only the class can access them. Generally data is designated as being private (look up "information hiding" in any text) and the methods are designated as being public, so that they can be called from elsewhere.

Here's a simple example:

class Computer
	// It's not necessary to include this term since that is the default.
	// However, it's good form to do so and clarifies your meaning.
    int processorspeed;
    void setspeed(int);
    void readspeed(int);

Note: It doesn't matter if private comes before public, or the other way around.

Let's try to visualize what two instances of this Computer class would look like. Say you've instantiated a Computer called pc and another called cray. Each of them would exist as an object and have its own methods and data.

You would use dot notation to refer to the members of each instance. e.g. pc.setspeed(x); or cray.setspeed(x); Be careful though - if you tried to access the data member, pc.processorspeed from outside the class you would get an error, because that was declared as being private, unlike the methods, which are public.

Implementation Details.

Member Functions

A header file - filename.h - is often used to contain the function prototypes and data members for the class. Another file, generally the same filename but with an extension of .cpp, contains the actual function definitions. You must remember to #include "filename.h" in the filename.cpp and in the calling program.

When you enter the code for the function in the filename.cpp file, you must precede each function data member name with the name of the class.

	void Computer::setspeed(int p)
The double colon :: is known as the scope resolution operator. It ties the function name to the name of the related class. This is necessary because the function might also exist in another class.

Going back to the example we were just looking at, you could have three files involved in the total program.

computer.h The header file for the Computer class.
This contains the private data members and the function
prototypes for the operations members.
computer.cpp The function definition file for the Computer class.
Must contain:   #include computer.h
compserv.cpp The main program that calls the class.
Must contain:   #include computer.h

You may recall from lab 1 that each .cpp file must be compiled to an object, .o, file individually and then linked together to produce the executable file. Click here to review that material. You can use the Makefile introduced last lab to simplify the process. Click here to get instructions.

Constructors - a special case

When we talked about ADT's one of the classes of operation we identified was Constructor. Constructors initialize data that wasn't there before.

An ADT can have two different general types of constructor. The first constructs the ADT itself. The second constructs pieces of the ADT if the ADT contains other ADTs, as in a list of addresses.

The first type of constructor generally has special language support. In C++ there are a few variations that allow for all, some or no data to be specified at object creation time. Any further changes would be made using transformers or the other type of constructor. The details of how to implement the first type of constructor follow in the next subsection.

The second type of constructor works just like any other member function, but at some point it may call a constructor for the contained data type.

C++ Constructors

Copy Constructors

A copy constructor creates a clone of an existing object by initializing a new object, with an existing object of the same class. Suppose you had a class called Tree and had defined a Tree object called Pine1 To create a clone, you could enter: Tree Pine2(Pine1); or: Tree Pine2 = Pine1; If you had no copy constructor defined, then the compiler would supply a default "copy constructor" to create the clone.
Note: The first notation "Tree Pine2(Pine1);", is the current preferred syntax. The second notation "Tree Pine2 = Pine1;", is now considered obsolete. However both forms are given here because a programmer is likely to encounter both.

Copying an object using the default copy constructor may work for simple objects. However, if there were pointers in the original object, only the pointers would be duplicated, not the data that was being pointed to. Following the previous example, suppose Tree objects had an integer data member, and a pointer data member to a character string. Pine1's pointer would have the address of the same data as Pine2's pointer! There would then be two ways of accessing (and modifying!) the same data. This type of a copy operation is called a shallow copy because the "pointed-to" data is not copied when a 'clone' is made. This is not a true clone.

If pointers are part of an object, what you may need is a deep copy which you would have to define yourself. Here is the general syntax for a deep copy constructor.

type :: type (const type  & object_name)
In that example of the general syntax, type refers to the class type name. Notice how the parameter is passed to the copy constructor. You do not want to do any harm to the existing object, so you declare the object parameter as type const and use the ampersand & to pass it as a reference rather than a value. For example:
Tree::Tree(const Tree & otherTree)
    age = otherTree.age;
    descrip = new char[strlen(otherTree.descrip) + 1];
    strcpy(descrip, otherTree.descrip);
Actually, we haven't seen the new operator yet. It defines a dynamic variable in "free store" - a topic of a later lab. However the code is given here for the sake of giving a complete example of a deep copy constructor.


The ADT concept is supported by many languages. Some of them can garbage collect things that you are done with. C++ does not. There are times when you will need special things done to an object when it is destroyed.

Lab Exercise -- C++ Abstract Data Types

Transfer the classorg.cpp grader.cpp grader.h Makefile files from the CS Dept's anonymous ftp site to your Hercules account space. Do that by:
  1. Entering the commands:
     cp /net/data/ftp/pub/class/170/ftp/cpp/Classes/classorg.cpp classorg.cpp
     cp /net/data/ftp/pub/class/170/ftp/cpp/Classes/grader.cpp grader.cpp
     cp /net/data/ftp/pub/class/170/ftp/cpp/Classes/grader.h grader.h
     cp /net/data/ftp/pub/class/170/ftp/cpp/Makefile Makefile
  2. OR, by using the command line FTP program using this information:
       The path: pub/class/170/ftp/cpp/Classes
       The files: classorg.cpp, grader.h, grader.cpp

I have included the generic Makefile from last lab. Click here to get instructions.

Exercise 1

The purpose of this exercise is to illustrate the files associated with classes and header files.
These files will regulate the information in the class instances of "Grader".

Exercise 2

The purpose of this exercise is to practice implementing and using ADT operations. For this exercise you will create a new report operation called getID, and a constructor/destructor pair for the Grader implementation.
  1. getID
  2. Constructor
  3. Destructor

This page last modified:
Thursday, 06-Oct-2011 12:13:32 CST

CS Dept Home Page
CS Dept Class Files
CS115 Lab Files

Copyright: Department of Computer Science, University of Regina.