Presented below is a simple version of 'Hello World' that uses the
Str class. Although not impressive, it is a good first step in
getting comfortable with Str. Here is the C-style I/O version:
#include "Str.hpp"
#include <stdio.h>
int main(int argc, char* argv[]) {
// Create a string on the Heap and print it
Str s = "Hello World!";
printf("s = %s\n", (const char*)s);
// Set it equal to something and print it again
s = "Goodbye!";
printf("now s = %s\n", (const char*)s);
return 0;
}
And here is the same version that uses C++-style I/O:
#include "Str.hpp"
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
// Create a string on the Heap and print it
Str s = "Hello World!";
cout << "s = " << s << endl;
// Set it equal to something and print it again
s = "Goodbye!";
cout << "now s = " << s << endl;
return 0;
}
Look at your favorite version and we'll review some concepts:
s' is created on stack.
s' for the string data, and "Hello World!" plus a NULL is copied to
this heap memory.
(const char*) conversion feature of
Str to give printf the char* type it can handle.
operator<< feature of Str
to work nicely with cout
s = "Goodbye!" copies the char* string
"Goodbye!" over top of "Hello World!".
Str::~Str() destructor is automatically
invoked when main() exits. This automatically frees the 12 bytes of
heap data that was initially created.