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 7984143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:10:30+00:00 2026-06-04T11:10:30+00:00

I’m very new to operator operloading concept and the related questions asked before were

  • 0

I’m very new to operator operloading concept and the related questions asked before were way ahead of me, so I need to ask a basic question.

Here is the .h file:

#define ACCOUNT_H

using namespace std;

class Account{
  friend Account &operator+ (Account &acc);
  friend ostream &operator<< (ostream &, Account &);

  public:
    Account(double=100.0,double=0.0,double=0.0);

    Account &returnSum(Account &otherAccount) const;
    Account& operator+=(Account &Acc1);

    void setT(double);
    void setD(double);
    void setE(double);
    double getT(void);
    double getD(void);
    double getE(void);
    void printAccount();

  private:
    double t;
    double d;
    double e;
};

#endif

I need to overload + as a global function “with single argument” (this was the challenging part for me here) and += as member function (here I assume I can’t take the right hand side operand since it is a member function, so that was the problematic part). Here’s my implementation for +=:

Account &Account ::operator+=(Account &Acc1){
   Account *result = new Account(Acc1.getT()+t,Acc1.getD()+d,Acc1.getE()+e);
   Acc1 = *result;
   return *this;
}

I would really appreciate if you could correct this += and write me an implementation for + overloading. I simply need the t,d,e values to be added as an Account object.

  • 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-04T11:10:33+00:00Added an answer on June 4, 2026 at 11:10 am

    If you want operator+ as a free function, you need:

    friend Account operator+ (const Account &acc, const Account &secondAcc);
    

    Also, operator + is a binary operator, so it’s impossible for it to only receive one argument. Even when a member function, it takes 2 parameters, just that the first parameter, this, is passed under the hood.

    So, your two options:

    1) Member operator

    class Account{
        Account operator+ (const Account &acc);
    };
    

    2) Free operator

    class Account{
        friend Account operator+ (const Account &acc, const Account &secondAcc);
    };
    
    Account operator+ (const Account &acc, const Account &secondAcc)
    {
    }
    

    Very important 1:

    Note that I’m returning by value, not reference as you do. This is to prevent UB, as you’ll probably return a local variable, which is illegal to return by reference.

    Very important 2:

    Account &Account ::operator+=(Account &Acc1){
    
       Account *result = new Account(Acc1.getT()+t,Acc1.getD()+d,Acc1.getE()+e);
       Acc1 = *result;
    
       return *this;
    
    }
    

    This code will leak. Why not use automatic storage variables:

    Account &Account ::operator+=(Account &Acc1){
       Account result(Acc1.getT()+t,Acc1.getD()+d,Acc1.getE()+e);
       Acc1 = result;
       return *this;
    }
    

    Still not sure about the logic inside, but at least it doesn’t leak memory. The way you have it now, you’re modifying the parameter, not the object you call += on. So after, say, a+=b, a will still be the same, and b will be modified.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I need to clean up various Word 'smart' characters in user input, including but
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.