By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,167 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,167 IT Pros & Developers. It's quick & easy.

Overload operator problem

P: n/a
Ok, thanks to some good assistance/advice from people in this group I've
been able to further develop my Distance class.

Since previous posts I've refined my code to accept the unit measurement as
a char rather than incorrectly representing it as an int. I've done this
because I want to develop the class so that it will be able to convert
between values, ie if I add 500 m to 1 km I'd like a correct result given in
metres (1500 m in this case). I'd appreciate any suggestion on how best to
do this expanding on the code I've already written.

My next step is implementing an overload of the "operator=" assignment
operator that will allow one Distance value to be assigned to another
Distance value. I've already written code into the .h and .cpp files to do
this; however, following a compile and execution the output of the test
driver (DISTANCE_TEST) shows the assignment of values is occuring before the
original values have been output to screen.

I'd appreciate any advice/suggestions on what I'm doing wrong and how I can
go about fixing the problem. I'm only a novice so please make any
suggestions plain and simple. Further to this, please explain any code
examples so that I can understand and learn. The .h file and .cpp file is
below.

Thanks

..h file
***********************
#ifndef DISTANCE_H

#define DISTANCE_H

#include <iostream>

using namespace std;

class Distance

{

public :

Distance (int, char) ; // constructor - takes int and char values

Distance (void) ; // default - zero

//access member functions

int number (void) const;

char measure (void) const;

//overloads

Distance operator= (Distance) ; // overload of assignment operator

private :

int nu ; // the value

char me ; // the unit of measure (m)

} ;

// provide an overload of "<<" for easy display

ostream& operator<< (ostream&, const Distance&);

#endif
..cpp file
***********************

#include "Distance.h"

#include <iostream>

using namespace std;

/*-------------------------------------------------------*\

| implementation of member functions |

\*-------------------------------------------------------*/

// constructors

Distance :: Distance (int n, char m) : nu(n), me(m) {}

Distance :: Distance (void) : nu(0), me(1) {}

enum {cm, m, km};

// access functions

int Distance :: number (void) const

{

return nu;

}

char Distance :: measure (void) const

{

return me;

}

// Overload of the "=" operator

Distance Distance :: operator= (Distance right_operand)

{

return Distance ( nu = right_operand.nu, me = right_operand.me);

}

// provide an overload of "<<" for easy display

ostream& operator<< (ostream& out, const Distance& d)

{

out << "(" << d.number() << ", ";

switch (d.measure())

{

case cm:

out << "cm";

break;

case m:

out << "m";

break;

case km:

out << "km";

break;

}

out << ")";

return out;

}

/*-------------------------------------------------------*\

| test driver for the Distance class |

\*-------------------------------------------------------*/

#ifdef TEST_DISTANCE // .... Distance class .... test driver

int main (void)

{

// create test input

Distance a = Distance (6, cm);

Distance b (4, km);

Distance c (2, m);

Distance d;

Distance e (5, m);

Distance z1 = a = b;

cout << a << endl << b << endl << c << endl << d << endl << e << endl <<
endl;

cout << a <<endl;

cin.ignore();

return 0; // normal termination

}

#endif


Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sat, 10 Apr 2004 14:10:38 GMT in comp.lang.c++, "Chiller" <...@...>
wrote,
My next step is implementing an overload of the "operator=" assignment
operator that will allow one Distance value to be assigned to another
Distance value. I've already written code into the .h and .cpp files to do
this; however, following a compile and execution the output of the test
driver (DISTANCE_TEST) shows the assignment of values is occuring before the
original values have been output to screen.
In your main you define some Distance variables, do an assignment on one
of them, and then output. I don't know why you would expect to see the
original values from before the assignment.
// Overload of the "=" operator

Distance Distance :: operator= (Distance right_operand)

{

return Distance ( nu = right_operand.nu, me = right_operand.me);

}


That could work OK, but it's usually best for operator= to return a
reference to the object being assigned, so that it works as much as
possible like the built in types' assignment operator. I'd suggest

Distance & Distance::operator= (Distance const & right_operand)
{
nu = right_operand.nu;
me = right_operand.me;
return *this;
}

Jul 22 '05 #2

P: n/a
On Sat, 10 Apr 2004 14:10:38 GMT in comp.lang.c++, "Chiller" <...@...>
wrote,
My next step is implementing an overload of the "operator=" assignment
operator that will allow one Distance value to be assigned to another
Distance value. I've already written code into the .h and .cpp files to do
this; however, following a compile and execution the output of the test
driver (DISTANCE_TEST) shows the assignment of values is occuring before the
original values have been output to screen.
In your main you define some Distance variables, do an assignment on one
of them, and then output. I don't know why you would expect to see the
original values from before the assignment.
// Overload of the "=" operator

Distance Distance :: operator= (Distance right_operand)

{

return Distance ( nu = right_operand.nu, me = right_operand.me);

}


That could work OK, but it's usually best for operator= to return a
reference to the object being assigned, so that it works as much as
possible like the built in types' assignment operator. I'd suggest

Distance & Distance::operator= (Distance const & right_operand)
{
nu = right_operand.nu;
me = right_operand.me;
return *this;
}

Jul 22 '05 #3

P: n/a

I've made amendments to the code in this thread to correctly implement the
"operator=" assignment operator. Thanks for those who supplied advice.

I'm now going to implement overloads of six comparison operators (==, !=, <,
<=, >, >=), which will return true or false when comparing two distance
values.

I've written the code below to do the == overload, it compiles correctly but
I'm not sure how to make it return the bool true or false. Could someone
please explain what I'm doing wrong and how to correct the problem.

Thanks,

Distance & Distance :: operator== (Distance const & right_operand)

{

nu = right_operand.nu;

me = right_operand.me;

return *this;

}


Jul 22 '05 #4

P: n/a

I've made amendments to the code in this thread to correctly implement the
"operator=" assignment operator. Thanks for those who supplied advice.

I'm now going to implement overloads of six comparison operators (==, !=, <,
<=, >, >=), which will return true or false when comparing two distance
values.

I've written the code below to do the == overload, it compiles correctly but
I'm not sure how to make it return the bool true or false. Could someone
please explain what I'm doing wrong and how to correct the problem.

Thanks,

Distance & Distance :: operator== (Distance const & right_operand)

{

nu = right_operand.nu;

me = right_operand.me;

return *this;

}


Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.