Well, let's give him a hint! A very straightforward way, you will find this in basic text books, is to overload operators as class member functions:
Code:
#include <iostream>
#include <string>
class Integer
{
public:
Integer(int i);
Integer(std::string str);
int getValue();
void setValue(int i);
Integer& operator+( Integer const& arg); // why use 'Integer' AND 'int'
private:
int myValue;
};
// ***
Integer& Integer :: operator+( Integer const& arg)
{
this->myValue += arg.myValue;
return *this;
}
However, try to minimize your class with functions that concern your (private) attributes in the most direct way only, e.g., the constructors (default, copy, cast, ..) 'setValue', 'getValue'. All other functions are best no-member, no-friend.
Why? Try to overload the operator+ for a class Double:
Code:
#include <iostream>
#include <string>
class Double
{
public:
Double(double i);
Double(std::string str);
double getValue();
void setValue(double i);
Double& operator+( Double const& arg); // suppose you have defined a class 'Double'
Double& operator+( Integer const& arg); // to add your Integer to your Double
private:
double myValue;
};
// ***
Integer& Integer :: operator+( Integer const& arg)
{
this->myValue += arg.myValue;
return *this;
}
Double& Double:: operator+( Integer const& arg)
{
this->myValue+= arg.myValue;
return *this;
}
This will work for ' double + int' , but it won't for ' int + double ' . Why? In the member-operator, '*this', so the double, is the left (implicit) argument of '+', and 'int' the right argument. You cannot invert these.
Now you can say: "Ok, but I simply write an 'operator+( Double)' in my class 'Integer' ". Wrong: you cannot declare this before the class 'Double' has been declared!
An even stronger argument: if you want to add an object of a class your colleague wrote, to your Integer, you cannot modify his/her class. And you do want this operator to be commutative.
There are better arguments, but let's hold for now.
I would try it like this:
Code:
#include <iostream>
#include <string>
class Integer
{
public:
Integer(); // no default constructor?
Integer(int i);
Integer(std::string str);
int getValue();
void setValue(int i);
Integer& operator+( Integer const& arg); // why use 'Integer' AND 'int' ?!
private:
int myValue;
};
class Double
{
public:
Double(); // don't you want a default constructor?
Double(double i); // not explicit to cast implicitely Double to Integer
Double(std::string str);
double getValue();
void setValue(double i);
private:
double myValue;
};
// ***
Integer& operator+( Integer const& arg1, Integer const& arg2)
{
Integer result = arg1.myValue + arg2.myValue;
return result;
}
Double& operator+( Integer const& arg1, Double const& arg2)
{
Double result = arg1.myValue + arg2.myValue;
return result;
}
Double& operator+( Double const& arg1, Integer const& arg2)
{
Double result = arg1.myValue+ arg2.myValue;
return result;
}
Double& operator+( Double const& arg1, Double const& arg2)
{
Double result = arg1.myValue + arg2.myValue;
return result;
}
Ok, only the operator with the two 'Double' arguments would have sufficed, since implicit casts are allowed (no 'explicit' keyword in the one-argument constructor). But to make the point...
And any 'operator@' should be defined, if possible, in the canonical way by calling the member 'operator@=' (this _must_ be a member, like '=').
EDIT: try to be as general as possible. Your class is called 'Integer', so why call the private attribute 'myInteger' ? Let's call it 'myValue'.
EDIT2: is #include <iostream> really always necessary?