I have a difficult to understand the subject of overloading operator in C++ and Java.
For example, I define a new class Fraction:
class Fraction {
public:
Fraction (int top, int bottom) { t = top; b = bottom; }
int numerator() { return t; }
int denominator() { return b; }
private:
int t, b;
};
and I want to overload the operator << for printing Fraction. How I do it? I need to overload it inside Class Fraction or outside Class Fraction?
In java – Is it possible to overload operator? How I can do it there (for example, I want to overload the operator +).
If there is a metrial about this subject, It will be great.
For C++: Overloading the << Operator on msdn
So as answer to “I need to overload it inside Class Fraction or outside Class Fraction?”
You declare the function as a
friendof the class so that thestd::osteamobject can access its private data. The function, however, is defined outside of the class.