Ok If I define a new class called bike. I have two bike objects: x,y and I want the expression x+y to be a new object of the class. What is the prototype of the function that i would write to use the expression x+y?
Ok If I define a new class called bike. I have two bike objects: x,y and I want the expression x+y to be a new object of the class. What is the prototype of the function that i would write to use the expression x+y?
This function might need to be a friend of your bike class if it requires access to private or protected data.Code:bike operator+(const bike& lhs, const bike& rhs);
thanks jlouOriginally Posted by jlou
what do the lhs and rhs mean in the code you provided
if i do delcare this as type friend then would that precede "bike" in your code?
thanks again
also is this just a case of overloading operator?
Here is Sutter's canonical form of operator+:
Among other things, it keeps the addition code confined to the operator+= so that if you have to update it you only have to change one place. This assumes that you have a working copy constructor, or the default copy constructor is sufficient.Code:T& T::operator+=( const T& other ) { // ... All addition work goes here. return *this; } const T operator+( const T& a, const T& b ) { T temp( a ); temp += b; return temp; }
Originally Posted by jlou
well I am very new at c++ and that seems a little out of my element for the time being, is there a simple way to explain that? I am just trying practice excercises here, thanks, sorry if I come off as silly
Yes, this is just operator overloading. And yes, you would precede bike with friend inside your class, then define it outside. The lhs and rhs are just variable names, you can use whatever you want. They stand for left-hand side and right-hand side of the equation.
If you use Sutter's form (above), you wouldn't need a friend, since operator+= is a member.[EDIT] - The code that is in blue above is what you should update for your class. The rest of the code is pretty standard and you can use it as is.Code:class bike { public: bike() : data(0) { } bike& operator+=(const bike& rhs); private: int data; }; bike& bike::operator+=(const bike& rhs) { data += rhs.data; return *this; } const bike operator+(const bike& lhs, const bike& rhs) { bike temp(lhs); temp += rhs; return temp; }
Also, make sure you notice the typo I fixed above (should be "temp += rhs", not "lhs += rhs").
Last edited by jlou; 10-19-2004 at 01:31 PM.
ok, so is there a more basic prototype to use then canoical form you provided? I am just taking notes on some standard prototypes in certain situations to prepare for self test
I'm not sure what you mean by more basic. I guess a simpler approach would be my first suggestion, especially if you only want to implement operator+.Code:class bike { public: bike() : data(0) { } friend bike operator+(const bike& lhs, const bike& rhs); private: int data; }; bike operator+(const bike& lhs, const bike& rhs) { bike temp; temp.data = lhs.data + rhs.data; return temp; }
hmmm. before you've gone to far, will it make sense to "add two bikes"?
I mean, you could always overload '+' to perform anything you want, but the...??Code:Bike bike3 = bike2 + bike1; // ????
source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense
One word: Tandem
Oh, okay. I had to look up that word in the dictionary. But, yeah, I was just checking to make sure that you don't add unnecessary, unintuitive features to your classOriginally Posted by sean_mackrory
source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense
Tandem bikes are the ones with two seats...