Re: Convertion problem

From: Chiller (..._at_...)
Date: 04/12/04


Date: Mon, 12 Apr 2004 05:43:50 GMT


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



Relevant Pages

  • Re: Constructor problem
    ... all the values are int values. ... ie, if I input Distance a I'd like to be able to ... int measure (void) const; ...
    (comp.lang.cpp)
  • Re: Constructor problem
    ... overloaded function differs only by return type from 'int ... Distance.h: see declaration of 'Distance::measure' ... int measure (void) const; ... int Distance:: number const ...
    (comp.lang.cpp)
  • Re: Class problem
    ... It now compiles and outputs correctly. ... allow one Distance value to be assigned to another. ... int measure (void) const; ...
    (comp.lang.cpp)
  • Re: Value convertion problem
    ... however, I think I'm doing the convertions ... Also, when outputing the Distance objects, ... Then I would not return anything and make the conv return void. ...
    (alt.comp.lang.learn.c-cpp)
  • Class problem
    ... ie compare, add, subtract etc distance values. ... int measure (void) const; ...
    (comp.lang.cpp)