954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

a small prob with operator overloading

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

boyz
Light Poster
39 posts since Nov 2007
Reputation Points: 2
Solved Threads: 0
 

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

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

thinker
Newbie Poster
14 posts since Nov 2007
Reputation Points: 6
Solved Threads: 0
 

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.

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

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???

thinker
Newbie Poster
14 posts since Nov 2007
Reputation Points: 6
Solved Threads: 0
 

moved to c++ board.

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

> 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' ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You