473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Overload operator problem

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
4 1216
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
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: bsaucer | last post by:
I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an operator having arguments having the other class...
1
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this...
3
by: Piotre Ugrumov | last post by:
I have done the overload on the operator >> and << in the class Attore. These 2 overload work correctly. I have done the overload of the same overload in the class Film. The class film ha inside...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
4
by: Chiller | last post by:
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...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
5
by: phiefer3 | last post by:
I'm currently a student, but this problem isn't directly related to what I have to do on an assignment. It's just a problem I've had with some supporting features. First of all, I'm using MSVS...
3
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as...
2
by: Markjan | last post by:
I have a problem with classes and structures in classes (C++) I have to overload operator . class Data { public: class Proxy { //for overload operator Data& _a; int...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.