// p239.cpp - VString Class Example, p. 239 // This introduces the beginnings of a Variable-length string class. #include #include class VString { public: VString( unsigned len = 0 ); // Construct a VString of size . VString( const char * s ); // Construct a VString from a C-style string. ~VString(); // Destructor unsigned GetSize() const; // Return the number of characters. private: char * str; // character array unsigned size; // allocation size }; VString::VString( unsigned len ) { size = len; str = new char[ size+1 ]; str[0] = '\0'; } VString::VString( const char * s ) { size = strlen( s ); str = new char[ size+1 ]; strcpy( str, s ); } VString::~VString() { delete [] str; } unsigned VString::GetSize() const { return size; } int main() { VString zeroLen; VString emptyStr( 50 ); VString aName( "John Smith" ); cout << aName.GetSize() << endl; return 0; }