str1.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. // str1.cpp -- introducing the string class
  2. #include <iostream>
  3. #include <string>
  4. // using string constructors
  5. int main()
  6. {
  7. using namespace std;
  8. string one("Lottery Winner!"); // ctor #1
  9. cout << one << endl; // overloaded <<
  10. string two(20, '$'); // ctor #2
  11. cout << two << endl;
  12. string three(one); // ctor #3
  13. cout << three << endl;
  14. one += " Oops!"; // overloaded +=
  15. cout << one << endl;
  16. two = "Sorry! That was ";
  17. three[0] = 'P';
  18. string four; // ctor #4
  19. four = two + three; // overloaded +, =
  20. cout << four << endl;
  21. char alls[] = "All's well that ends well";
  22. string five(alls,20); // ctor #5
  23. cout << five << "!\n";
  24. string six(alls+6, alls + 10); // ctor #6
  25. cout << six << ", ";
  26. string seven(&five[6], &five[10]); // ctor #6 again
  27. cout << seven << "...\n";
  28. string eight(four, 7, 16); // ctor #7
  29. cout << eight << " in motion!" << endl;
  30. // std::cin.get();
  31. return 0;
  32. }