int main() { const MyString cs("Ein konstanter String"); MyString s(cs); s.assign(cs); s.append(cs); cout << cs.c_str() << endl; cout << cs.size() << endl; cout << cs.capacity() << endl; cout << boolalpha << cs.empty() << endl; s = cs + cs; cout << (cs == cs) << endl; s = cs; cout << cs << endl; s.at(1) = 'X'; s[2] = 'Y'; // Hallo cout << s << endl; cin.get(); return 0; }
int main() { cout << "Testing default constructor\n\n"; const MyString s1; cout << "s1: " << s1 << endl; cout << "s1 size: " << s1.size() << endl; cout << "s1 capacity: " << s1.capacity() << endl; cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n"); cout << endl; cout << "Testing second constructor\n\n"; MyString s2 = "some text"; cout << "s2: " << s2 << endl; cout << "s2 size: " << s2.size() << endl; cout << "s2 capacity: " << s2.capacity() << endl; cout << "s2 is " << ((s2.empty()) ? "empty\n" : "not empty\n"); cout << endl; cout << "Testing second constructor with long string\n\n"; MyString s3 = "This is a really long string and this time all of it will actually end up in the array - pretty neat, huh?"; cout << "s3: " << s3 << endl; cout << "s3 size: " << s3.size() << endl; cout << "s3 capacity: " << s3.capacity() << endl; cout << endl; cout << "Testing write form of subscript operator\n\n"; s2[0] = 'S'; s2[5] = 'T'; cout << "s2: " << s2 << endl << endl; cout << "Testing read form of subscript operator\n\n"; cout << "s2: "; for (unsigned int i = 0; i < s2.size(); i++) cout << s2[i]; cout << endl << endl; cout << "Testing equality operators\n\n"; const MyString s4 = "Some Text"; cout << "s2 and s4 are " << ((s2 == s4) ? "equal\n" : "not equal\n"); cout << "s3 and s4 are " << ((s3 == s4) ? "equal\n" : "not equal\n\n"); cout << "s4 and \"Some Text\" are " << ((s4 == "Some Text") ? "equal\n" : "not equal\n"); cout << "s4 and \"More Text\" are " << ((s4 == "More Text") ? "equal\n" : "not equal\n\n"); cout << "\"Some Text\" and s4 are " << (("Some Text" == s4) ? "equal\n" : "not equal\n"); cout << "\"More Text\" and s4 are " << (("More Text" == s4) ? "equal\n" : "not equal\n\n"); cout << "Testing clear() method\n\n"; s3.clear(); cout << "s3: " << s3 << endl; cout << "s3 size: " << s3.size() << endl; cout << "s3 capacity: " << s3.capacity() << endl; cout << "s3 is " << ((s3.empty()) ? "empty\n" : "not empty\n"); cout << "Testing copy constructor\n\n"; MyString s5(s4); // Problem cout << "s5: " << s5 << endl; cout << "s5 size: " << s5.size() << endl; cout << "s5 capacity: " << s5.capacity() << endl; cout << endl; cout << "Testing assignment operator\n\n"; s3 = s5; cout << "s3: " << s3 << endl; cout << "s3 size: " << s3.size() << endl; cout << "s3 capacity: " << s3.capacity() << endl; cout << endl; s3 = "a different string"; cout << "s3: " << s3 << endl; cout << "s3 size: " << s3.size() << endl; cout << "s3 capacity: " << s3.capacity() << endl; cout << endl; cout << "Testing self-assignment\n\n"; s3 = s3; cout << "s3: " << s3 << endl; cout << "s3 size: " << s3.size() << endl; cout << "s3 capacity: " << s3.capacity() << endl; cout << endl; cout << "Testing chained assignment\n\n"; s3 = s2 = "Hello, world"; cout << "s2: " << s2 << endl; cout << "s2 size: " << s2.size() << endl; cout << "s2 capacity: " << s2.capacity() << endl; cout << endl; cout << "s3: " << s3 << endl; cout << "s3 size: " << s3.size() << endl; cout << "s3 capacity: " << s3.capacity() << endl; return 0; }