Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Pre and Post Conditions, Style Conventions Apply the following style requirements Header File : In the case of a class, the header file should

C++ Pre and Post Conditions, Style Conventions

Apply the following style requirements

Header File: In the case of a class, the header file should begin with a (typically) very large initial file comment. This comment should start by indicating your name, class, date, instructor, name of file, etc. Next it should include a brief general description of the class (so client programmers can tell right away whether they want to use it), followed by a listing of all of the prototypes of public functions, each with pre and post conditions. Note that this list of prototypes is still part of the comment, so you will have to list the prototypes again in the code below this header comment. You are required to use pre/post conditions to document your public member functions and friend functions. Do not include any comments regarding the implementation details in the header file! This initial file comment will then be followed by the header file code (e.g. the class declaration), with no comments.

Function Comments: Just above each of your function definitions (except main()) you must provide a comment describing what the function does. A simple function might have a 15 word comment, while a more complex function might have a comment of at least 50 words. Make sure to explain the role of each parameter in your function comments, and refer to them by name. Note: main() does not need a function comment, because this information should be included in the initial file comment.

Initial File Comment: Your programs should be well-documented. Each program should begin with an initial file comment. This comment should start by indicating your name, class, date, instructor, name of file, etc. Next it should describe in detail what the program does and how the code works. Any input expected from the user and any output produced by the program should be described in detail. You should expect your initial file comments to be at least 50 words.

//MyString.h

#ifndef MyString_h

#define MyString_h

#include

using namespace std;

namespace cs_mystring {

class MyString

{

private:

char *desc;

public:

MyString();

MyString(const char *inDesc);

MyString(const MyString &right);

~MyString();

char operator[](int index)const;

char& operator[](int index);

long length()const;

friend std::ostream& operator<<(std::ostream& out, const MyString& myString);

MyString operator=(const MyString &right);

friend bool operator>(const MyString &left, const MyString &right);

friend bool operator<(const MyString &left, const MyString &right);

friend bool operator>=(const MyString &left, const MyString &right);

friend bool operator<=(const MyString &left, const MyString &right);

friend bool operator==(const MyString &left, const MyString &right);

friend bool operator!=(const MyString &left, const MyString &right);

friend std::istream& operator>>(std::istream& in, MyString& target);

friend MyString operator+(const MyString& left, const MyString& right);

void read(std::istream& in, char delimiter);

MyString operator+=(const MyString& right);

};

}

#endif /* MyString_h */

using namespace cs_mystring;

bool eof(istream& in);

void BasicTest();

void RelationTest();

void ConcatTest();

void CopyTest();

MyString AppendTest(const MyString& ref, MyString val);

string boolString(bool convertMe);

int main()

{

BasicTest();

RelationTest();

ConcatTest();

CopyTest();

return 0;

}

bool eof(istream& in)

{

char ch;

in >> ch;

in.putback(ch);

return !in;

}

string boolString(bool convertMe) {

if (convertMe) {

return "true";

}

else {

return "false";

}

}

void BasicTest()

{

MyString s;

cout << "----- Testing basic String creation & printing" << endl;

const MyString strs[] =

{ MyString("Wow"), MyString("C++ is neat!"),

MyString(""), MyString("a-z") };

for (int i = 0; i < 4; i++) {

cout << "string [" << i << "] = " << strs[i] << endl;

}

cout << endl << "----- Now reading MyStrings from file" << endl;

cout << endl << "----- first, word by word" << endl;

ifstream in("mystring.txt");

assert(in);

while (in.peek() == '#') {

in.ignore(128, ' ');

}

in >> s;

while (in) {

cout << "Read string = " << s << endl;

in >> s;

}

in.close();

cout << endl << "----- now, line by line" << endl;

ifstream in2("mystring.txt");

assert(in2);

while (in2.peek() == '#') {

in2.ignore(128, ' ');

}

s.read(in2, ' ');

while (in2) {

cout << "Read string = " << s << endl;

s.read(in2, ' ');

}

cout << endl << "----- Testing access to characters (using const)" << endl;

const MyString s1("abcdefghijklmnopqsrtuvwxyz");

cout << "Whole string is " << s1 << endl;

cout << "now char by char: ";

for (int i = 0; i < s1.length(); i++) {

cout << s1[i];

}

cout << endl << "----- Testing access to characters (using non-const)" << endl;

MyString s2("abcdefghijklmnopqsrtuvwxyz");

cout << "Start with " << s2;

for (int i = 0; i < s2.length(); i++) {

s2[i] = toupper(s2[i]);

}

cout << " and convert to " << s2 << endl;

}

void RelationTest()

{

cout << " ----- Testing relational operators between MyStrings ";

const MyString strs[] =

{ MyString("app"), MyString("apple"), MyString(""),

MyString("Banana"), MyString("Banana") };

for (int i = 0; i < 4; i++) {

cout << "Comparing " << strs[i] << " to " << strs[i + 1] << endl;

cout << "\tIs left < right? " << boolString(strs[i] < strs[i + 1]) << endl;

cout << "\tIs left <= right? " << boolString(strs[i] <= strs[i + 1]) << endl;

cout << "\tIs left > right? " << boolString(strs[i] > strs[i + 1]) << endl;

cout << "\tIs left >= right? " << boolString(strs[i] >= strs[i + 1]) << endl;

cout << "\tDoes left == right? " << boolString(strs[i] == strs[i + 1]) << endl;

cout << "\tDoes left != right ? " << boolString(strs[i] != strs[i + 1]) << endl;

}

cout << " ----- Testing relations between MyStrings and char * ";

MyString s("he");

const char *t = "hello";

cout << "Comparing " << s << " to " << t << endl;

cout << "\tIs left < right? " << boolString(s < t) << endl;

cout << "\tIs left <= right? " << boolString(s <= t) << endl;

cout << "\tIs left > right? " << boolString(s > t) << endl;

cout << "\tIs left >= right? " << boolString(s >= t) << endl;

cout << "\tDoes left == right? " << boolString(s == t) << endl;

cout << "\tDoes left != right ? " << boolString(s != t) << endl;

MyString u("wackity");

const char *v = "why";

cout << "Comparing " << v << " to " << u << endl;

cout << "\tIs left < right? " << boolString(v < u) << endl;

cout << "\tIs left <= right? " << boolString(v <= u) << endl;

cout << "\tIs left > right? " << boolString(v > u) << endl;

cout << "\tIs left >= right? " << boolString(v >= u) << endl;

cout << "\tDoes left == right? " << boolString(v == u) << endl;

cout << "\tDoes left != right ? " << boolString(v != u) << endl;

}

void ConcatTest()

{

cout << " ----- Testing concatentation on MyStrings ";

const MyString s[] =

{ MyString("outrageous"), MyString("milk"), MyString(""),

MyString("cow"), MyString("bell") };

for (int i = 0; i < 4; i++) {

cout << s[i] << " + " << s[i + 1] << " = " << s[i] + s[i + 1] << endl;

}

cout << " ----- Testing concatentation between MyString and char * ";

const MyString a("abcde");

const char *b = "XYZ";

cout << a << " + " << b << " = " << a + b << endl;

cout << b << " + " << a << " = " << b + a << endl;

cout << " ----- Testing shorthand concat/assign on MyStrings ";

MyString s2[] =

{ MyString("who"), MyString("what"), MyString("WHEN"),

MyString("Where"), MyString("why") };

for (int i = 0; i < 4; i++) {

cout << s2[i] << " += " << s2[i + 1] << " = ";

cout << (s2[i] += s2[i + 1]) << "and";

cout << s2[i] << endl;

}

cout << " ----- Testing shorthand concat/assign using char * ";

MyString u("I love ");

const char *v = "programming";

cout << u << " += " << v << " = ";

cout << (u += v) << endl;

}

MyString AppendTest(const MyString& ref, MyString val)

{

val[0] = 'B';

return val + ref;

}

void CopyTest()

{

cout << " ----- Testing copy constructor and operator= on MyStrings ";

MyString orig("cake");

MyString copy(orig); // invoke copy constructor

copy[0] = 'f'; // change first letter of the *copy*

cout << "original is " << orig << ", copy is " << copy << endl;

MyString copy2; // makes an empty string

copy2 = orig; // invoke operator=

copy2[0] = 'f'; // change first letter of the *copy*

cout << "original is " << orig << ", copy is " << copy2 << endl;

copy2 = "Copy Cat";

copy2 = copy2; // copy onto self and see what happens

cout << "after self assignment, copy is " << copy2 << endl;

cout << "Testing pass & return MyStrings by value and ref" << endl;

MyString val = "winky";

MyString sum = AppendTest("Boo", val);

cout << "after calling Append, sum is " << sum << endl;

cout << "val is " << val << endl;

val = sum;

cout << "after assign, val is " << val << endl;

}

//Rest of the program

//main.cpp

#include "MyString.h" #include #include // for toupper() #include #include #include using namespace std;

//MyString.cpp

#include

#include

#include

#include

#include "MyString.h"

using namespace std;

namespace cs_mystring {

// BIG THREE (FOUR) Default constructor, constructor, copy constructor,

// destructor, assignment operator

//

MyString::MyString()

{

desc = new char[1];

strcpy(desc, "");

}

MyString::MyString(const char *inDesc)

{

desc = new char[strlen(inDesc) + 1];

strcpy(desc, inDesc);

}

MyString::MyString(const MyString& right)

{

desc = new char[strlen(right.desc) + 1];

strcpy(desc, right.desc);

}

MyString::~MyString()

{

delete[] desc;

}

MyString MyString::operator=(const MyString &right)

{

if (this != &right)

{

delete[] desc;

desc = new char[strlen(right.desc) + 1];

strcpy(desc, right.desc);

}

return *this;

}

// Two overloaded functions for the [] operator, as the first will deal with

// accessing a value in the C-String, and will pass back a copy of the first

// without the ability to change the C-String

char MyString::operator[](int index)const

{

assert(index >= 0 && index < strlen(desc));

return desc[index];

}

// The second will pass the object reference, and this will allow us to put

// the brackets in the left side of an equation, meaning that we can actually

// change the value in the C-String object

char& MyString::operator[](int index)

{

assert(index >= 0 && index < strlen(desc));

return desc[index];

}

// will return the length of the string without changing the calling object

long MyString::length()const

{

long length = strlen(desc);

return length;

}

// insertion overload

ostream& operator<<(ostream& out, const MyString& myString)

{

out << myString.desc;

return out;

}

// RELATIONAL OPERATORS

bool operator>(const MyString &left, const MyString &right)

{

if (strcmp(left.desc, right.desc) > 0)

return true;

return false;

}

bool operator<(const MyString &left, const MyString &right)

{

if (strcmp(left.desc, right.desc) < 0)

return true;

return false;

}

bool operator>=(const MyString &left, const MyString &right)

{

if (strcmp(left.desc, right.desc) >= 0)

return true;

return false;

}

bool operator<=(const MyString &left, const MyString &right)

{

if (strcmp(left.desc, right.desc) <= 0)

return true;

return false;

}

bool operator==(const MyString &left, const MyString &right)

{

if (strcmp(left.desc, right.desc) == 0)

return true;

return false;

}

bool operator!=(const MyString &left, const MyString &right)

{

if (strcmp(left.desc, right.desc) != 0)

return true;

return false;

}

// temp will be a temporary char array that we will use to hold the values

// of the instream

istream& operator>>(istream& in, MyString& target)

{

while (isspace(in.peek())) {

in.ignore();

}

char temp[128];

in.getline(temp, 127, ' ');

delete[] target.desc;

target.desc = new char[strlen(temp) + 1];

strcpy(target.desc, temp);

return in;

}

// the strLength variable will hold the length of the two strings and

// will add one for the delimiter character

//

// temp will be a temporary string that will be returned as a result of the

// addition, since both of the arguments are consts

MyString operator+(const MyString& left, const MyString& right)

{

long strLength = strlen(left.desc);

strLength += strlen(right.desc);

MyString temp = new char[strLength + 1];

strcpy(temp.desc, left.desc);

strcat(temp.desc, right.desc);

return temp;

}

// char is a temp array that we will use to read in the instream file,

// and we will rely on strcpy on how to add it into our this variable

void MyString::read(std::istream& in, char delimiter)

{

char temp[128];

in.getline(temp, 127, ' ');

delete[] this->desc;

this->desc = new char[strlen(temp) + 1];

strcpy(this->desc, temp);

}

// the strLength variable is used to hold the length of both of the variables

// and add one for the ending character

//

// the temp MyString object is used to temporarily hold the value of the char

// array before we copy it in

MyString MyString::operator+=(const MyString& right)

{

long strLength = strlen(this->desc) + strlen(right.desc) + 1;

MyString temp = new char[strLength];

strcpy(temp.desc, this->desc);

delete[] this->desc;

this->desc = new char[strLength];

strcpy(this->desc, temp.desc);

strcat(this->desc, right.desc);

return *this;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Internals A Deep Dive Into How Distributed Data Systems Work

Authors: Alex Petrov

1st Edition

1492040347, 978-1492040347

More Books

Students also viewed these Databases questions

Question

Who responds to your customers complaint letters?

Answered: 1 week ago

Question

Under what circumstances do your customers write complaint letters?

Answered: 1 week ago