Overload operator problem

C

Chiller

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
 
D

David Harmon

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::eek:perator= (Distance const & right_operand)
{
nu = right_operand.nu;
me = right_operand.me;
return *this;
}
 
C

Chiller

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;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top