Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using inheritance, derive two new classes from the WCS_String class:UpperCaseStr

ID: 3856443 • Letter: U

Question

Using inheritance, derive two new classes from the WCS_String class:UpperCaseString–This class will allow any characters to be stored but will store all alphabetic characters as upper case.DigitString–This class will allow only digits to be stored. Usage of other types of characters will cause an exception to be thrown.All error checking (bounds checking of indices, etc.) must be handled as exceptions (try, throw, catch). Your test program must demonstrate that all exception tests work. WCS_String.h

#ifndef WCS_STRING_H

#define WCS_STRING_H

#include <cctype>

#include <cstring>

#include <iostream>

using namespace std;

#pragma warning (disable:4996)

#ifdef WIN32

#define STRCMP_NO_CASE _strcmpi

#else

#define STRCMP_NO_CASE strcasecmp

#endif

class WCS_String

{

//************* Exceptions *****

public:

enum Exceptions {IndexOutOfBounds};

//************* Method Prototypes *****

public:

explicit WCS_String (const char * = "");

WCS_String (const WCS_String &) throw (...);

virtual ~WCS_String () throw ();

int Compare (const WCS_String &) const throw ();

int Compare (const char *) const throw ();

WCS_String & Concat (const WCS_String &) throw (...);

WCS_String & Concat (const char *) throw (...);

WCS_String & Copy (const WCS_String &) throw (...);

WCS_String & Copy (const char *) throw (...);

ostream & Display (ostream & = cout) const throw ();

bool GetAt (char &, int) const throw (...);

bool IsEmpty () const throw ();

size_t Length () const throw ();

istream & Read (istream & = cin) throw (...);

bool SetAt (char, int) throw (...);

WCS_String & ToLower () throw ();

WCS_String & ToUpper () throw ();

operator bool () const throw ();

operator const char * () const throw ();

WCS_String & operator = (const WCS_String &) throw (...);

WCS_String & operator = (const char *) throw (...);

bool operator < (const WCS_String &) const throw ();

bool operator < (const char *) const throw ();

bool operator <= (const WCS_String &) const throw ();

bool operator <= (const char *) const throw ();

bool operator == (const WCS_String &) const throw ();

bool operator == (const char *) const throw ();

bool operator >= (const WCS_String &) const throw ();

bool operator >= (const char *) const throw ();

bool operator > (const WCS_String &) const throw ();

bool operator > (const char *) const throw ();

bool operator != (const WCS_String &) const throw ();

bool operator != (const char *) const throw ();

WCS_String operator & (const WCS_String &) const throw (...);

WCS_String operator & (const char *) const throw (...);

WCS_String & operator &= (const WCS_String &) throw (...);

WCS_String & operator &= (const char *) throw (...);

char & operator [] (int) throw (...);

char operator [] (int) const throw (...);

private:

bool IsValidSubscript (int) const throw ();

void LocalCheckAndCopy (const char *) throw (...);

WCS_String & LocalConcat (const char *) throw (...);

void LocalCopy (const char *) throw (...);

WCS_String NewConcat (const char *) const throw (...);

static char ReadChar (istream & = cin) throw ();

operator char * () const throw ();

//************* Properties *****

private:

char * pChar;

size_t CharCount;

size_t MaxSize;

};

//************* Method Definitions *****

inline WCS_String::WCS_String (const char * p): CharCount (0), MaxSize (0), pChar (0)

{

LocalCopy (p);

}

inline WCS_String::WCS_String (const WCS_String & M)

{

LocalCopy (M.pChar);

}

inline int WCS_String::Compare (const WCS_String & M) const

{

return strcmp (pChar, M.pChar);

}

inline int WCS_String::Compare (const char * p) const

{

return strcmp (pChar, p);

}

inline WCS_String & WCS_String::Concat (const WCS_String & M)

{

return LocalConcat (M.pChar);

}

inline WCS_String & WCS_String::Concat (const char * p)

{

return LocalConcat (p);

}

inline WCS_String & WCS_String::Copy (const WCS_String & M)

{

return *this = M;

}

inline WCS_String & WCS_String::Copy (const char * p)

{

return *this = p;

}

inline ostream & WCS_String::Display (ostream & out) const

{

return out << pChar;

}

inline bool WCS_String::GetAt (char & c, int i) const

{

if (IsValidSubscript (i))

{

c = pChar [i];

return true;

}

else

return false;

}

inline bool WCS_String::IsEmpty () const

{

return Length () == 0;

}

inline bool WCS_String::IsValidSubscript (int i) const

{

return (i >= 0) && (i < static_cast <int> (CharCount));

}

inline size_t WCS_String::Length () const

{

return CharCount;

}

inline WCS_String WCS_String::NewConcat (const char * p) const

{

WCS_String S (*this);

S.Concat (p);

return S;

}

inline WCS_String::operator bool () const

{

return Length () > 0;

}

inline WCS_String::operator const char * () const

{

return pChar;

}

inline WCS_String & WCS_String::operator = (const WCS_String & M)

{

if (this != &M)

LocalCheckAndCopy (M.pChar);

else;

return *this;

}

inline WCS_String & WCS_String::operator = (const char * p)

{

LocalCheckAndCopy (p);

return *this;

}

inline bool WCS_String::operator < (const WCS_String & M) const

{

return Compare (M) < 0;

}

inline bool WCS_String::operator < (const char * p) const

{

return Compare (p) < 0;

}

inline bool operator < (const char * p, const WCS_String & S)

{

return S.Compare (p) > 0;

}

inline bool WCS_String::operator <= (const WCS_String & M) const

{

return Compare (M) <= 0;

}

inline bool WCS_String::operator <= (const char * p) const

{

return Compare (p) <= 0;

}

inline bool operator <= (const char * p, const WCS_String & S)

{

return S.Compare (p) >= 0;

}

inline bool WCS_String::operator == (const WCS_String & M) const

{

return Compare (M) == 0;

}

inline bool WCS_String::operator == (const char * p) const

{

return Compare (p) == 0;

}

inline bool operator == (const char * p, const WCS_String & S)

{

return S.Compare (p) == 0;

}

inline bool WCS_String::operator >= (const WCS_String & M) const

{

return Compare (M) >= 0;

}

inline bool WCS_String::operator >= (const char * p) const

{

return Compare (p) >= 0;

}

inline bool operator >= (const char * p, const WCS_String & S)

{

return S.Compare (p) <= 0;

}

inline bool WCS_String::operator > (const WCS_String & M) const

{

return Compare (M) > 0;

}

inline bool WCS_String::operator > (const char * p) const

{

return Compare (p) > 0;

}

inline bool operator > (const char * p, const WCS_String & S)

{

return S.Compare (p) < 0;

}

inline bool WCS_String::operator != (const WCS_String & M) const

{

return Compare (M) != 0;

}

inline bool WCS_String::operator != (const char * p) const

{

return Compare (p) != 0;

}

inline bool operator != (const char * p, const WCS_String & S)

{

return S.Compare (p) != 0;

}

inline WCS_String WCS_String::operator & (const WCS_String & M) const

{

return NewConcat (M.pChar);

}

inline WCS_String WCS_String::operator & (const char * p) const

{

return NewConcat (p);

}

inline WCS_String operator & (const char * p, const WCS_String & S)

{

WCS_String Temp (p);

return Temp.Concat (S);

}

inline WCS_String & WCS_String::operator &= (const WCS_String & M)

{

return Concat (M);

}

inline WCS_String & WCS_String::operator &= (const char * p)

{

return Concat (p);

}

inline char & WCS_String::operator [] (int i)

{

if (IsValidSubscript (i))

return pChar [i];

else

throw IndexOutOfBounds;

}

inline char WCS_String::operator [] (int i) const

{

return (*const_cast <WCS_String *> (this)).operator [] (i);

}

inline bool WCS_String::SetAt (char c, int i)

{

if ((i >= 0) && (i < static_cast <int> (CharCount)))

{

pChar [i] = c;

return true;

}

else

return false;

}

inline ostream & operator << (ostream & out, const WCS_String & M)

{

return M.Display (out);

}

inline istream & operator >> (istream & in, WCS_String & M)

{

return M.Read (in);

}

#pragma warning (default:4996)

#endif

Explanation / Answer

Main.cpp
-----------------------------------------
#include <iostream>

using namespace std;

#include "UpperCaseString.h"
#include "WCS_String.h"
#include "DigitString.h"

int main () {
    UpperCaseString LC1(WCS_String("abcd"));
    UpperCaseString LC2(WCS_String("WXYZ"));

    cout << " Testing the UpperCaseString - the following should be uppercase: " << endl;
    LC1.Display();

    cout << " Testing a the DigitString class using try/throw/catch: " << endl;

    try {
        cout << " Testing a bad DigitString object: " << endl;
        DigitString DC1(WCS_String("123abc"));
        DC1.Display();

        cout << " Testing a good DigitString object: " << endl;
        DigitString DC2(WCS_String("123456"));
        DC1.Display();

    }
    catch (DigitString::Exceptions e) {
        switch (e) {
            case DigitString::IndexOutOfBounds:
                cout << "Error: index out of bounds" << endl;
                break;
            case DigitString::NotDigits:
                cout << "Error: something other than digits assigned to the DigitString class." << endl;
                break;
            default:
                cout << "Internal Error - unknown exception" << endl;
        } // END SWITCH
    } // END TRY

    try {
        cout << " Testing a good DigitString object: " << endl;
        DigitString DC2(WCS_String("123456"));
        DC2.Display();

    }
    catch (DigitString::Exceptions e) {
        switch (e) {
            case DigitString::IndexOutOfBounds:
                cout << "Error: index out of bounds" << endl;
                break;
            case DigitString::NotDigits:
                cout << "Error: something other than digits assigned to the DigitString class." << endl;
                break;
            default:
                cout << "Internal Error - unknown exception" << endl;
        } // END SWITCH
    } // END CATCH 2

    try {
        cout << " Testing = operator for DigitString object: " << endl;
        // good copy
        DigitString DC4(WCS_String("123456"));
        DC4 = WCS_String("111");
        DC4.Display();

        // bad copy
        DigitString DC3(WCS_String("123456"));
        DC3 = WCS_String("aaa");
    }
    catch (DigitString::Exceptions e) {
        switch (e) {
            case DigitString::IndexOutOfBounds:
                cout << "Error: index out of bounds" << endl;
                break;
            case DigitString::NotDigits:
                cout << "Error: something other than digits assigned to the DigitString class." << endl;
                break;
            default:
                cout << "Internal Error - unknown exception" << endl;
        } // END SWITCH
    } // END CATCH 3

    try {
        cout << " Testing copy method for DigitString object: " << endl;
        // good copy
        DigitString DC5(WCS_String("123456"));
        DC5.Copy(WCS_String("111"));
        DC5.Display();

        // bad copy
        DigitString DC6(WCS_String("123456"));
        DC6.Copy(WCS_String("aaa"));
    }
    catch (DigitString::Exceptions e) {
        switch (e) {
            case DigitString::IndexOutOfBounds:
                cout << "Error: index out of bounds" << endl;
                break;
            case DigitString::NotDigits:
                cout << "Error: something other than digits assigned to the DigitString class." << endl;
                break;
            default:
                cout << "Internal Error - unknown exception" << endl;
        } // END SWITCH
    } // END CATCH 3

    try {
        cout << " Testing concat method for WCS_String object: " << endl;
        DigitString DC5(WCS_String("123456"));

        // good concat
        DC5.Concat(WCS_String("999"));
        DC5.Display();
        // bad concat
        DC5.Concat(WCS_String("abc"));
        DC5.Display();
    }
    catch (DigitString::Exceptions e) {
        switch (e) {
            case DigitString::IndexOutOfBounds:
                cout << "Error: index out of bounds" << endl;
                break;
            case DigitString::NotDigits:
                cout << "Error: something other than digits assigned to the DigitString class." << endl;
                break;
            default:
                cout << "Internal Error - unknown exception" << endl;
        } // END SWITCH
    } // END CATCH 3

    try {
        cout << " Testing concat method for DigitString object: " << endl;
        DigitString DC5(WCS_String("123456"));
        DigitString DC6(WCS_String("789"));


        // good concat
        DC5.Concat(DC6);
        DC5.Display();

    }
    catch (DigitString::Exceptions e) {
        switch (e) {
            case DigitString::IndexOutOfBounds:
                cout << "Error: index out of bounds" << endl;
                break;
            case DigitString::NotDigits:
                cout << "Error: something other than digits assigned to the DigitString class." << endl;
                break;
            default:
                cout << "Internal Error - unknown exception" << endl;
        } // END SWITCH
    } // END CATCH 3

    try {
        cout << " Testing concat method for char array: " << endl;
        DigitString DC5(WCS_String("123456"));
        // good concat
        DC5.Concat("789101112");
        DC5.Display();
        // bad concat
        DC5.Concat("abc");
        DC5.Display();

    }
    catch (DigitString::Exceptions e) {
        switch (e) {
            case DigitString::IndexOutOfBounds:
                cout << "Error: index out of bounds" << endl;
                break;
            case DigitString::NotDigits:
                cout << "Error: something other than digits assigned to the DigitString class." << endl;
                break;
            default:
                cout << "Internal Error - unknown exception" << endl;
        } // END SWITCH
    } // END CATCH 3

    try {
        cout << " Testing concat overloaded op: " << endl;
        DigitString DC5(WCS_String("123456"));
        // good concat
        DC5 &= "test";
        DC5.Display();

    }
    catch (DigitString::Exceptions e) {
        switch (e) {
            case DigitString::IndexOutOfBounds:
                cout << "Error: index out of bounds" << endl;
                break;
            case DigitString::NotDigits:
                cout << "Error: something other than digits assigned to the DigitString class." << endl;
                break;
            default:
                cout << "Internal Error - unknown exception" << endl;
        } // END SWITCH
    } // END CATCH 3

    try {
        DigitString DC5;
        cout << " Testing read method - enter whatever you want: " << endl;
        DC5.Read();
        // good concat
        DC5.Display();

    }
    catch (DigitString::Exceptions e) {
        switch (e) {
            case DigitString::IndexOutOfBounds:
                cout << "Error: index out of bounds" << endl;
                break;
            case DigitString::NotDigits:
                cout << "Error: something other than digits assigned to the DigitString class." << endl;
                break;
            default:
                cout << "Internal Error - unknown exception" << endl;
        } // END SWITCH
    } // END CATCH 3

    try {
        DigitString DC5;
        cout << " Testing concat method in UpperCaseString " << endl;
        UpperCaseString LC1(WCS_String("abcd"));
        UpperCaseString LC2(WCS_String("WXYZ"));
        LC2.Concat(LC1);
        LC2.Display();
        LC2.Concat("this is more text");
        LC2.Display();


    }
    catch (DigitString::Exceptions e) {
        switch (e) {
            case DigitString::IndexOutOfBounds:
                cout << "Error: index out of bounds" << endl;
                break;
            case DigitString::NotDigits:
                cout << "Error: something other than digits assigned to the DigitString class." << endl;
                break;
            default:
                cout << "Internal Error - unknown exception" << endl;
        } // END SWITCH
    } // END CATCH 3

    try {
        DigitString DC5;
        cout << " Testing read method in UpperCaseString - type in some text " << endl;
        UpperCaseString LC17;
        LC17.Read();
        LC17.Display();

    }
    catch (DigitString::Exceptions e) {
        switch (e) {
            case DigitString::IndexOutOfBounds:
                cout << "Error: index out of bounds" << endl;
                break;
            case DigitString::NotDigits:
                cout << "Error: something other than digits assigned to the DigitString class." << endl;
                break;
            default:
                cout << "Internal Error - unknown exception" << endl;
        } // END SWITCH
    } // END CATCH 3

} // end main
-------------------------------------------------------------------------------------------
DigitString.cpp
---------------------------------------------
#include "DigitString.h"

#ifndef   STRING_SIZE
#define   STRING_SIZE   50
#endif

DigitString::DigitString ()
{
}

DigitString::DigitString (const DigitString & Str): WCS_String (Str)
{
}

DigitString::DigitString (const WCS_String & Str): WCS_String (Str)
{
    for (int i = 0; i < strlen(Str); i++) {
        if (!isdigit(Str[i])) {
            throw NotDigits;
        }
    }
    WCS_String::Copy (Str);
}


DigitString::~DigitString ()
{
}

char DigitString::ReadChar (istream & in)
{
    return static_cast <char> (in.get ());
}

istream & DigitString::Read (istream & In)
{
    char           c;
    char *           pTemp;

    CharCount = 0;
    while (((c = ReadChar (In)) != ' ') && !In.eof ())
    {
        pChar [CharCount++] = c;
        if (CharCount >= MaxSize)
        {
            pTemp = new char [(MaxSize += STRING_SIZE) + 1];
            memcpy (pTemp, pChar, CharCount);
            delete [] pChar;
            pChar = pTemp;
        }
        else;
    }
    pChar [CharCount] = '';
    for (int i = 0; i < strlen(pChar); i++) {
        if (!isdigit(pChar[i])) {
            throw NotDigits;
        } else {
            return In;
        }
    }

}
-------------------------------------------------------------------------------------------
DigitString.h
-----------------------------------------------#ifndef DIGITSTRING_H
#define DIGITSTRING_H

#include <iostream>
#include <stdlib.h>
#include <ctype.h>
#include "WCS_String.h"

using namespace std;

class DigitString: public WCS_String
{
public:
    enum Exceptions {IndexOutOfBounds, NotDigits};

    DigitString       ();
    DigitString       (const DigitString &);
    DigitString       (const WCS_String &);
    ~DigitString   ();
    DigitString &   Concat                   (const DigitString &);
    DigitString &   Concat                   (const WCS_String &);
    DigitString &   Concat                   (const char *);
    DigitString &   Copy               (const DigitString &);
    DigitString &   Copy               (const WCS_String &);
    DigitString &   operator =           (const DigitString &);
    DigitString &   operator =           (const WCS_String &);
    DigitString &   operator &=               (const WCS_String &);
    DigitString &   operator &=               (const DigitString &);
    DigitString &   operator &=               (const char *);
    DigitString &   LocalConcat               (const char *);
    istream &       Read                   (istream & = cin);


private:
    static   char           ReadChar               (istream & = cin);


};

inline DigitString & DigitString::Copy (const DigitString & Str)
{
    return operator = (Str);
}

inline DigitString & DigitString::Copy (const WCS_String & Str)
{
    return operator = (Str);
}

inline DigitString & DigitString::operator = (const DigitString & Str)
{
    WCS_String::Copy (Str);
    return *this;
}

inline DigitString & DigitString::operator = (const WCS_String & Str)
{
    for (int i = 0; i < strlen(Str); i++) {
        if (!isdigit(Str[i])) {
            throw NotDigits;
        }
    }
    WCS_String::Copy (Str);
}

inline DigitString & DigitString::Concat (const WCS_String & Str)
{
    for (int i = 0; i < strlen(Str); i++) {
        if (!isdigit(Str[i])) {
            throw NotDigits;
        }
    }
    WCS_String::Concat (Str);

}

inline DigitString & DigitString::Concat (const DigitString & Str)
{
    WCS_String::Concat (Str);
}

inline DigitString & DigitString::Concat (const char * Str)
{
    for (int i = 0; i < strlen(Str); i++) {
        if (!isdigit(Str[i])) {
            throw NotDigits;
        }
    }
    WCS_String::Concat (Str);
}

inline DigitString &   DigitString::operator &=   (const WCS_String & M)
{
    return Concat (M);
}

inline DigitString &   DigitString::operator &=   (const DigitString & M)
{
    return Concat (M);
}
inline DigitString &   DigitString::operator &=   (const char * p)
{
    return Concat (p);
}


#endif
-------------------------------------------------------------------------------------------
UpperCaseString.cpp
-----------------------------------------------
#include "UpperCaseString.h"
#ifndef   STRING_SIZE
#define   STRING_SIZE   50
#endif

UpperCaseString::UpperCaseString ()
   {
   }

UpperCaseString::UpperCaseString (const UpperCaseString & Str): WCS_String (Str)
   {
   }

UpperCaseString::UpperCaseString (const WCS_String & Str): WCS_String (Str)
   {
   ToUpper ();
   }

UpperCaseString::~UpperCaseString ()
   {
   }
UpperCaseString & UpperCaseString::LocalConcat (const char * p)
{
    CharCount += strlen (p);
    if (MaxSize < CharCount)
    {
        char * pTemp = new char [CharCount + 1];
        strcpy (pTemp, pChar);
        strcat (pTemp, p);
        delete [] pChar;
        pChar   = pTemp;
        MaxSize   = CharCount;
    }
    else
    {
        strncat (pChar, p, strlen (p));
        pChar [CharCount] = '';
    }
    ToUpper();
    return *this;
}

istream & UpperCaseString::Read (istream & In)
{
    char           c;
    char *           pTemp;

    CharCount = 0;
    while (((c = ReadChar (In)) != ' ') && !In.eof ())
    {
        pChar [CharCount++] = c;
        if (CharCount >= MaxSize)
        {
            pTemp = new char [(MaxSize += STRING_SIZE) + 1];
            memcpy (pTemp, pChar, CharCount);
            delete [] pChar;
            pChar = pTemp;
        }
        else;
    }
    pChar [CharCount] = '';
    ToUpper();

    return In;
    }
char UpperCaseString::ReadChar (istream & in)
{
    return static_cast <char> (in.get ());
}
-------------------------------------------------------------------------------------------
UpperCaseString.h
----------------------------------------------
#ifndef LOWER_CASE_STRING_H
#define LOWER_CASE_STRING_H

#include "WCS_String.h"

class UpperCaseString: public WCS_String
   {
   public:
                           UpperCaseString       ();
                           UpperCaseString       (const UpperCaseString &);
                           UpperCaseString       (const WCS_String &);
                           ~UpperCaseString   ();
       UpperCaseString &   Copy               (const UpperCaseString &);
       UpperCaseString &   Copy               (const WCS_String &);
        UpperCaseString &   Concat               (const UpperCaseString &);
        UpperCaseString &   Concat               (const WCS_String &);
        UpperCaseString &   Concat               (const char *);
        UpperCaseString &   LocalConcat           (const char *);
        UpperCaseString &   operator =           (const UpperCaseString &);
       UpperCaseString &   operator =           (const WCS_String &);
        istream &       Read                   (istream & = cin);

private:
    static   char           ReadChar               (istream & = cin);

};

inline UpperCaseString & UpperCaseString::Copy (const UpperCaseString & Str)
   {
   return operator = (Str);
   }

inline UpperCaseString & UpperCaseString::Copy (const WCS_String & Str)
   {
   return operator = (Str);
   }

inline UpperCaseString & UpperCaseString::operator = (const UpperCaseString & Str)
   {
   WCS_String::Copy (Str);
   return *this;
   }

inline UpperCaseString & UpperCaseString::operator = (const WCS_String & Str)
   {
   WCS_String::Copy (Str);
   ToUpper ();
   return *this;
   }

inline UpperCaseString & UpperCaseString::Concat (const UpperCaseString & Str)
{
    WCS_String::Concat (Str);
}

inline UpperCaseString & UpperCaseString::Concat (const WCS_String & Str)
{
    WCS_String::Concat (Str);
    WCS_String::ToUpper();

}

inline UpperCaseString & UpperCaseString::Concat (const char * Str)
{
    WCS_String::Concat (Str);
    WCS_String::ToUpper();

}

#endif
-------------------------------------------------------------------------------------------
WCS_String.cpp
-----------------------------------------------
#include "WCS_String.h"

#ifndef   STRING_SIZE
#define   STRING_SIZE   50
#endif

WCS_String::~WCS_String ()
{
    delete [] pChar;
}

#pragma warning (disable:4996)

istream & WCS_String::Read (istream & In)
{
    char           c;
    char *           pTemp;

    CharCount = 0;
    while (((c = ReadChar (In)) != ' ') && !In.eof ())
    {
        pChar [CharCount++] = c;
        if (CharCount >= MaxSize)
        {
            pTemp = new char [(MaxSize += STRING_SIZE) + 1];
            memcpy (pTemp, pChar, CharCount);
            delete [] pChar;
            pChar = pTemp;
        }
        else;
    }
    pChar [CharCount] = '';
    return In;
}

char WCS_String::ReadChar (istream & in)
{
    return static_cast <char> (in.get ());
}

WCS_String & WCS_String::LocalConcat (const char * p)
{
    CharCount += strlen (p);
    if (MaxSize < CharCount)
    {
        char * pTemp = new char [CharCount + 1];
        strcpy (pTemp, pChar);
        strcat (pTemp, p);
        delete [] pChar;
        pChar   = pTemp;
        MaxSize   = CharCount;
    }
    else
    {
        strncat (pChar, p, strlen (p));
        pChar [CharCount] = '';
    }
    return *this;
}

void WCS_String::LocalCheckAndCopy (const char * p)
{
    CharCount = strlen (p);
    if (CharCount > MaxSize)
    {
        delete [] pChar;
        MaxSize = CharCount;
        pChar = new char [CharCount + 1];
    }
    else;
    strcpy (pChar, p);
}

void WCS_String::LocalCopy (const char * p)
{
    CharCount   = strlen (p);
    MaxSize       = CharCount;
    pChar       = new char [CharCount + 1];
    strcpy (pChar, p);
}

WCS_String & WCS_String::ToLower ()
{
    for (int i = 0; pChar [i]; i++)
        pChar [i] = static_cast <char> (tolower (pChar [i]));
    return *this;
}

WCS_String & WCS_String::ToUpper ()
{
    for (int i = 0; pChar [i]; i++)
        pChar [i] = static_cast <char> (toupper (pChar [i]));
    return *this;
}

#pragma warning (default:4996)
-------------------------------------------------------------------------------------------
WCS_String.h
-----------------------------------------------
#ifndef WCS_STRING_H
#define WCS_STRING_H

#include <cctype>
#include <cstring>
#include <iostream>

using namespace std;

#pragma warning (disable:4996)

#ifdef WIN32
#define STRCMP_NO_CASE _strcmpi
#else
#define STRCMP_NO_CASE strcasecmp
#endif

class WCS_String
{
//************* Exceptions *****
public:
    enum       Exceptions   {IndexOutOfBounds};
//************* Method Prototypes *****
public:
    explicit WCS_String(const char * = "");
    WCS_String(const WCS_String &);
    virtual~WCS_String();
    int   Compare   (const WCS_String &)const;
    int   Compare   (const char *)const;
    WCS_String & Concat(const WCS_String &);
    WCS_String & Concat(const char *);
    WCS_String & Copy(const WCS_String &);
    WCS_String & Copy(const char *);
    ostream & Display(ostream & = cout)const;
    bool GetAt(char &, int)const;
    bool IsEmpty()
    const;
    size_t Length()const;
    istream & Read(istream & = cin);
    bool SetAt(char, int);
    WCS_String & ToLower();
    WCS_String & ToUpper();
    operator bool()const;
    operator const char *()const;
    WCS_String & operator = (const WCS_String &);
    WCS_String & operator = (const char *);
    bool           operator <               (const WCS_String &) const;
    bool           operator <               (const char *) const;
    bool           operator <=               (const WCS_String &) const;
    bool           operator <=               (const char *) const;
    bool           operator ==               (const WCS_String &) const;
    bool           operator ==               (const char *) const;
    bool           operator >=               (const WCS_String &) const;
    bool           operator >=               (const char *) const;
    bool           operator >               (const WCS_String &) const;
    bool           operator >               (const char *) const;
    bool           operator !=               (const WCS_String &) const;
    bool           operator !=               (const char *) const;
    WCS_String       operator &               (const WCS_String &) const;
    WCS_String       operator &               (const char *) const;
    WCS_String &   operator &=               (const WCS_String &);
    WCS_String &   operator &=               (const char *);
    char &           operator []               (int);
    char           operator []               (int)                   const;
private:
    bool           IsValidSubscript       (int)                   const;
    void           LocalCheckAndCopy       (const char *);
    WCS_String &   LocalConcat               (const char *);
    void           LocalCopy               (const char *);
    WCS_String       NewConcat               (const char *)           const;
    static   char           ReadChar               (istream & = cin);
    operator char *           ()                       const;
//************* Properties *****
protected:
    size_t   CharCount;
    size_t   MaxSize;
    char *   pChar;
};

//************* Method Definitions *****

inline WCS_String::WCS_String (const char * p): CharCount (0), MaxSize (0), pChar (0)
{
    LocalCopy (p);
}

inline WCS_String::WCS_String (const WCS_String & M)
{
    LocalCopy (M.pChar);
}

inline int WCS_String::Compare (const WCS_String & M) const
{
    return STRCMP_NO_CASE (pChar, M.pChar);
}

inline int WCS_String::Compare (const char * p) const
{
    return STRCMP_NO_CASE (pChar, p);
}

inline WCS_String & WCS_String::Concat (const WCS_String & M)
{
    return LocalConcat (M.pChar);
}

inline WCS_String & WCS_String::Concat (const char * p)
{
    return LocalConcat (p);
}

inline WCS_String & WCS_String::Copy (const WCS_String & M)
{
    return *this = M;
}

inline WCS_String & WCS_String::Copy (const char * p)
{
    return *this = p;
}

inline ostream & WCS_String::Display (ostream & out) const
{
    return out << pChar;
}

inline bool WCS_String::GetAt (char & c, int i) const
{
    if (IsValidSubscript (i))
    {
        c = pChar [i];
        return true;
    }
    else
        return false;
}

inline bool WCS_String::IsEmpty () const
{
    return Length () == 0;
}

inline bool WCS_String::IsValidSubscript (int i) const
{
    return (i >= 0) && (i < static_cast <int> (CharCount));
}

inline size_t WCS_String::Length () const
{
    return CharCount;
}

inline WCS_String WCS_String::NewConcat (const char * p) const
{
    WCS_String S (*this);
    S.Concat (p);
    return S;
}

inline WCS_String::operator bool () const
{
    return Length () > 0;
}

inline WCS_String::operator const char * () const
{
    return pChar;
}

inline WCS_String & WCS_String::operator = (const WCS_String & M)
{
    if (this != &M)
        LocalCheckAndCopy (M.pChar);
    else;
    return *this;
}

inline WCS_String & WCS_String::operator = (const char * p)
{
    LocalCheckAndCopy (p);
    return *this;
}

inline bool WCS_String::operator <   (const WCS_String & M) const
{
    return Compare (M) < 0;
}

inline bool WCS_String::operator <   (const char * p) const
{
    return Compare (p) < 0;
}

inline bool operator <   (const char * p, const WCS_String & S)
{
    return S.Compare (p) > 0;
}

inline bool WCS_String::operator <=   (const WCS_String & M) const
{
    return Compare (M) <= 0;
}

inline bool WCS_String::operator <=   (const char * p) const
{
    return Compare (p) <= 0;
}

inline bool operator <=   (const char * p, const WCS_String & S)
{
    return S.Compare (p) >= 0;
}

inline bool WCS_String::operator ==   (const WCS_String & M) const
{
    return Compare (M) == 0;
}

inline bool WCS_String::operator ==   (const char * p) const
{
    return Compare (p) == 0;
}

inline bool operator ==   (const char * p, const WCS_String & S)
{
    return S.Compare (p) == 0;
}

inline bool WCS_String::operator >=   (const WCS_String & M) const
{
    return Compare (M) >= 0;
}

inline bool WCS_String::operator >=   (const char * p) const
{
    return Compare (p) >= 0;
}

inline bool operator >=   (const char * p, const WCS_String & S)
{
    return S.Compare (p) <= 0;
}

inline bool WCS_String::operator >   (const WCS_String & M) const
{
    return Compare (M) > 0;
}

inline bool WCS_String::operator >   (const char * p) const
{
    return Compare (p) > 0;
}

inline bool operator >   (const char * p, const WCS_String & S)
{
    return S.Compare (p) < 0;
}

inline bool WCS_String::operator !=   (const WCS_String & M) const
{
    return Compare (M) != 0;
}

inline bool WCS_String::operator !=   (const char * p) const
{
    return Compare (p) != 0;
}

inline bool operator !=   (const char * p, const WCS_String & S)
{
    return S.Compare (p) != 0;
}

inline WCS_String WCS_String::operator & (const WCS_String & M) const
{
    return NewConcat (M.pChar);
}

inline WCS_String WCS_String::operator & (const char * p) const
{
    return NewConcat (p);
}

inline WCS_String operator & (const char * p, const WCS_String & S)
{
    WCS_String Temp (p);
    return Temp.Concat (S);
}

inline WCS_String &   WCS_String::operator &=   (const WCS_String & M)
{
    return Concat (M);
}

inline WCS_String &   WCS_String::operator &=   (const char * p)
{
    return Concat (p);
}

inline char & WCS_String::operator [] (int i)
{
    if (IsValidSubscript (i))
        return pChar [i];
    else
        throw IndexOutOfBounds;
}

inline char WCS_String::operator [] (int i) const
{
    return (*const_cast <WCS_String *> (this)).operator [] (i);
}

inline bool WCS_String::SetAt (char c, int i)
{
    if ((i >= 0) && (i < static_cast <int> (CharCount)))
    {
        pChar [i] = c;
        return true;
    }
    else
        return false;
}

inline ostream & operator << (ostream & out, const WCS_String & M)
{
    return M.Display (out);
}

inline istream & operator >> (istream & in, WCS_String & M)
{
    return M.Read (in);
}

#pragma warning (default:4996)

#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote