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

NEED HELP WITH C++ Using inheritance, derive two new classes from the WCS_String

ID: 3695754 • Letter: N

Question

NEED HELP WITH C++

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.

Create all members or friend functions deemed necessary for proper functioning of

your classes.

Explanation / Answer

Solution: Please follow these as shown in below..

#include <WCS_String.h>

#include <iostream>

using namespace std;

class UpperCaseString: public WCS_String

{

public:

    UpperCaseString();

    UpperCaseString(const UpperCaseString &);

    UpperCaseString(const WCS_String &);

   ~UpperCaseString();

    istream & Read(istream & = cin);

private:

    UpperCaseString &ToLower();

};

inline istream & operator>>(istream & in, UpperCaseString & UC)

{

    UC.Read(in);

}

UpperCaseString::UpperCaseString() { }

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

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

{

    ToUpper();

}

UpperCaseString::~UpperCaseString() { }

istream & UpperCaseString::Read(istream & in)

{

    WCS_String::Read(in);

    ToUpper();

    return in;

}

int main()

{

    WCS_String       Str1("Test1");

    UpperCaseString UC1(Str1);   // if I changed this to UpperCaseString UC1, it    still works.

    UpperCaseString UC2(UC1);

    UpperCaseString UC3(Str1);

    UpperCaseString UC4(WCS_String ("Test2"));

    UC1 = WCS_String("Test2"); // This works even I don't have assignment operator to assign base class object to derived class object

    UC1 = UC4;   // This works since there will be an implicit defined assignment   operator

    cout << "UC1 is " << UC1 << endl;

    UC2 = Str1;

    cout << "UC2 is " << UC2 << endl;

    UC2.Concat(UC1);    // Concat is in WCS_String file

    cout << "UC2 is " << UC2 << endl;

    UC2.Concat(WCS_String("Test2"));

    cout << "UC2 is " << UC2 << endl;

    UC2 [4] = 'a';              // [] operator is in WCS_String file.

    cout << "UC2 is " << UC2 << endl;

    UC3.ToUpper ();       // ToUpper is in WCS_String file.

    cout << "UC3 is " << UC3 << endl;

return 0;

}