suppose u r makin a class to carry out oprations on complex numbers...

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

6
Contributors
6
Replies
7
Views
10 Years
Discussion Span
Last Post by vijayan121

suppose u r makin a class to carry out oprations on complex numbers...

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

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' ;
}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.