In our previous simple examples, we can note a difference between
first class data types such as int and second class data types such
as the string type char *. For example, we cannot assign character
constants directly to a character string outside of a declaration, and
instead must use operations like strcpy; the string representation
makes an assumption that the final character of every string is the
NIL character
, but the manipulations allowed on the
data structure do not ensure this. Further, the character array does
not know how many characters have been allocated to it. It is easy to
walk off the end of the array and attempt to write into invalid memory
locations. C-style strings cannot be used for template-sorting
algorithms since the comparison operator ``<'' compares the values of
the pointers, not the contents of the string.
What we would like to have is a better string which behaves much as an int, or a float. C++ provides the ability to extend the language features in this fashion through the use of classes. Classes can be thought of as object definitions where each object encapsulates a set of data and a number of routines defined to provide transparent access to the objects behavior.