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

  • SEARCH
  • Home
  • 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 9227139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:59:45+00:00 2026-06-18T04:59:45+00:00

The code below, which is giving a correct output of the addition, changes x

  • 0

The code below, which is giving a correct output of the addition, changes x value of the first object b after doing it.

class numbers{
public:
    int x;
    numbers(int i1){
        x = i1;
    }
    numbers operator+ (numbers num){
        x = x + num.x;
        return(x);
    }
};

int main(){
    numbers a (2);
    numbers b (3);
    numbers c (5);
    numbers d (7);
    cout << a.x << b.x << c.x << d.x << endl; // returns 2357
    numbers final (100); //this value won't be shown
    final = a+b+c+d; 
    cout << a.x << b.x << c.x << d.x << endl; // returns 5357
    cout << final.x; //returns 17 (2+3+5+7)
    system("pause");
}

The question is, how does this addition class works exactly? I mean, why is x from object a modified?
I though only x from final object would be modified.

Thanks 🙂

  • 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-18T04:59:46+00:00Added an answer on June 18, 2026 at 4:59 am

    The call to operator+ is just like any other member function call. a + b translates to a.operator+(b). So the line x = x + num.x; in this case is actually assigning to a.x. To achieve what you want you need to instead populate a new numbers with the new value i.e.

    numbers operator+ (numbers num) const {
        return numbers(x + num.x)
    }
    

    Also note the const – which would have given you a compile error when you made that mistake.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following java code, which is giving the error below: import java.io.File;
I have a working JavaScript code below which dynamically creates JSON object using JSON.parse
Why is the below code giving a compile time error. Map<String,? extends Object> inputMap
I was trying below code: In which I am calling opengascap() of car class
Used code first and everything appears to work apart from the below which also
I am trying to get my own location.Below is the code which is giving
I have the code below which I use clone() and live() . The code
I have the following code below which works, but I want to insert values
I have the code below which I use clone() and delegate() . The code
I have wrote the code below which was taken from Java How to program

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.