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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:22:57+00:00 2026-05-28T04:22:57+00:00

Hi I am trying to do operator overloading for +(addition) operator to add my

  • 0

Hi I am trying to do operator overloading for +(addition) operator to add my user defined datatype objects .
Following is the code for that.

  #include <iostream>
using namespace std;
class complex {
    int i;
    double f;
public:
    complex(int ii=0,double ff=0){
        i = ii;
        f = ff;
    }
    complex operator+(complex object) {
        complex result;
        result.i = this->i + object.i;
        result.f = this->f + object.f;
        return result;
    }
    void display() {
        cout << i <<"\t"<< f;
        cout << endl;
    }
};
int main(){
    complex obj1(1,1.1),obj2(2,2.2),obj3;
    int i(5);
    obj3 = obj1 + obj2;
    obj3.display();
    obj3 = obj3 + i;
    obj3.display();
    obj3 = i + obj3;//generates me compiler error
    obj3.display();
    return 0;
}

I have learnt that when I do obj1 + obj2,it is expanded by the compiler as obj1.operator+(obj2);
So that part of code works fine.

But when I do add an int and complex ,I think it get expanded as i.operator(obj1).

So it gives me compiler errors.

Should I define operator+ function in int class or how to solve this?
Please suggest,
Thank you,

  • 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-05-28T04:22:58+00:00Added an answer on May 28, 2026 at 4:22 am

    You will have to provide an overloaded version of + which takes int and class complex object as input parameters. This function should be a non member function.

    complex operator+(int i, complex object) 
    

    Note that additionally, if you want to access protected or private members inside this overloaded function then it will have to be made friend of your complex class.

    This is a peculiar example of the strength of free functions used for operator overloading as against member function operator overloads.

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

Sidebar

Related Questions

I'm trying to overload the dereference operator, but compiling the following code results in
I'm trying to create a library for some work and am using operator overloading
Possible Duplicate: Operator Overloading in PHP I am trying to create a library that
I am trying to add two Vectors below is the code snippet :- #include
I'm trying to use operator overloading to define the basic operations (+,-,*,/) for my
Ok, I am working through a book and trying to learn C++ operator overloading.
I'm trying to create an overridden operator function using both const parameters, but I
Possible Duplicate: Operator overloading I didn't find any thing that could help me in
I'm trying to overload an operator in C# (don't ask why!) that applies to
I am trying to deal with operator overloading at the first time, and 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.