Note that there are some explanatory texts on larger screens.

plurals
  1. POOperator+ overloading
    primarykey
    data
    text
    <p>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:</p> <blockquote> <p>The MyString object should contain a Print() method that prints the string</p> <p>The MyString object should contain a Length() method that reports the length of the string</p> <p>The MyString object should contain a default constructor that sets the inital string to the value of "Hello World".</p> <p>The MyString object should contain an alternate constructor that allows for setting of the inital value of the string.</p> <p>The MyString object should overload the following operators:</p> <ul> <li><p>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. </p></li> <li><p>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.</p></li> <li><p>Logical comparison operator (==) that returns true iff the two strings are identical in size and contents.</p></li> <li><p>Negated logical comparison operator (!=) that returns boolean negation of 2.</p></li> <li><p><strong>Addition operator (+) that concatenates two strings</strong></p></li> <li><p>Addition/Assigment operator (+=) used in the following fashion: String1 += String2 to operate as String1 = String1 + String2</p></li> <li><p><strong>Both addition (+) and assignment (=) operators need to be capable of cascaded operations. This means String3 = String1 + String2, or String1 = String2 = String3 should work.</strong></p></li> </ul> </blockquote> <p>Here is my code in the .cpp file:</p> <pre><code>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 &lt; 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 &lt; 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&amp; one, const MyString&amp; two) { MyString String1; return String1; }** MyString&amp; MyString::operator()(const int index, const char b) { if(String[index] == '\0') { exit(1); } else { String[index] = b; } } MyString&amp; MyString::operator=(const MyString&amp; rhs) { Size = rhs.Size; counter = rhs.counter; delete [] String; String = new char[Size]; for(int i = 0; i &lt; counter+1 ; i++) { String[i] = rhs.String[i]; } return *this; } bool MyString::operator==(const MyString&amp; one) { if(one.Length() == two.Length()) { for(int i = 0; i &lt; one.Length()+1; i++) { if(one[i] == two[i]) return true; } } else return false; } MyString&amp; MyString::operator()(const int i) { if( String[i] == '\0') { exit(1); } else { return String[i]; } } void MyString::Print() { for(int i=0; i &lt; Size; i++) cout &lt;&lt; String[i]; cout &lt;&lt; endl; } </code></pre> <p>Here is my code in the main file:</p> <pre><code>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 &lt;&lt; "*************Test of values*************" &lt;&lt; endl; String1.Print (); String2.Print (); String3.Print (); cout &lt;&lt; "*************Test of Length*************" &lt;&lt; endl; cout &lt;&lt; String1.Length () &lt;&lt; " "; cout &lt;&lt; String2.Length () &lt;&lt; " "; cout &lt;&lt; String3.Length () &lt;&lt; endl; cout &lt;&lt; "*************Test of Set*************" &lt;&lt; endl; String1 (0, 'J'); String1.Print (); cout &lt;&lt; "*************Test of Copy*************" &lt;&lt; endl; String1.Print (); cout &lt;&lt; endl; String3.Print (); cout &lt;&lt; endl; String3.Copy (String1); //String1 should be copied into String3 String1.Print (); cout &lt;&lt; endl; String3.Print (); cout &lt;&lt; endl; cout &lt;&lt; "*************Test of Get*************" &lt;&lt; endl; for (int i = 0; i &lt; String1.Length (); i++) //The last character should exit the program { cout &lt;&lt; String1 (i) &lt;&lt; " "; } } cout &lt;&lt; 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; } </code></pre> <p>The main.cpp file cannot change but the other .cpp file has to compile and run along with that file.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload