Each modern programming language provides a way of expressing character strings. For instance, in C language, there is no built-in data type for strings, but instead, they appear as null-terminated character arrays. However, there is a library named string.h that includes several predefined string-handling functions. In C++, on the other hand, there is no built-in string object, however, there is a class named string in the standard C++ library that gives way better type-safe and more functionalities compared to the C library.
Thus, in this programming assignment, students are expected to study the standard C library, namely, the string.h, and convert it to a fully-operating class. To clarify the task, let me give a simple example as in the sequel.
In C, one can write a simple two-line code to present how a string stored in one array can be copied into another one.
char s1[] = “a string”, s2[10]; strcpy(s2, s1);
Note that in the example given above, we consider the standard C library function strcpy as safe. It is the Visual Studio environment that gives the alternative version strcpy_s for it with the claim that theirs better and safer.
In an attempt to convert those lines into the object-oriented way, one can rewrite it as below:
Cstring s1(“a string”), s2; s2.strcpy(s1);
List of functions to be included into the class declaration are strlen, strcpy, strncpy, strcat, strncat, strcmp, strncmp, strchr, strrchr, strstr, and strtok. List of operators that can be overloaded for the class are the assignment, addition, shorthand assignment, is equal to, is not equal to, is greater than, is greater than or at least equal to, is less than, and is less than or at least equal to. All member functions and operators listed should work without any problems. It is the students responsibility to visit, study, try, and learn the string.h library.
The instance of Cstring class should be a dynamic object compared to the classical C-style compile-time fixed character array!
As bonus, for those finding the assignment simple, students may try to make all listed operators/functions to work with C/C++ strings. For instance,
char s1[] = “a string”; Cstring s2; s2.strcpy(s1);
or
should also work!
Students should develop an interface file, an implementation file, and a driver program to prove all functionalities included in their class declaration are working without any side-effects!
CString class
July 15th, 2020