/* * File: String-test2.cpp * Author: Robert I. Pitts * Last Modified: December 1, 1999 * Topic: A String Class: Part II - Lab * ---------------------------------------------------------------- * * This program demonstrates a basic String class that implements * dynamic allocation and operator overloading. * */ #include #include "mystring.h" /************************ Function Prototypes ************************/ /* * Function: PrintString * Usage: PrintString(str); * * Prints out the value and length of the String object passed to it. */ void PrintString(const char *label, const String &str); /* * Function: TestCopy * Usage: TestCopy(copy_str); * * Accepts a copy of a string (i.e., by value) to make sure the copy * constructor does what it is supposed to. */ void TestCopy(String copy_str); /*************************** Main Program **************************/ int main() { String str1, str2("init2"), str3 = "init3"; // Some String objects. char s1[100], s2[100], s3[100]; // Some character strings. // Print out their initial values... cout << "Initial values:" << endl; PrintString("str1", str1); PrintString("str2", str2); PrintString("str3", str3); // Store some values in them... cout << "\nEnter a value for str1 (no spaces): "; cin >> s1; str1 = s1; cout << "\nEnter a value for str2 (no spaces): "; cin >> s2; str2 = s2; cout << "\nEnter a value for str3 (no spaces): "; cin >> s3; str3 = s3; cout << "\nAfter assignments..." << endl; PrintString("str1", str1); PrintString("str2", str2); PrintString("str3", str3); // Access some elements... int i; cout << "\nEnter which element of str1 to display: "; cin >> i; cout << "Element #" << i << " of str1 is '" << str1[i] << "'" << endl; cout << "\nEnter which element of str2 to display: "; cin >> i; cout << "Element #" << i << " of str2 is '" << str2[i] << "'" << endl; cout << "\nEnter which element of str3 to display: "; cin >> i; cout << "Element #" << i << " of str3 is '" << str3[i] << "'" << endl; // Append some strings... cout << "\nEnter a value to append to str1 (no spaces): "; cin >> s1; str1.append(s1); cout << "\nEnter a value to append to str2 (no spaces): "; cin >> s2; str2.append(s2); cout << "\nEnter a value to append to str3 (no spaces): "; cin >> s3; str3.append(s3); cout << "\nAfter appending..." << endl; PrintString("str1", str1); PrintString("str2", str2); PrintString("str3", str3); // Compare some strings... cout << "\nComparing str1 and str2..." << endl; cout << "\""; str1.print(); cout << "\" is "; if (str1 < str2) { cout << "less than"; } else if (str1 > str2) { cout << "greater than"; } else { cout << "equal to"; } cout << " \""; str2.print(); cout << "\"" << endl; // Demonstrate the copy constructor. cout << "\nDemonstrating copy constructor..." << endl; String str4 = "original value"; cout << "Before call by value: "; PrintString("str4", str4); TestCopy(str4); cout << "After call by value: "; PrintString("str4", str4); return 0; } /*********************** Function Definitions **********************/ void PrintString(const char *label, const String &str) { cout << label << " holds \""; str.print(); cout << "\" (length = " << str.length() << ")" << endl; } void TestCopy(String copy_str) { // Change copy, leaving original alone. copy_str = "new value"; PrintString("copy_str", copy_str); }