Homework #4

Your assignment this week is to extend your flString class again, but this time by deriving a new class from it.  You will need to make some changes to your flString class, but will also need to create a new class derived from  it.

 

New Class

Your new class will be called flNameString.  Like flString which it is derived from, flNameString is a string class.  The main difference is that flNameString will always insure that the string it contains is correctly formatted for case. 

 

For example, when we set an flNameString object with the text “bob smith”, when we go to retrieve it, it will be retrieved as “Bob Smith” (with the case modified).  In order to make this case correction work, we will add a Changed function to flNameString which will properly format the string in memory.  Whenever the string is changed, as with Set, Insert, Append, Delete, etc., then the Changed function will be called, which will properly format the string in memory.

 

The flNameString will be a very simple class to write, because most of the string manipulation is already done in the base class.  There is a specific approach we will implement which will require us to modify flString.  The approach is documented below.

 

Implementation Approach

We are going to modify the flString class to assist the flNameString class, and any future related classes to it.  We are going to implement a virtual Changed function in the flString class.  We will then have any code in flString that makes a change to the internal string call this Changed function, after the change is made.

 

The base class Changed function will do nothing.  It is assumed that classes derived from flString will implement the real function to do whatever they please, in terms of re-formatting the string.  Please note the following:

 

 

Functions

Functions inside flNameString are as follows:

 

flNameString( const char* Src=0 )

flNameString( const MGString& Src )

Constructor and copy constructor.  These function should use an Initializer List to invoke the desired base class constructor.  Since base class constructors can not call the virtual members of a derived class, you must call Changed yourself from within these constructors.

 

flNameString& operator=( const char* Src )

Assignment operators are not inherited.  This function should set the internal string using a base class member function.  Return value is this.

 

void Changed()

This function is called by the base class after a string has been modified.  You must alter the contents of the internal string so that the first letter of each word is capitalized.  You must not call a base member function to alter the internal string from this function.

 

Note: Keep the logic simple.  Do not worry about names like “NBC Television Network”, or “John McGee III, Ph.D.”

Changes to flString

·         Add the virtual Changed function.  It accepts and returns nothing.

·         In any code that alters a string, call Changed.

·         Make sure your destructor is declared virtual

 

 

 Sample Test Program

#include <iostream>

using namespace std;

 

#include <iostream>

using namespace std;

 

#include "MGNameString.h"

 

int main()

{

       MGNameString A = "tom jones";

       MGString * pB = new MGNameString;

 

       A.Insert( "a. ", 4 );

 

       cout << A << endl;

       pB->Set(A);

 

       MGString::m_Sensitive = true;

 

       if( (*pB)=="Tom A. Jones" )

              cout << "Excellent" << endl;

       else

              cout << "Boo!" << endl;

 

       if( (*pB)=="tom a. jones" )

              cout << "Boo!" << endl;

       else

              cout << "Excellent" << endl;

 

       delete pB;

 

       return( 0 );

}

Sample Output

Tom A. Jones

Excellent

Excellent