How about reading your C book to discover that there is no operator overloading in C?
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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
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