Professional Documents
Culture Documents
34.1 Introduction
Whatever you believed in the past, be assured that you are learning computer
science and the programming language we use to teach computer science
concepts is C++. The choice of the language is often debated by people, just as
the previous choice, Pascal, was frequently debated. Our choice of C++ is simply
dictated by the fact that the AP Computer Science Examination is given in C++.
This is one reason why you have seen various examination alerts sprinkled
throughout the chapters. Currently there are various people who feel that a switch
to Java should be made, and if in fact the examination switches to Java or some
other language in the future, then the language used will change.
When you finished Part III of this Exposure C++ series, you finished learning
most of the C++ syntax. Starting with Part IV the emphasis with be primarily on
computer science concepts. Now, we will continue to use C++ for all the
program examples, and you will see programs in C++ that you have not seen
before. But there will not be many new C++ keywords, functions or capabilities
introduced anymore. You have learned the majority of C++ that is going to be
introduced in this course.
The real power of a programming language is not so much which tools are
available in a language, as how many different tools can you create with the
language. You saw evidence of that in the OOP section. We created an array
class from scratch. You may not really like that example because C++ does
provide us with an array data structure. Yet you saw that C++ gave us the tools to
create a better, safer array with greater capabilities.
In the chapters to come, you will be using, and creating stacks, queues, linked
lists, binary trees, circular lists, priority queues and hashing tables. Right now
this looks like a list of strange sounding names. All these names have one simple
feature in common: they are data structures that do not exist in C++. You need
them, you want them, so you must create them. Using and implementing many
different data structures is the goal of the remaining chapters.
Computer science has its share of vocabulary, and object oriented programming
did little to reduce the definition list that started long ago in Chapter I. This
chapter starts a new unit in the computer science course and it is important that
you are on solid ground with the vocabulary that will be used. Abstract data types
is the title of this fourth unit and the focus is heavily on taking control of the data
types that we use and can create in programming. I propose to back up to the
beginning and take another look at the various data types introduced along the
computer science journey.
Variable Definition
Ouch, that was a bad start. You used to think that these books were not half bad
because they used simple language. That data type definition and variable
definition is less than terrific. Both definitions are presented together because
they work so much together. The definitions are short but think about it. The
purpose of data types is to let the compiler know how much memory needs to be
allocated.
The statement cout << ”811 Fleming Trail” << endl; displays a street address.
A data type is not necessary to display that literal string. On the other hand, if
there are numerous addresses to print on mailing labels, these multiple addresses
will need to be stored in some manner. The storage is done with a variable of
some data type that is suitable for the job. So information is stored in memory,
and to identify that information a variable identifier must be provided.
The data type definition states that the memory size is determined by the data
type. This part is very important. Consider for instance an integer. What is the
size of an integer? In mathematical terms it is infinity. In C++ terms - for many
compilers - it is 2 bytes with a range well below infinity. The point is that the
specific compiler that you are using knows the specifications for each data type
that you use. If the data type is more complex and composed of many other
components, then the individual components are added up for the total data type
requirements.
char 1 byte
bool 1 byte
int 2 bytes
long 4 bytes
float 4 bytes
double 8 bytes
There should be little difficulty with this definition. A simple data type holds a
single value, period. There is no fancy arithmetic; there are not any
complications because every simple data type has its own specified memory
requirement.
Do you remember that first data structure definition? The main point of the first
definition was to drill home that a data structure is used to store more than one
value. Furthermore, the complexity of a data structure can be such that a large
data structure is created with smaller data structures as components, and each one
of the smaller data structures is made up of simple data types. That first definition
did fine for starters but it had some fundamental problems that do not properly
explain the true nature of a data structures. Later another definition was presented
that explained the data structure in greater detail.
The improved data structure definition was less comfortable when it was first
introduced and it may be less comfortable right now. Basically, it is saying that it
is lovely to talk about the values that are stored, but there is also the issue of how
do you access those values for data processing. There is quite a difference in the
manner that you access different data structures. An individual element of a
record (struct in C++) is with a field identifier and period syntax like
Student.Name. Accessing an array element requires knowledge of the element’s
index and the use of bracket syntax like Student[100].
The data structures provided by a programming language are not always the same.
The language BASIC does not have a record. The language Pascal provides the
same basic data structures as C++ and also the Set. Newer version of C++ also
add the string data structure.
Additionally, the College Board has provided a group of data structures to make
learning computer science with C++ easier. They are called the AP classes. You
have already used some of these classes.
With the use of these classes it is also possible to examine students in a consistent
manner by providing questions that all use the same data structures. You will find
that many future C++ compilers will include new data structures that will very
strongly resemble the current AP classes.
AP Classes
The AP classes are a good example that it is possible, and desirable to use the
tools provided by C++ and make our own better tools. In all honesty, the average
student probably did not think that there was much difference between any of the
C++ data types and the AP classes. From your point of view any C++ data type
could be used no matter what, and the AP classes required inclusion of certain
files in an appropriate directory.
If you prefer to think of the AP classes as C++ data structures, go right ahead.
You did not create them. At the same time you have been looking rather closely
at some of the AP classes in the last couple of chapters, and soon you will be quite
familiar with the apstack and apqueue classes.
You probably think that this is another goofy Schram definition. It probably
looks like a crossing between a data structure definition and an object definition
to you. We have not reviewed the object definition yet, but that ADT has the
distinct smell of attributes (data) and actions (functions) that OOP has been
blasting at you for many chapters. At the same time, it has some of the data
structure flavor as well. The ADT definition has two very important components.
The first component states that an ADT has data and functions that act on the
data. At that stage it sure sounds like a data structure. Now there comes a later
component that says that the implementation of the ADT is not part of any ADT
definition.
This means that I can talk about a data type, called CardDeck. Before I even
worry about how to store data, and how to manipulate the data, I sit down and do
some thinking. This data type is needed for a program to play Blackjack. I need
to store cards and those cards need to be shuffled, dealt, counted, etc. At this
stage I am thinking about the data type in an abstract manner. Whether an array, a
record, a class, whatever is used to implement this data type is not a concern right
now. At the abstract data type stage, implementation is not an issue.
Object Definition
The relationship between an ADT and an object is so strong that an ADT can be
used to define an object. Objects have abstract data type written all over it. The
key difference is that an object goes further and also includes inheritance and
polymorphism. You may think that I left out encapsulation but that was quite
intentional. What is encapsulation? Is it not packaging both the attributes (data)
and actions (member functions) in the same data type. It sure sounds like the
definition of an ADT to me.
The information hiding topic is not new, but it belongs in this chapter. Data
abstraction and information go hand in hand. Besides, what do you expect from a
book that is titled Exposure.
OK, I put the key in the ignition and turn the key. This will complete an electronic
circuit and activate the starter motor. As the starter motor turns the engine over,
the following process brings the car in motion. Fuel is pumped from the tank to
the fuel injectors. The fuel injectors convert the liquid gas to a fine spray and
Is it necessary for you to know all those car details before you drive a car? No it
is not necessary. Furthermore, as much detail as I wrote about car functionality,
you can count on many details that are left out. Details that I never knew, and it
simply does not matter. A car is a classic example of an object. It contains
attributes and actions, and you can activate the car actions without knowing how
such actions are implemented.
Suppose that you have a large program team working on a new software
application. When your product is finished, you eagerly market and sell the
product to anybody interested in buying it. What do the customers get? They get
an executable file of your software application and all the necessary files to run
the program. Your customers do not get any of the source code. The majority of
customers would not be interested in the source code and the few people who are
interested in the source code could duplicate your product and compete in an
At this point the focus is completely on the icon editor. You design it, write it,
test it and then use it for your graphics game. It is precisely at this point that the
fully tested icon editor is used to create some graphics sprites. You are no longer
concerned about the implementation of the icon editor. That part is done now and
focus switches.
You can pretty well tell that data abstraction is a big deal. It has been
mentioned earlier, and the topic keeps returning. Right now there is an entire
chapter devoted to this abstraction business. It is wise to take data abstraction
seriously. It is an important topic in computer science and the committee who
creates the AP Computer Science Examination enjoy including ADT questions on
the exam.
The whole idea of ADT questions is to test a student’s ability to create new
routines with the aid of existing routines. You are in actuality programming at a
For this problem, you will write two functions that use the apstring class to
represent strings. The details of the apstring class implementation is not known to
you. However, apstring can be manipulated with a large variety of functions. In
particular, for this problem you will need three functions, whose headers and
descriptions follow below:
For example:
S A T U R D A Y
S A T D A Y
In writing function DeleteString you may only manipulate strings by using calls
to functions subtr, length and operator+.
(b) Write function InsertString that alters a nonempty string, S1, by inserting
a nonempty string, S2, starting at the character position indicated by Start.
For example:
S1
S A T U R D A Y
S2
F U N
S1
S A T U R F U N D A Y
S2
F U N
The two solution functions of this free response question do not involve any
source code knowledge of the apstring class. This is the whole point of working
with data abstraction. You are expected to create new tools with the use of
existing tools. You are not expected to create tools from scratch.
Do not flip the page right away. The solutions are shown there and motivated
student that you are, you want to get out a piece of paper and see if you can write
the solution without any help.
You may not may not accept the two solutions. Perhaps you can trace through the
code and see that the code is correct. At this stage you should be able to do that
type of tracing. Just to be sure, I am including two programs that will test each
one of the functions.
// PROG3401.CPP
// This program demonstrates how to use data
abstraction by
// creating a new DeleteString function using existing
function
// without knowing the implementation of the new
functions.
#include <iostream.h>
#include <conio.h>
#include "APSTRING.H"
void main()
{
apstring String1 = "SATURDAY";
apstring String2 = "GOOD MORNING";
apstring String3 = "HELLO";
clrscr();
PROG3401.CPP OUTPUT
SATDAY
GOOD MORNIN
ELLO
The DeleteString function does a nice job. Notice the manner in which the
function is tested. Three different kinds of strings are removed. The first string is
from the middle, the second string from the end, and last test is a string removed
from the front of the string. Is this a complete test of the function? Can you think
of test situations that were not considered in this program?
// PROG3402.CPP
// This program demonstrates how to use data
abstraction by
// creating a new InsertString function using existing
function
// without knowing the implementation of the new
functions.
#include <iostream.h>
#include <conio.h>
#include "APSTRING.H"
void main()
{
apstring String2 = "GOOFY";
apstring String1 = "SATURDAY";
clrscr();
InsertString(String1,String2,6);
cout << String1 << endl;
String1 = "SATURDAY";
InsertString(String1,String2,1);
cout << String1 << endl;
String1 = "SATURDAY";
InsertString(String1,String2,9);
cout << String1 << endl;
getch();
}
PROG3402.CPP OUTPUT
SATURGOOFYDAY
GOOFYSATURDAY
SATURDAYGOOFY
You learned in the previous section how new functions can be created with the
help of existing functions. Furthermore, it is possible to create new functionality
At the same time, you may have been surprised that data abstraction was
demonstrated without using any objects. The definition of an object, shown a few
pages back, states that an object is an ADT. This certainly makes objects
excellent examples for data abstraction programs. Do not worry, because this
section will demonstrate data abstraction with the aid of two classes.
Turtle Graphics
In the eighties many students learned to create graphics images with the help of a
special turtle graphics package. The programming language Logo used such
graphics and it was an excellent approach in creating simple graphics images.
Data abstraction with objects will be shown with the aid of a Turtle class. This
class will be demonstrated in program PROG3403.CPP. Now this program will
not demonstrate data abstraction with objects yet. First, we must set the stage and
the stage is going to be set with a very simple graphics class, called Turtle.
The Turtle class actually draws its “graphics” images in text with pound signs.
This is done to make sure that the program example will execute on any computer
system with any compiler. The concepts of turtle graphics is the same and you
just need to imagine a fancier pixel graphics display. The concepts of turtle
graphics is to move a “turtle” on the screen. Once upon a time it did look like a
turtle. In our program the turtle is our cursor. The object is to move the turtle
around with a variety of commands to manipulate some image. The command
descriptions of the Turtle functions are shown before the actual program
example.
void PenDown()
// cursor can move to a new location and will draw a
line
void Change(Direction P)
// Changes the direction of the moving cursor. There
are
// four directions: UP, DOWN, LEFT, RIGHT. The four
// directions are enumerated constant to be used as
parameters.
Program PROG3403.CPP draws a square first, skips a distance with the pen up,
and then draws a line. Horizontal distances are intentionally doubled to make the
horizontal and vertical distance give approximately the same length.
// PROG3403.CPP
// This program draws a square and a line with the Turtle class.
#include <iostream.h>
#include <conio.h>
#include <dos.h>
#include "BOOL.H"
class Turtle
{
private:
int XPos;
int YPos;
bool Show;
Direction Point;
public:
Turtle() { XPos = 40; YPos = 12; Show = true; Point = UP; }
void PenUp() { Show = false; }
void PenDown() { Show = true; }
int GetX() const { return XPos; }
int GetY() const { return YPos; }
void Change(Direction P) { Point = P; }
void Move(int Distance);
};
void main()
{
clrscr();
Turtle T;
T.PenDown();
T.Move(5);
T.Change(RIGHT);
T.Move(9);
T.Change(DOWN);
T.Move(4);
T.Change(LEFT);
T.Move(9);
T.PenUp();
T.Move(5);
T.PenDown();
T.Move(10);
getch();
}
PROG3403.CPP OUTPUT
##########
# #
# #
# #
######### ##########
There exist several options for us right now. Every time we need a square or
rectangle, we can use the existing commands. That option is undesirable because
there are just too many commands. We can also expand the Turtle class and add
a Square member function. Finally, we can make Turtle a base class and then
derive a class, Turtle2 that will implement a Square function.
I have selected the last option. It seems to be truly in the spirit of data abstraction.
In this situation we are declaring a new class, Turtle2, and derive it from the
Turtle base class. We now have all the functionality of the Turtle class and in
proper data abstraction style, we have no clue about any of the Turtle details.
This allows us to create a Square member function for our current needs. This
process is shown in program PROG3404.CPP, which draws a single square, and
the main function shows that it is considerably less work.
// PROG3404.CPP
// This program demonstrates abstraction by creating a
second
// Turtle class without any source code of the base
Turtle class.
#include <iostream.h>
#include <conio.h>
#include <dos.h>
#include "BOOL.H"
void main()
{
clrscr();
Turtle2 T;
T.Square(5);
PROG3404.CPP OUTPUT
##########
# #
# #
# #
##########
There are bunches more that can be said about data abstraction, information
hiding and all this good program design stuff in a proper manner. Such a topic
If you are not convinced that data abstraction and information hiding are
important, fine. Just promise that you will keep an open mind and look closely at
the programs that will follow. Hopefully, you will be a convert by the time that
this book is finished.