suppose u r makin a class to carry out oprations on complex numbers...
over loading the '+' operator for adding objects' made frm dat class is simple.....


but how would u carry out this operation???

a= 10 + b; // it's not (b+10)


where a & b are objects of the complex no. class.........

Dani AI

Generated

asked how to make an expression like a = 10 + b; work when a and b are instances of a user Complex class. Several replies pointed in the right direction — suggested friend/free functions and demonstrated free-function overloads — but a concise, idiomatic pattern is worth adding.

Make a single-argument constructor that can convert numeric types to your Complex (or provide a dedicated Complex(int) overload). Then implement operator+= as a member and a non-member operator+ that reuses operator+=. Because operator+ is a non-member, the left operand can be implicitly converted (so 10 becomes a Complex) and the expression 10 + b will compile and behave as expected.

Example pattern:

struct Complex {
  double re = 0, im = 0;
  Complex(double r = 0, double i = 0) : re(r), im(i) {}
  Complex& operator+=(const Complex& rhs) { re += rhs.re; im += rhs.im; return *this; }
};

inline Complex operator+(Complex lhs, const Complex& rhs) { lhs += rhs; return lhs; }

Notes and cautions:

  • Making the single-argument constructor explicit disables implicit conversions; do that if you want to avoid 10 + b converting automatically.
  • If you do not want implicit conversions but still need int + Complex, add a non-member operator+(int, Complex) overload.
  • Prefer passing by const& for large objects and returning by value for operator+ (copy-and-add or move-elide is fine).
  • Use std::complex<T> from the standard library if you do not need custom behavior: see the docs.

Further reading: C++ operator rules and conversion behavior on cppreference — operators, explicit/constructors, and std::complex.

Recommended Answers

All 6 Replies

How about reading your C book to discover that there is no operator overloading in C?

suppose u r makin a class to carry out oprations on complex numbers...
over loading the '+' operator for adding objects' made frm dat class is simple.....


but how would u carry out this operation???

a= 10 + b; // it's not (b+10)


where a & b are objects of the complex no. class.........

operator overloading is a topic of c++...not in c......

Hey you are not a newbie; how could you post it in the C forum?!

Considering that you are talking about C++, the problem you are facing can be eliminated by using friend functions.

sorry brother ...
a new user...please dont mind these things...just a learner but know some unkown facts of C too....
have done some sort of reasearch to renew my curiousity...
please tell me how to become a member of this tag???

moved to c++ board.

>>please tell me how to become a member of this tag???
Huh? Have no idea what you want.

> but how would u carry out this operation???
a= 10 + b; // it's not (b+10)

>> the problem you are facing can be eliminated by using friend functions.

friend functions are functions which are not members of the class, but are granted the same access rights as members of the class. they may be either free functions or member functions of another class.
operators (not all) can be overloaded using free functions.
the ideas are orthogonal; a free functions which oveloads an operator need not be a friend function.

#include <iostream>

struct modulo7
{
  explicit modulo7( int i ) : number(i%7) {}
  inline int value() const { return number ; }
  private: int number ;
};

inline modulo7 operator+( modulo7 first, modulo7 second )
{ return modulo7( (first.value()+second.value()) % 7 ) ; }

inline modulo7 operator+( modulo7 first, int second )
{ return modulo7( (first.value()+second) % 7 ) ; }

inline modulo7 operator+( int first, modulo7 second )
{ return second + first ; }

template< typename stream_type > inline
stream_type& operator<< ( stream_type& stm, modulo7 m )
{ return stm << m.value() ; }

int main()
{
  modulo7 a(6), b(4) ;
  std::cout << a+4 << ' ' << 4+a << ' ' << a+b << '\n' ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.