Doing something wrong

C

Chiller

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
 
D

Daniel T.

Chiller said:
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...
 

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

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top