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:perator= (Distance const & right_operand)
{
nu = right_operand.nu;
me = right_operand.me;
return *this;
}
// Overload of the "==" operator
bool Distance:perator == ( Distance const & rhs )
{
conv();
return ( nu == rhs.nu);
}
//Overload of the != operator
bool Distance:perator != ( Distance const & rhs )
{
conv();
return ( nu != rhs.nu);
}
//Overload of the < operator
bool Distance:perator < (Distance const & rhs)
{
conv();
return ( nu < rhs.nu);
}
//Overload of the <= operator
bool Distance:perator <= (Distance const & rhs)
{
conv();
return (nu <= rhs.nu);
}
//Overload of the > operator
bool Distance:perator > (Distance const & rhs)
{
conv();
return (nu > rhs.nu);
}
//Overload of the >= operator
bool Distance:perator >= (Distance const & rhs)
{
conv();
return (nu >= rhs.nu);
}
//Overload of the + (addition) operator
Distance Distance:perator+ (Distance right_operand)
{
conv();
return Distance (nu + right_operand.nu, me);
}
//Overload of the - (subtraction) operator
Distance Distance: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: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:perator+ (Distance right_operand);
Distance Distance: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
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:perator= (Distance const & right_operand)
{
nu = right_operand.nu;
me = right_operand.me;
return *this;
}
// Overload of the "==" operator
bool Distance:perator == ( Distance const & rhs )
{
conv();
return ( nu == rhs.nu);
}
//Overload of the != operator
bool Distance:perator != ( Distance const & rhs )
{
conv();
return ( nu != rhs.nu);
}
//Overload of the < operator
bool Distance:perator < (Distance const & rhs)
{
conv();
return ( nu < rhs.nu);
}
//Overload of the <= operator
bool Distance:perator <= (Distance const & rhs)
{
conv();
return (nu <= rhs.nu);
}
//Overload of the > operator
bool Distance:perator > (Distance const & rhs)
{
conv();
return (nu > rhs.nu);
}
//Overload of the >= operator
bool Distance:perator >= (Distance const & rhs)
{
conv();
return (nu >= rhs.nu);
}
//Overload of the + (addition) operator
Distance Distance:perator+ (Distance right_operand)
{
conv();
return Distance (nu + right_operand.nu, me);
}
//Overload of the - (subtraction) operator
Distance Distance: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: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:perator+ (Distance right_operand);
Distance Distance: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