On Jul 23, 3:15 am, "Jim Langston" <tazmas...@rocketmail.com> wrote:
"clicwar" <clic...@gmail.com> wrote in message
news:1185144847.041900.64380@q75g2000hsh.googlegroups.com...
[...]
Here's your program cleaned up a bit and extened a little.
Cleaned up still some more, to make it more idiomatic. (The
changes don't actually make a difference here, but in many
cases, they do, and it's good practice to stick to.)
#include <iostream>
#include <string>
class Vector
{
private:
float x,y;
Normally, of course, we'd use double everywhere. Float is
reserved for cases where we have to save memory.
public:
Vector(float u, float v);
Vector operator+ (const Vector &a) const;
Most of the time, it is preferable to make operator+ a free
function. It only really makes a difference if implicit
conversions are involved, but it's never wrong, so why not be
consistent about it.
Also, the most important single rule about operator overloading
is to do it consistently with what the built in types do. So if
you define +, you also define +=. With the same semantics.
It's fairly frequent to define operator+ in terms of +=, e.g.:
// Free function...
Vector operator+( Vector lhs, Vector rhs )
{
lhs += rhs ;
return lhs ;
}
or
Vector operator+( Vector const& lhs, Vector const& rhs )
{
Vector result( lhs ) ;
result += rhs ;
return result ;
}
(The generation of these free functions can actually be
automated pretty well by means of some clever use of templates,
but I don't think that the original poster is ready for that
yet.)
Vector operator* (const float a ) const;
Vector operator* (const Vector &b) const;
Vector(const Vector &source);
void Show();
};
void Vector::Show()
{
std::cout <<"(" << x <<"," <<y <<")";
}
Vector::Vector(float u = 0, float v = 0): x(u), y(v)
{
}
Given that this function will typically be in a separate soruce
file, the default arguments don't make any sense here. They
should be on the function declaration in the class.
And if you use them, you've created a converting constructor,
and you'll definitely want operator+ to be a non-member:
Vector a, b ;
a = b + 3.14 ; // Legal, member or not...
a = 3.14 + b ; // Legal if non-member, illegal if member.
Vector::Vector(const Vector &source)
{
x = source.x;
y = source.y;
}
Again, it makes no difference in this particular case, but you
really should prefer initialization lists:
Vector::Vector(
Vector const& source )
: x( source.x )
, y( source.y )
{
}
(And while I'm at it: note the position of the const. In this
case, const Vector and Vector const are both legal, but most of
the time, the const must follow what it modifies, so for
consistency's sake, we always put it after.)
Vector Vector::operator+ (const Vector &a) const
{
Vector temp;
temp.x = x + a.x;
temp.y = y + a.y;
return temp;
}
Vector Vector::operator* (float a) const
{
Vector temp;
temp.x = x * a;
temp.y = y * a;
return temp;
}
So you support "v * 3.14", but not "3.14 * v". Not good, IMHO.
--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
James Kanze, thank you for your time.
myself again.
I concentrate this confusion in two quick problems. The code below