a small prob with operator overloading

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 39
Reputation: boyz has a little shameless behaviour in the past 
Solved Threads: 0
boyz boyz is offline Offline
Light Poster

a small prob with operator overloading

 
0
  #1
Nov 22nd, 2007
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.........
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: a small prob with operator overloading

 
0
  #2
Nov 22nd, 2007
How about reading your C book to discover that there is no operator overloading in C?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 14
Reputation: thinker is an unknown quantity at this point 
Solved Threads: 0
thinker thinker is offline Offline
Newbie Poster

Re: a small prob with operator overloading

 
0
  #3
Nov 22nd, 2007
Originally Posted by boyz View Post
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......
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: a small prob with operator overloading

 
0
  #4
Nov 22nd, 2007
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.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 14
Reputation: thinker is an unknown quantity at this point 
Solved Threads: 0
thinker thinker is offline Offline
Newbie Poster

Re: a small prob with operator overloading

 
0
  #5
Nov 23rd, 2007
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???
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,397
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: a small prob with operator overloading

 
0
  #6
Nov 23rd, 2007
moved to c++ board.

>>please tell me how to become a member of this tag???
Huh? Have no idea what you want.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: a small prob with operator overloading

 
0
  #7
Nov 24th, 2007
> 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.
  1. #include <iostream>
  2.  
  3. struct modulo7
  4. {
  5. explicit modulo7( int i ) : number(i%7) {}
  6. inline int value() const { return number ; }
  7. private: int number ;
  8. };
  9.  
  10. inline modulo7 operator+( modulo7 first, modulo7 second )
  11. { return modulo7( (first.value()+second.value()) % 7 ) ; }
  12.  
  13. inline modulo7 operator+( modulo7 first, int second )
  14. { return modulo7( (first.value()+second) % 7 ) ; }
  15.  
  16. inline modulo7 operator+( int first, modulo7 second )
  17. { return second + first ; }
  18.  
  19. template< typename stream_type > inline
  20. stream_type& operator<< ( stream_type& stm, modulo7 m )
  21. { return stm << m.value() ; }
  22.  
  23. int main()
  24. {
  25. modulo7 a(6), b(4) ;
  26. std::cout << a+4 << ' ' << 4+a << ' ' << a+b << '\n' ;
  27. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC