CS170 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: You do NOT have to hand in this lab exercise, but doing it will help you in your class.

C++ typedef.

A typedef is a C++ construct that you can use to assign a generic name to a specific data type. The idea is that you can create a global typedef and then use that generic name rather than a specific data type in several functions. When you want to change the program to utilize a different data type, you only need to change the one typedef statement.

Consider a calculator program with this header file Calculatr.h which contains the line:
typedef int Data_Type;

You can see that the functions in that header file always refer to the user selected generic name Data_Type rather than the C++ data type int. e.g. You see: void GetOperands( Data_Type &, Data_Type &); rather than: void GetOperands( int &, int &);

To change that simple calculator program so that it uses float instead of int you only need to change the one line in the .h file from:
typedef int Data_Type;   to:   typedef float Data_Type;
None of the function prototypes or function code needs to be changed at all; just that one typedef line in the .h file.

The typedef construct allows you to implement a level of abstraction in your program. Taking this concept to a higher level, you can consider ADTs, or Abstract Data Types as described in the next section of these notes.

Data Abstraction.

You 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 Modifies data in the ADT.
Observer Allows you to "look but not touch" the ADT.
Iterator Processes all components in an ADT, one at a time.

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.

Type
	Grader
Domain
	Each Grader value is a collection of a character value name,
	and a set of integer values for ID and 3 marks per student.
Operations
	InitName	- a contructor type operation
	ChangeID	- a transformer type operation
	SetMarks	- kind of an iterator 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 last week's 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). The big difference between classes and structures is that a class definition includes a description of the kinds of operations that can be performed on any instance of that class.

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

class   Computer struct   STUDENT
{ {
  // class member declarations
  // (We'll expand on this in a bit.)
  // data declarations
}; };
   
// 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:

TermDefinition
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
{
private:	
	// 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;
public:
    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.

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 name with the name of the class.

e.g.
	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

Each one of these .cpp must be compiled individually, and the resultant object files must then be linked together to produce the executable file. Here is what that looks like conceptually:

If you want to have a look at these files, go to this web page in the C++ Syntax pages.

In order to compile a C++ program and produce an object file rather than an executable file, you need to specify the -c option when you compile. You would also of course, not specify the -o executable_name option when you compile. The object file will automatically be given a .o extension (file type). Here is the general syntax to compile C++ programs and produce an object files:

On Linux:    g++ -c file1.cpp   Or, on Hercules: CC -c file1.cpp
On Linux:    g++ -c file2.cpp   Or, on Hercules: CC -c file2.cpp

After you have created the individual object files, you need to use the g++ command again to create the executable file from the individual object files. Here is the general syntax:

On Linux: g++ -o executable_name file1.o file2.o
On Hercules: CC -o executable_name file1.o file2.o

Go back now to the illustration of the C++ files and how they are combined to produce an executable file. Here are the commands you would use to compile each of the C++ programs, and then link the object files into an executable.


Lab Exercise:


CS Dept Home Page
CS Dept Class Files
CS170 Lab Files

Copyright: Department of Computer Science, University of Regina.