473,385 Members | 1,925 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.

Convertion problem


I'm in the process of writing a class that performs functions on a Distance
object. The object is created by entering details as "Distance a (5, km)" or
"Distance b (3, cm)" etc.

I wish to write a function that I can call to "as required by comparison
functions" to convert any value to a mm value so that when I'm comparing
Distance objects the values will all be the same.

The code I've come up with is below; however, it doesn't work correctly.
When I try and compile it the compiler advises it must return a value;
however, I simply want to replace the object that called to it with the new
values, ie Distance a (2, cm) to Distance a (20, mm) etc.

The constructor used in the class is as follows:

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

My function as follows:

int Distance::to_mm()
{

if ( me == km)

{

nu = nu/1000000;

me = mm;

}

if (me == m)

{

nu = nu/1000;

me = mm;

}

if ( me == cm)

{

nu = nu/10;

me = m;

}

}

Thanks for any help

Jul 22 '05 #1
6 2211
"Chiller" <...@...> wrote...

I'm in the process of writing a class that performs functions on a Distance object. The object is created by entering details as "Distance a (5, km)" or "Distance b (3, cm)" etc.

I wish to write a function that I can call to "as required by comparison
functions" to convert any value to a mm value so that when I'm comparing
Distance objects the values will all be the same.

The code I've come up with is below; however, it doesn't work correctly.
When I try and compile it the compiler advises it must return a value;
however, I simply want to replace the object that called to it with the new values, ie Distance a (2, cm) to Distance a (20, mm) etc.

The constructor used in the class is as follows:

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

My function as follows:

int Distance::to_mm()
You declared your function as returning an int. That means that inside
the body of your function _all_ control paths have to lead to a statement
of the kind

return <some_expression_convertible_to_int>;

If you didn't intend to return anything from your function 'to_mm', you
ought to declare it 'void':

void Distance::to_mm()
{

if ( me == km)

{

nu = nu/1000000;

me = mm;

}

if (me == m)
I'd recommend

else if (me == m)

{

nu = nu/1000;

me = mm;

}

if ( me == cm)
The same here.

{

nu = nu/10;

me = m;

}

}

Thanks for any help


HTH

Victor
Jul 22 '05 #2

I've implemented the function to change all values to "mm" when calling to a
comparison function; however, it just isn't working. In my TEST_DRIVER at
the bottom of the .cpp file I've inluded a greater than test for two values;
however it believes that 5 m is greater than 4 km, so I've obviously done
something wrong.

I've included my revised .cpp file and .h file below. I'd appreciate some
advice on what I'm doing wrong.

Thanks for any help
..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(2) {}

enum {mm, cm, m, km};

// access functions

int Distance :: number (void)

{

return nu;

}

char Distance :: measure (void)

{

return me;

}

//Convertion function to ensure all values are in the same unit of measure

void Distance::to_mm()

{

if ( me == km)

{

nu = nu/1000000;

me = mm;

}

else if (me == m)

{

nu = nu/1000;

me = mm;

}

else if (me == cm)

{

nu = nu/10;

me = cm;

}

}

// Overload of the "=" operator

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

{

nu = right_operand.nu;

me = right_operand.me;

return *this;

}

// Overload of the "==" operator

bool Distance::operator == ( Distance const & rhs )

{

to_mm();

return ( nu == rhs.nu && me == rhs.me );

}

//Overload of the != operator

bool Distance::operator != ( Distance const & rhs )

{

to_mm();

return ( nu != rhs.nu && me != rhs.me );

}

//Overload of the < operator

bool Distance::operator < (Distance const & rhs)

{

to_mm();

return ( nu < rhs.nu && me < rhs.me );

}

//Overload of the <= operator

bool Distance::operator <= (Distance const & rhs)

{

to_mm();

return (nu <= rhs.nu && me <= rhs.me);

}

//Overload of the > operator

bool Distance::operator > (Distance const & rhs)

{

to_mm();

return (nu > rhs.nu && me > rhs.me);

}

//Overload of the >= operator

bool Distance::operator >= (Distance const & rhs)

{

to_mm();

return (nu >= rhs.nu && me >= rhs.me);

}

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

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

{

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

switch (d.measure())

{

case mm:

out << "mm";

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 << endl;

cout << "The results of comparing various Distance values :" << endl;

cout << "Distance a == Distance e : ";

cout << (a == e ? "true" : "false") << endl;

cout << "Distance a != Distance e : ";

cout << (a != e ? "true" : "false") << endl;

cout << "Distance a < Distance c : ";

cout << (a < c ? "true" : "false") << endl;

cout << "Distance a <= Distance c : ";

cout << (a <= c ? "true" : "false") << endl;

cout << "Distance a > Distance c : ";

cout << (a > c ? "true" : "false") << endl;

cout << "Distance a >= Distance b : ";

cout << (a >= c ? "true" : "false") << endl;

cout << "Distance a > Distance e : ";

cout << (a > e ? "true" : "false") << endl;

cin.ignore();

return 0; // normal termination

}

#endif
..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);

char measure (void);

//overloads

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

bool operator == ( Distance const &rhs );

bool operator != ( Distance const &rhs );

bool operator < ( Distance const &rhs );

bool operator <= ( Distance const &rhs );

bool operator > ( Distance const &rhs );

bool operator >= ( Distance const &rhs );

void Distance::to_mm();

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


Jul 22 '05 #3
"Chiller" <...@...> wrote in message
news:d1******************************@news.teranew s.com...
using namespace std;
Not in header files.

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

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

enum {mm, cm, m, km};
enum Measure {mm=1, cm=10, m=1000, km=1000000};

Furthermore, if you do operator>, operator-, etc most often, it's best to
store the result in millimeters.

Distance :: Distance (int n=0, Measure m=mm) : d_n(n*m) {}

Ultimately it's your call. BTW, member function measure should probably
return a Measure, not a char. And the constructor should take a Measure.

void Distance::to_mm()

{

if ( me == km)

{

nu = nu/1000000;

me = mm;

}
Seems you want multiplication?

// Overload of the "=" operator

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

{

nu = right_operand.nu;

me = right_operand.me;

return *this;

}
Compiler generated constructor is fine; no need to write your own.

// Overload of the "==" operator

bool Distance::operator == ( Distance const & rhs )

{

to_mm();

return ( nu == rhs.nu && me == rhs.me );

}
Really? I think 1000mm equals 1m, so

Distance(1000,Distance::mm) == Distance(1,m)

ought to return true. So the comparison function ought to take the measure
into account. If you store all values in mm inside the class, then
operator== is very easy to write.

//Overload of the != operator

bool Distance::operator != ( Distance const & rhs )

{

to_mm();

return ( nu != rhs.nu && me != rhs.me );

}
Should probably be implemented in terms of operator==.

return !(lhs==rhs);

Also, probably make operator== and the others as non-members for symmetry if
there is a one-arg non-explicit constructor.

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

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

{

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

switch (d.measure())

{

case mm:

out << "mm";

case cm:

out << "cm";

break;

case m:

out << "m";

break;

case km:

out << "km";

break;

}

out << ")";

return out;

}


If you used

enum {mm, cm, m, km};

then just

out << "(" << d.number() << ", " << g_table[d.measure()] << ')';

where

const char g_table[4] = { "mm", "cm", "m", "km" };

What about operator>>, which is harder?
Jul 22 '05 #4
"Chiller" <...@...> wrote in message
news:cf******************************@news.teranew s.com...
Distance :: Distance (int n, char m) : nu(n), me(m) {}

My function as follows:

int Distance::to_mm()
{

if ( me == km)

{

nu = nu/1000000;

me = mm;

}

if (me == m)

{

nu = nu/1000;

me = mm;

}

if ( me == cm)

{

nu = nu/10;

me = m;

}

}


Should all the multiplication signs be division? Also, the last else says
me=m, but the others are me=mm.
Jul 22 '05 #5
Distances are continuous. Storing them as integers is silly.
And as Siemel Naran seems to have wanted to say, all those
divisions should be multiplications.

--
Regards,
Buster.
Jul 22 '05 #6
"Buster" <no***@nowhere.com> wrote in message
news:c5**********@newsg1.svr.pol.co.uk...
Distances are continuous. Storing them as integers is silly.
And as Siemel Naran seems to have wanted to say, all those
divisions should be multiplications.


Storing them as integers might make sense for some applications.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Petr Studenovsky | last post by:
Hello I made small GUI test program in Visual Basic NET for SQL communication. I need some help with these problems. problem 1) If I change some settings in datagrid from properties window...
0
by: Konstantin Kivi | last post by:
I have problems getting character set convertion to work in 4.1.4 gamma under linux. Can sombody point me to the online recource other than standard MySQL doc so that I get more information.
6
by: Chiller | last post by:
I'm in the process of writing a class that performs functions on a Distance object. The object is created by entering details as "Distance a (5, km)" or "Distance b (3, cm)" etc. I wish to write...
1
by: Vidar | last post by:
I have a problem in dotNET XSL convertion object. (XMLTRANSFORM) It won't convert UTF-8 to ISO-8859-1 I use this stylesheet for konvertion: <?xml version="1.0" encoding="ISO-8859-1"?> <!--...
0
by: McKoy | last post by:
Hi, I'm translating some code from C++ to C#, but I don't know how to change the code below to get it working under C# ...., I've tried explicity convertion, but without any results :( Please...
4
by: juli | last post by:
Good afternoon! I have an error while trying to convert to DateTime ,here is the code and the error: ArrayList al = new ArrayList(); temp_str=Line.Split(' '); al.Add(temp_str); //...
4
by: kaikai | last post by:
Hi, I've got a problem while trying to make my source code clean. In a template: template<typename T> class A { public: T data; };
7
by: Filips Benoit | last post by:
Dear all, Tables: COMPANY: COM_ID, COM_NAME, ..... PROPERTY: PRP_ID, PRP_NAME, PRP_DATATYPE_ID, PRP_DEFAULT_VALUE ( nvarchar) COMPANY_PROPERTY: CPROP_COM_ID, CPROP_PRP_ID, CPROP_VALUE...
4
by: HackerisNewKnight | last post by:
Hello, 1) First problem. In my project i need to convert from char* to BYTE*( BYTE is unsigned char), is there any way to make the convertion? BYTE* bite; char* ch="help me, please"; bite =...
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: 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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.