Okay I am very new to operator overloading and I have to execute this function in an object oriented program, but I definitely need help. Here are the instructions:
The MyString object should contain a Print() method that prints the
stringThe MyString object should contain a Length() method that reports the
length of the stringThe MyString object should contain a default constructor that sets the
inital string to the value of “Hello World”.The MyString object should contain an alternate constructor that
allows for setting of the inital value of the string.The MyString object should overload the following operators:
Parenthesis operators should be overloaded to replace the Set and Get functions of your previous assignment. Note that both instances
should issue exit(1) upon violation of the string array bounaries.Assignment operator (=) which will copy the source string into the destination string. Note that size of the destination needs to be
adjusted to be the same as the source.Logical comparison operator (==) that returns true iff the two strings are identical in size and contents.
Negated logical comparison operator (!=) that returns boolean negation of 2.
Addition operator (+) that concatenates two strings
Addition/Assigment operator (+=) used in the following fashion: String1 += String2 to operate as String1 = String1 + String2
Both addition (+) and assignment (=) operators need to be capable of cascaded operations. This means String3 = String1 +
String2, or String1 = String2 = String3 should work.
Here is my code in the .cpp file:
MyString::MyString()
{
char temp[] = "Hello World";
int counter(0);
while(temp[counter] != '\0') {
counter++;
}
Size = counter;
String = new char [Size];
for(int i=0; i < Size; i++)
String[i] = temp[i];
}
MyString::MyString(char *message)
{
int counter(0);
while(message[counter] != '\0') {
counter++;
}
Size = counter;
String = new char [Size];
for(int i=0; i < Size; i++)
String[i] = message[i];
}
MyString::~MyString()
{
delete [] String;
}
int MyString::Length()
{
int counter(0);
while(String[counter] != '\0')
{
counter ++;
}
return (counter);
}
**const MyString operator +(const MyString& one, const MyString& two)
{
MyString String1;
return String1;
}**
MyString& MyString::operator()(const int index, const char b)
{
if(String[index] == '\0')
{
exit(1);
}
else
{
String[index] = b;
}
}
MyString& MyString::operator=(const MyString& rhs)
{
Size = rhs.Size;
counter = rhs.counter;
delete [] String;
String = new char[Size];
for(int i = 0; i < counter+1 ; i++)
{
String[i] = rhs.String[i];
}
return *this;
}
bool MyString::operator==(const MyString& one)
{
if(one.Length() == two.Length())
{
for(int i = 0; i < one.Length()+1; i++)
{
if(one[i] == two[i])
return true;
}
}
else
return false;
}
MyString& MyString::operator()(const int i)
{
if( String[i] == '\0')
{
exit(1);
}
else
{
return String[i];
}
}
void MyString::Print()
{
for(int i=0; i < Size; i++)
cout << String[i];
cout << endl;
}
Here is my code in the main file:
int main (int argc, char **argv)
{
MyString String1; //Test of default constructor. Use "Hello world"
MyString String2 ("Two strings are not equal"); //Test of alternate constructor
MyString String3 ("Two strings are equal");
MyString String4 (String1);
cout << "*************Test of values*************" << endl;
String1.Print ();
String2.Print ();
String3.Print ();
cout << "*************Test of Length*************" << endl;
cout << String1.Length () << " ";
cout << String2.Length () << " ";
cout << String3.Length () << endl;
cout << "*************Test of Set*************" << endl;
String1 (0, 'J');
String1.Print ();
cout << "*************Test of Copy*************" << endl;
String1.Print ();
cout << endl;
String3.Print ();
cout << endl;
String3.Copy (String1); //String1 should be copied into String3
String1.Print ();
cout << endl;
String3.Print ();
cout << endl;
cout << "*************Test of Get*************" << endl;
for (int i = 0; i < String1.Length (); i++) //The last character should exit the program
{
cout << String1 (i) << " ";
}
}
cout << endl;
if (String1 == String4)
{
String3.Print ();
}
else
{
String4.Print ();
}
if (String1 != String4)
{
String3.Print ();
}
else
{
String4.Print ();
}
String1 = String2 = String3;
String1.Print ();
String2.Print ();
String3.Print ();
String1 = String2 + String3 + String4;
String1.Print ();
String2 += String3;
String2.Print ();
return 0;
}
The main.cpp file cannot change but the other .cpp file has to compile and run along with that file.
You would need to put the declaration of the operator in a header, but the problem you have is that you are using
operator+for your string inside theoperator+for your stringAlso, since you are returning a
MyStringby value, you shouldn’t return byconst:Finally, if your operator needs to access non-public data of
MyString, you should declare it as a friend of yourMyStringclass.