Wyo C++ Ch. 14 Notes
Objective #1: Use built-in C++ arrays.
- You should be familiar with built-in C++ arrays.
However, you should only use apvector objects in programming assignments.
Therefore, the other objectives (see below) in this chapter are more important
than this first one.
- To declare a C++ array you can use a statement like
int sums[10];
which would declare the array sums to hold 10 separate integer values. The
index positions (i.e. subscripts) would range from 0 to 9.
- You can declare AND initialize a C++ array at one
time with a statement like
int sums[] = {1, 2, 3};
- You cannot use a variable to specify the size of a
C++ array. That is, int sums[maxNum]; would be illegal syntax since the compiler needs to
know exactly how much memory to allocate for the array at compile time.
- You can use subscript notation to assign a value to
a specific element of a C++ array
sums[4] = 15;
- Be careful because the compiler will allow you to
overwrite other memory locations (and possibly crash the program at run-time)
with a statement like sums[20] = 16;
where sums was only declared to size 10 for example.
Objective #2: Understand templates, template classes, and template functions.
- If you wished to create an array class to give additional features to the
handling of arrays (as we saw in Chapter 13 with strings) it would seem that
you would have to create a different class for arrays of different data types.
That is you would have to create one class for arrays of ints and another
for arrays of floats and another for arrays of doubles, etc. This is not necessary
since C++ allows you to use templates to create a template
class.
- By beginning your class definition with the line
template <class DataType>
you could continue to create a model for a class where C++ would later use
an actual data type (int, double, etc.) of your choice everywhere that the
word DataType
appears within the class definition. So function return types and incoming
parameter types can remain general as long as you use the word DataType. Note
that the word DataType can be any word of your choice. This technique is called
parameterizing for data type.
- When a programmer wants to declare an object from the class definition,
he/she must use a declaration statement like
TemplateExample <int> myObject;
where TemplateExample is simply the name that he gave to the class (like "apstring")
on the second line of its definition and the
<int> modifier specifies
that this object should handle int data types and the word int should replace
all occurrences of the placeholder DataType.
In the statement above, myObject is simply the name of the instanciated object
within the client program.
Objective #3: Use a vector class.
- Using the apvector
class instead of built-in C++ arrays is useful for a number
of reasons:
- It can be reused over and over again without having to rewrite or modify
specific array functions.
- The apvector class includes error-checking (especially bounds
checking which prevents one from accidentally overwriting other memory
locations.)
- You can use operator overloading with vectors (i.e. one-dimensional
arrays) with operators such as = and [ ] .
- You can pass apvectors in numerous ways with flexible copy constructors.
- Carefully read the apvector
class definition to become familiar with the member functions and overloaded
operators that can be used with vectors.
- To declare an apvector named games with 10 int elements, you use the following
statement:
apvector <int> games(10);
Notice the use of parentheses rather than square brackets, which are used
with built-in C++ arrays.
- To declare and initialize an apvector named games with 10 int elements and
initialize each element to the value 0, you use the following statement:
apvector <int> games(10, 0);
where the second parameter is the value that you wish to initialize the elements
to. Note that this declaration statement makes use of one of the second constructor
in the apvector class.
- You could declare an apvector of structures named
graph with the declaration statement after the following struct definition:
struct Point
{
int x;
int y;
};
apvector <Point> graph(5);
Objective #4: Be able to use parallel arrays or vectors when necessary.
- Sometimes, two arrays (or apvectors) can be used to tie two sets of data
together. For example, you can store 20 student ID numbers in a vector of
integers, while storing the grades of the same 20 students in a vector of
doubles. As long as the student grades are sorted in the same representative
order as the student ID numbers, one can use the two arrays as parallel
arrays. This allows the same index position to refer
to a given student's ID number and his/her grade by accessing the appropriate
elements of the two arrays (or vectors).
- Parallel arrays can be used to relate sets of variables of different data
types. For example, one array could store names (as strings) and its associated
parallel array could store test scores (as integers).