Doing something wrong

Discussion in 'C++' started by Chiller, Apr 12, 2004.

  1. Chiller

    Chiller Guest

    Ok, I've implemented a few changes to the code and the bool functions now
    seem to be functioning correctly; however, I think I'm doing the convertions
    incorrectly because the values printed out from my TEST_DISTANCE driver
    haven't converted correctly. I'm possibly using enum incorrectly, so I'd
    appreciate
    some assistance with its use. Also, when outputing the Distance objects,
    numbers are displayed but the value isn't displayed correctly. ie I'm
    getting erroneous characters rather than a simple m etc.

    Can I please ask someone to compile my code and have a look where I'm going
    wrong. I've tried, but I've hit a brick wall so to say. I'm still very much
    learning so can I please ask for simple examples and explanations. I'm not
    too concerned with complicated refined code.

    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('m') {}

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



    // Convert mm value into metres
    int Distance::conv()
    {
    return (nu = nu * 1000, me ='m');
    }

    // access functions
    int Distance :: number (void)
    {
    return nu;
    }

    char Distance :: measure (void)
    {
    return me;
    }

    // Overload of the "=" operator
    Distance & Distance::eek:perator= (Distance const & right_operand)

    {
    nu = right_operand.nu;
    me = right_operand.me;
    return *this;
    }

    // Overload of the "==" operator
    bool Distance::eek:perator == ( Distance const & rhs )
    {
    conv();
    return ( nu == rhs.nu);
    }

    //Overload of the != operator
    bool Distance::eek:perator != ( Distance const & rhs )
    {
    conv();
    return ( nu != rhs.nu);
    }

    //Overload of the < operator
    bool Distance::eek:perator < (Distance const & rhs)
    {
    conv();
    return ( nu < rhs.nu);
    }

    //Overload of the <= operator
    bool Distance::eek:perator <= (Distance const & rhs)
    {
    conv();
    return (nu <= rhs.nu);
    }

    //Overload of the > operator
    bool Distance::eek:perator > (Distance const & rhs)
    {
    conv();
    return (nu > rhs.nu);
    }

    //Overload of the >= operator
    bool Distance::eek:perator >= (Distance const & rhs)
    {
    conv();
    return (nu >= rhs.nu);
    }

    //Overload of the + (addition) operator
    Distance Distance::eek:perator+ (Distance right_operand)
    {
    conv();
    return Distance (nu + right_operand.nu, me);
    }

    //Overload of the - (subtraction) operator
    Distance Distance::eek:perator- (Distance right_operand)
    {
    conv();
    return Distance (nu - right_operand.nu, me);
    }

    // provide an overload of "<<" for easy display
    ostream& operator<< (ostream& out, Distance& d)
    {
    out << "(" << d.number() <<", " << d.measure() << ")";
    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 b > Distance e : ";
    cout << (b > e ? "true" : "false") << endl;

    Distance z2 = a + b;
    cout << "\nDistance a plus Distance b = " << z2 <<endl << endl;

    Distance z3 = b - c;
    cout << "\nDistance b minus Distance c = " << z3 <<endl << 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::eek:perator= (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 );

    Distance Distance::eek:perator+ (Distance right_operand);
    Distance Distance::eek:perator- (Distance right_operand);

    int Distance::conv();

    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
    Chiller, Apr 12, 2004
    #1
    1. Advertising

  2. Chiller

    Daniel T. Guest

    In article <>,
    "Chiller" <...@...> wrote:

    > Ok, I've implemented a few changes to the code and the bool functions now
    > seem to be functioning correctly; however, I think I'm doing the convertions
    > incorrectly because the values printed out from my TEST_DISTANCE driver
    > haven't converted correctly. I'm possibly using enum incorrectly, so I'd
    > appreciate
    > some assistance with its use. Also, when outputing the Distance objects,
    > numbers are displayed but the value isn't displayed correctly. ie I'm
    > getting erroneous characters rather than a simple m etc.
    >
    > Can I please ask someone to compile my code and have a look where I'm going
    > wrong. I've tried, but I've hit a brick wall so to say. I'm still very much
    > learning so can I please ask for simple examples and explanations. I'm not
    > too concerned with complicated refined code.
    >
    > Thanks for any help.


    > /*-------------------------------------------------------*\
    >
    > | 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);


    In the above code, your c_tor is being used incorrectly. The second
    argument is supposed to be a char but you are sending it an enum type.
    I'm suprised your compiler isn't at least warning you about it...
    Daniel T., Apr 12, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    0
    Views:
    461
  2. Michael Sparks
    Replies:
    6
    Views:
    468
    Michael Sparks
    Sep 21, 2005
  3. Jp Calderone
    Replies:
    1
    Views:
    379
    Michael Sparks
    Sep 20, 2005
  4. Simon Brooke
    Replies:
    5
    Views:
    554
  5. Eric Lilja

    Am I doing something wrong with printf() here?

    Eric Lilja, Jan 8, 2005, in forum: C Programming
    Replies:
    9
    Views:
    282
    Jonathan Burd
    Jan 10, 2005
Loading...

Share This Page