// CSE 333 Su12 Lecture 11 demo: strtest.cc
// Perkins

// Test program for a simple string class.
// Demonstrates string class with constructors, destructors,
// and assignment.  Trace output from Str.cc will show when
// the various constructors and destructor are called.

#include <iostream>
using namespace std;

#include "Str.h"

// function prototypes
Str id(Str);
void printstr(Str&);

// print message to show progress
void here(int n) {
  cout << "main: " << n << endl;
}

// id(x) == x (tests copy construtor)
Str id(Str s) {
  return s;
}

// print the string s
// (s is a reference parameter to avoid making copies of it)
void printstr(Str &s) {
  char * cstring = s.c_str();
  cout << cstring << endl;
  delete [] cstring;
}

// create some strings and play with them
int main() {
  // local variables in main's stack frame
  // (destructor is called when main exits)
  Str s1, s2;            // null constructors
  Str hello("hello");    // c-string constructor
  Str howdy = hello;     // copy constructor

  here(1);

  // get c-string from Str
  char *hi = howdy.c_str();
  cout << hi << endl;
  delete [] hi;

  here(2);

  // test append (constructs a Str and destructs it)
  hello.append(" there");
  printstr(hello);
  cout << "  length is " << hello.length() << endl;

  here(3);

  // assignment operator and Str(char *) constructor
  s1 = "howdy";
  printstr(s1);

  here(4);

  // copy constructors
  s2 = id(hello);
  printstr(s2);

  here(5);

  // heap allocated Str object
  // (uses copy constructor)
  // (destructor called when object deleted)
  // (same general idea as a Java String, but in C++)
  Str * h = new Str(hello);
  h->append(" y'all");
  printstr(*h);
  cout << "  length is " << h->length() << endl;
  delete h;

  here(6);

  // Enough already!!
  return 0;
}