Question
Assignment.: In C++, derive two new classes from the WCS_String class. UpperCaseString - This class will allow any characters to be stored but will store
Assignment.: In C++, 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 - Thi class will only allow 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. ---
This is what I have so far and what I think I need to do finish it up. How do I implement it in my program? Main.cpp
#include
using namespace std;
#include
#include "WCS_String.h"
#include "UpperCaseString.h"
int main()
{
LabDescription();
WCS_String Str1("John");
UpperCaseString UC1;
WCS_String * StringArray[5];
WCS_String Str2("123Nns");
StringArray[0] = new WCS_String("abcdefg");
StringArray[1] = new UpperCaseString(Str1);
(*StringArray[1]).SetAt('c', 1);
delete StringArray[0];
delete StringArray[1];
UC1 = Str1;
// UC1.ToLower ();
system("pause");
return 0; UpperCaseString.cpp
#include "UpperCaseString.h"
UpperCaseString::UpperCaseString() // automatically calls the default constructor of the parent
{
}
UpperCaseString::UpperCaseString(const UpperCaseString & UC) : WCS_String(UC) // polymorphism, we are passing the address of the WCS_String part of UC
{
}
UpperCaseString::~UpperCaseString() // automatically calls the destructor of the parent
{
}
UpperCaseString & UpperCaseString::Copy(const UpperCaseString & UC)
{
WCS_String::Copy(UC);
return *this;
}
UpperCaseString & UpperCaseString::Copy(const WCS_String & Str)
{
WCS_String::Copy(Str);
ToUpper();
return *this;
}
UpperCaseString & UpperCaseString::operator = (const UpperCaseString & UC)
{
WCS_String::Copy(UC);
return *this;
}
UpperCaseString & UpperCaseString::operator = (const WCS_String & Str)
{
WCS_String::Copy(Str);
ToUpper();
return *this;
}
UpperCaseString.h
#ifndef UPPER_CASE_STRING
#define UPPER_CASE_STRING
#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 &);
bool SetAt(char, int);
UpperCaseString & operator = (const UpperCaseString &);
UpperCaseString & operator = (const WCS_String &);
// UpperCaseString & operator = (const char []); // probably argue against using char [] here because if we
// ever go international everywhere we do this needs to be changed
// and we should only set that up to be in WCS_String
private:
UpperCaseString & ToLower(); // so that people can't call this to mess up our class
char & operator [] (int);
};
class Digit
{
Digit();
Digit(const Digit&);
Digit(const WCS_String &);
};
#endif WCS_String.h
//******************************************************************************
//
// Example Class to act as a string of characters
//
// Copyright: 2002-2007 WCS Software Services
// Author: Bill Slater
//
//******************************************************************************
#ifndef WCS_STRING_H
#define WCS_STRING_H
#include
#include
#include
using namespace std;
#pragma warning (disable:4996)
class WCS_String
{
//************* Exceptions *****
public:
enum Exceptions { IndexOutOfBounds };
//************* Method Prototypes *****
public:
explicit WCS_String(const char * = "") throw (...);
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;
bool GetAt(char &, int) const throw (...);
bool IsEmpty() const throw ();
size_t Length() const throw ();
istream & Read(istream & = cin) throw (...);
static char ReadChar(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 (...);
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
}
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
}
inline bool WCS_String::SetAt(char c, int i)
{
if ((i >= 0) && (i < static_cast
{
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
} The idea:
So I'm assuming copying a string is this for all chars in string { data[i] = char[i] }.
uppercase would be for all chars in string { if char[i] >='a' && char[i] <= 'z' data[i] = char[i] + ('A'-'a') else data[i] = char[i] } basically the ('A'- 'a' ) is the difference to jump between the two.
Another way of doing this is char[i] - ' a' + 'A' which means see how far the char is from 'a' and add that difference to 'A' for Number, I assume having an exception added called "NotANumber" if would be for all chars in string { if char[i] >='0' && char[i]<='9' data[i] = char[i] else throw NotANumber }. it'll overwrite some of it
So I say for all chars in string { if ! (>='0' <='9') throw exception } for all chars in string { data[i]=char[i] }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started