Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7874143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:43:47+00:00 2026-06-03T02:43:47+00:00

Okay I am very new to operator overloading and I have to execute this

  • 0

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
string

The MyString object should contain a Length() method that reports the
length of the string

The 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:43:48+00:00Added an answer on June 3, 2026 at 2:43 am

    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 the operator+ for your string

    const MyString operator +(const MyString& one, const MyString& two) {
            MyString String1 = one + two; // this calls operator+, which calls operator+, which calls...
            return String1;
    }
    

    Also, since you are returning a MyString by value, you shouldn’t return by const:

    MyString operator +(const MyString& one, const MyString& two) {
            MyString String1;
            // do something with the data of one and two and put it in String1
            return String1;
    }
    

    Finally, if your operator needs to access non-public data of MyString, you should declare it as a friend of your MyString class.

    class MyString {
    
    // as before
    
    friend MyString operator+(const Mytring& rhs, const MyString& lhs);
    
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay this is very hard to explain, but i want to add padding to
Okay, I have done a bit of searching online and found this thread, but
Okay, let's start with this very simple button click method private void button1_Click(object sender,
This is probably a very simple question. I am new to Rails and have
Hi I'm very new to web development but have a coding background. I'm trying
okay so this is probably a soft pitch question for sombody, but I want
I'm very new to web design...okay, now that that's out of the way, where
Let me start off by saying Im VERY new to iphone development, but Im
Okay, i'm very new to Javascript and i'm trying to create a slideshow with
Okay, just for starters, I am very new to Objective-C, (C in general). I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.