Each time you post you list something different.
There are three general sets of arithmetic operators you need to implement:
1) member functions that take as argument the same type as the class
2) member functions that take as argument something different than the class
3) non-member functions that take as arguments something different and the class
Here are examples. Assume a, b, and c are
complxex, and d is a
double.
1)
c = a + b; c += b;
2)
c = a + d; c += d;
3)
c = d + b;
The member functions should have the following signatures:
1)
complx complx::operator + ( const complx& a ) const
complx& complx::operator += ( const complx& a )
2)
complx complx::operator + ( double d ) const
complx& complx::operator += ( double d )
3)
complx operator + ( double d, const complx& b )
The ones you just listed are from group 3. They should be
friends of the class. (But not members!)
Remember, each function should be prototyped inside the class definition, and then the function should be again listed, complete with it's body, outside the class definition.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
class complx
{
...
public:
...
complx operator + ( const complx& b ) const;
complx& operator += ( const complx& b );
complx operator + ( double d ) const;
complx& operator += ( double d );
friend complx operator + ( double d, const complx& b );
};
...
complx complx::operator + ( const complx& b ) const
{
return complx( real + b.real, imag + b.imag );
}
complx& complx::operator += ( const complx& b )
{
real += b.real;
imag += b.imag;
return *this;
}
...
|
Hope this helps.