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

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.