Gang of Four and other friends Program Description: This assignment is continuin
ID: 3720513 • Letter: G
Question
Gang of Four and other friends
Program Description: This assignment is continuing to make your MYString class so that it is more complete and usable. The data within your MYString class will be the same as before, but now we are adding the Gang of Four functions and some operators. I have highlighted the new/modified functions in red below.
Your Gang of four and overloaded operator functions should have “normal behavior”. As you write each function, you should make a little test to verify that the function is working correctly.
The MYString class will need to have the following member functions:
Programming Note: Write and test one or two functions at a time.
Remember to mark member functions that do not change member data with const
Member Functions : return type
Description
MYString( )
Default Constructor: creates an empty string
MYString(const MYString & mstr)
Copy Constructor: creates a string that is a duplicate of the argument
MYString (const char*)
creates a string that contains the information from the argument
example: MYString greeting( "hello there wise one");
~MYString()
Destructor: release the dynamic memory
= operator : lvalue&
Replaces
setEqualTo(const MYString& str): void
Assignment Operator: this does the assignment operation aStr.setEqualTo( bStr ) would change aStr so that it would contain the same information as bStr
Non-const
[ ] operator : char
Like:
at( int index) : char
returns the character at a certain location. No Error checking. Fast but dangerous
>> operator:
istream&
Replaces
read( istream& istr) : istr
read a string from the istream argument (could be from cin or an ifstream variable) {when reading in, you can assume that you will not read in a string longer than 99 characters} This function will now return the istream which is the normal behavior.
<< operator : ostream&
Replaces
write( ostream& ostr) : ostr
write the string out to the ostream argument, but do not add any end of line (could be cout or an ofstream variable) This function will now return the ostream which is the normal behavior.
< operator
> operator
== operator : all return a bool
Replaces/Uses
compareTo( const MYString& str) : int
You could either replace the compareTo function with the 3 comparison operators or you could make the compareTo function private and then have the operators call compareTo. If you have the operators use compareTo, then you should make compareTo private.
Compares the object string (Ostr) to the argument string (Astr) by subtracting each element of Astr from Ostr until a difference is found or until all elements have been compared
Ostr < Astr returns a negative number
Ostr > Astr returns a positive number
Ostr ==Astr returns zero
+ operator : MYString
this function will add the rvalue onto the back of the lvalue. For example if you had the following values inside two of your strings and added them “bat” + “man”, then you would return a MYString that contains “batman”
length( ) : int
the length of the string ( "cat" would return 3)
capacity() : int
The amount of spaces that is currently reserved for possible use before growing
c_str( ) : const char *
return a pointer to a constant cstring version of the MYString object
Main Program Requirements:
Read the words from the input file into MYString variables (Note: The input file is out in Files/Program Resource/P3 MYString v2 and is called infile3.txt). As the words are being read in append them with the + operator to each other until you have combined 5 words together into one jumbo MYString. Then take this jumbo MYString and add it into the vector with pushback (so it will be using your copy constructor). The input file probably won’t have an even multiple of 5, so you will need to allow the loop to end due to reaching end of file (not due to knowing the word count).
Once you have read, combined, and pushed your MYStrings into the vector, then once again sort the strings into ascending order based on their ASCII value. Then output them to the screen with one combination string per line. When outputting the strings also include the length and capacity of the string on the same line. Here is an example of one line of output:
After outputting all the combo strings, then on a new line each output the count of the current objects, and the total count of the objects create throughout your program. These static outputs are required. You should strive for accuracy, but are more for our curiousity sake.
You much create your own sort routine (can’t call a library, bubble sort is fine) and you can’t use the swap function (I want you to be using your assignment operator).
_____________________________________________________________________________________________________
My .cpp file without overriding: https://pastebin.com/0RPRGepY
My .h file: https://pastebin.com/bqw8u9vy
main.cpp not made yet.
input file: https://pastebin.com/Q9P5eLX4
Programming Note: Write and test one or two functions at a time.
Remember to mark member functions that do not change member data with const
Member Functions : return type
Description
MYString( )
Default Constructor: creates an empty string
MYString(const MYString & mstr)
Copy Constructor: creates a string that is a duplicate of the argument
MYString (const char*)
creates a string that contains the information from the argument
example: MYString greeting( "hello there wise one");
~MYString()
Destructor: release the dynamic memory
= operator : lvalue&
Replaces
setEqualTo(const MYString& str): void
Assignment Operator: this does the assignment operation aStr.setEqualTo( bStr ) would change aStr so that it would contain the same information as bStr
Non-const
[ ] operator : char
Like:
at( int index) : char
returns the character at a certain location. No Error checking. Fast but dangerous
>> operator:
istream&
Replaces
read( istream& istr) : istr
read a string from the istream argument (could be from cin or an ifstream variable) {when reading in, you can assume that you will not read in a string longer than 99 characters} This function will now return the istream which is the normal behavior.
<< operator : ostream&
Replaces
write( ostream& ostr) : ostr
write the string out to the ostream argument, but do not add any end of line (could be cout or an ofstream variable) This function will now return the ostream which is the normal behavior.
< operator
> operator
== operator : all return a bool
Replaces/Uses
compareTo( const MYString& str) : int
You could either replace the compareTo function with the 3 comparison operators or you could make the compareTo function private and then have the operators call compareTo. If you have the operators use compareTo, then you should make compareTo private.
Compares the object string (Ostr) to the argument string (Astr) by subtracting each element of Astr from Ostr until a difference is found or until all elements have been compared
Ostr < Astr returns a negative number
Ostr > Astr returns a positive number
Ostr ==Astr returns zero
+ operator : MYString
this function will add the rvalue onto the back of the lvalue. For example if you had the following values inside two of your strings and added them “bat” + “man”, then you would return a MYString that contains “batman”
length( ) : int
the length of the string ( "cat" would return 3)
capacity() : int
The amount of spaces that is currently reserved for possible use before growing
c_str( ) : const char *
return a pointer to a constant cstring version of the MYString object
Explanation / Answer
main.cpp
#include "stdafx.h"
#include "MYString.h"
#include<iostream>
#include<fstream>
#include<vector>
#include<iomanip>
using namespace std;
vector<MYString> strVec(100);
int main()
{
ifstream inFile;
inFile.open("infile2.txt");
if (inFile.is_open())
{
int index = 0;
while (!inFile.eof())
{
strVec[index].read(inFile);
index++;
}
inFile.close();
strVec.resize(index);
}
else
{
cout << "Error reading file ";
system("pause");
return 1;
}
for (int i = 0; i < strVec.size(); i++)
{
for (int j = 1; j < strVec.size(); j++)
{
if (strVec[j - 1].compareTo(strVec[j]) > 0)
{
MYString tempStr = *new MYString;
tempStr.setEqualTo(strVec[j]);
strVec[j].setEqualTo(strVec[j - 1]);
strVec[j - 1].setEqualTo(tempStr);
}
}
}
cout << setw(10);
for (int i = 0; i < strVec.size(); i++)
{
strVec[i].write(cout);
if ((i % 7 == 0) && (i != 0))
cout << endl;
}
cout << endl;
ofstream outFile("outfile.txt");
outFile << setw(10);
for (int i = 0; i < strVec.size(); i++)
{
strVec[i].write(outFile);
if ((i % 7 == 0) && (i != 0))
outFile << endl;
}
outFile << endl;
outFile.close();
system("pause");
}
MYString.cpp
#include "MYString.h"
#include<iostream>
MYString::MYString()
{
len = 0;
cap = 20;
str = new char[20];
str[0] = '';
}
MYString::MYString(const char* in)
{
while (in[len] != '')
{
len++;
}
len++;
cap = ((len / 20) + 1) * 20;
str = new char[cap];
for (int i = 0; i < len; i++)
{
str[i] = in[i];
}
}
int MYString::length()
{
return len;
};
int MYString::capacity()
{
return cap;
}
char MYString::at(int index)
{
if ((index > cap) && (index >= 0))
return '';
char iChar = str[index];
return iChar;
}
char* MYString::c_str()
{
return str;
}
void MYString::setEqualTo(const MYString& argStr)
{
if (&argStr != this)
{
delete str;
len = argStr.len;
cap = argStr.cap;
str = new char[cap];
for (int i = 0; i < len; i++)
{
str[i] = argStr.str[i];
}
}
}
void MYString::write(std::ostream& ostr)
{
for (int i = 0; i < len; i++)
ostr << str[i];
}
bool MYString::read(std::istream& istr)
{
char* readBuf = new char[100];
istr >> readBuf;
len = 0;
while (readBuf[len - 1] != '')
{
len++;
}
cap = ((len / 20) + 1) * 20;
str = new char[cap];
for (int i = 0; i < len; i++)
{
str[i] = readBuf[i];
}
delete readBuf;
return true;
}
int MYString::compareTo(const MYString& argStr)
{
int sumDiff = 0;
for (int i = 0; i < len; i++)
{
sumDiff = str[i] - argStr.str[i];
if (sumDiff != 0)
return sumDiff;
}
return 0;
}
MYString::~MYString()
{
delete[] str;
}
MYString.h
#pragma once
#include<iostream>
class MYString
{
public:
//Default constructor. will give you an empty string with a
//capacity of 20 characters.
MYString();
//Overloaded constructor, will construct a string by using the character
//pointer given in the constructor call.
MYString(const char*);
//returns the length of the string
int length();
//returns the capacity of the string (increases in blocks of 20)
int capacity();
//returns the character at the index location given
char at(int index);
//returns a pointer to the character array, works with cout
char* c_str();
//replace the contents of the string with the contents of the
//given string
void setEqualTo(const MYString &argStr);
//writes the string to the given ostream. can write to console
//or file.
void write(std::ostream& ostr);
//reads from the given istream. can read from files or console.
bool read(std::istream& istr);
//compares this string to the contents of another string.
int compareTo(const MYString& argStr);
//destructor. cleans up after the function is no longer needed.
~MYString();
private:
int len = 0;
int cap = 20;
char* str;
};
infile2.txt
They seemed amazingly busy. I began to ask myself what they could be.
Were they intelligent mechanisms? Such a thing I felt was impossible.
Or did a Martian sit within each, ruling, directing, using, much as
a man's brain sits and rules in his body? I began to compare the
things to human machines, to ask myself for the first time in my life
how an ironclad or a steam engine would seem to an intelligent lower
animal.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.