- Continued with array example.
- Version 2 declared a C++ class called myArray
in the header file:
myArray.h.
The member functions are implemented in:
myArray.cpp.
Note the existence of a constructor and a destructor.
Two main programs exercise the class:
array08.cpp and
array09.cpp.
- Version 3 adds an append() member function.
(See header file:
myArray.h.)
We now have to maintain, a capacity cap and
an length len. These are private data members.
In the implemtation
myArray.cpp,
note that the capacity of the array is doubled each
time that we run out of space. There are some examples of defensive
programming in this implementation.
Main programs to exercise Version 3:
array10.cpp,
array11.cpp and
array12.cpp (buggy!).
- Version 4 adds a copy constructor and an overloaded
assginment operator for myArray:
myArray.h and
myArray.cpp.
These two features make sure that a copy is made when a myArray
object is assigned, passed as a parameter or returned from a function.
Main programs to exercise Version 4:
array10.cpp,
array11.cpp,
array12.cpp and
array13.cpp.
- Version 5 adds overloaded [ ] (indexing) and
+ operators. It also has an overlaoded <<
operator outside the class. (See
myArray.h and
myArray.cpp.)
This allows us to use a myArray object more conveniently.
Main programs to exercise Version 5:
array10.cpp,
array14.cpp
array15.cpp
array16.cpp