C
Chiller
I'm now getting close to finishing my Distance class. In the code below I
have included a number of overload operators that test for equality etc.
I've also added more code in the TEST_DISTANCE driver to test the code.
I now have 2 remaining problems:
1. The code will allow for the input of distances in either cm, m or km
values; however, it isn't smart enough to convert values of different types
(ie cm and km) for correct follow on processing. I would like to add
something to my class that will take any of the three inputs and convert
them to metres for all subsequent operations.
Could someone please give me some examples on how I can achive this.
2. I would also like to include a unary operator- operation which will
return the negative of a Distance value. All my attempts thus far have only
subtracted values. Could someone please advise me on how I must implement
this.
Thanks
..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) const;
char measure (void) const;
//overloads
Distance & Distance:perator= (Distance const & right_operand);
bool operator == ( Distance const &rhs ) const;
bool operator != ( Distance const &rhs ) const;
bool operator < ( Distance const &rhs ) const;
bool operator <= ( Distance const &rhs ) const;
bool operator > ( Distance const &rhs ) const;
bool operator >= ( Distance const &rhs ) const;
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
..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(1) {}
enum {cm, m, km};
// access functions
int Distance :: number (void) const
{
return nu;
}
char Distance :: measure (void) const
{
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 ) const
{
return ( nu == rhs.nu && me == rhs.me );
}
//Overload of the != operator
bool Distance:perator != ( Distance const & rhs ) const
{
return ( nu != rhs.nu && me != rhs.me );
}
//Overload of the < operator
bool Distance:perator < (Distance const & rhs) const
{
return ( nu < rhs.nu && me < rhs.me );
}
//Overload of the <= operator
bool Distance:perator <= (Distance const & rhs) const
{
return (nu <= rhs.nu && me <= rhs.me);
}
//Overload of the > operator
bool Distance:perator > (Distance const & rhs) const
{
return (nu > rhs.nu && me > rhs.me);
}
//Overload of the >= operator
bool Distance:perator >= (Distance const & rhs) const
{
return (nu >= rhs.nu && me >= rhs.me);
}
// provide an overload of "<<" for easy display
ostream& operator<< (ostream& out, const Distance& d)
{
out << "(" << d.number() << ", ";
switch (d.measure())
{
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;
cin.ignore();
return 0; // normal termination
}
#endif
have included a number of overload operators that test for equality etc.
I've also added more code in the TEST_DISTANCE driver to test the code.
I now have 2 remaining problems:
1. The code will allow for the input of distances in either cm, m or km
values; however, it isn't smart enough to convert values of different types
(ie cm and km) for correct follow on processing. I would like to add
something to my class that will take any of the three inputs and convert
them to metres for all subsequent operations.
Could someone please give me some examples on how I can achive this.
2. I would also like to include a unary operator- operation which will
return the negative of a Distance value. All my attempts thus far have only
subtracted values. Could someone please advise me on how I must implement
this.
Thanks
..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) const;
char measure (void) const;
//overloads
Distance & Distance:perator= (Distance const & right_operand);
bool operator == ( Distance const &rhs ) const;
bool operator != ( Distance const &rhs ) const;
bool operator < ( Distance const &rhs ) const;
bool operator <= ( Distance const &rhs ) const;
bool operator > ( Distance const &rhs ) const;
bool operator >= ( Distance const &rhs ) const;
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
..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(1) {}
enum {cm, m, km};
// access functions
int Distance :: number (void) const
{
return nu;
}
char Distance :: measure (void) const
{
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 ) const
{
return ( nu == rhs.nu && me == rhs.me );
}
//Overload of the != operator
bool Distance:perator != ( Distance const & rhs ) const
{
return ( nu != rhs.nu && me != rhs.me );
}
//Overload of the < operator
bool Distance:perator < (Distance const & rhs) const
{
return ( nu < rhs.nu && me < rhs.me );
}
//Overload of the <= operator
bool Distance:perator <= (Distance const & rhs) const
{
return (nu <= rhs.nu && me <= rhs.me);
}
//Overload of the > operator
bool Distance:perator > (Distance const & rhs) const
{
return (nu > rhs.nu && me > rhs.me);
}
//Overload of the >= operator
bool Distance:perator >= (Distance const & rhs) const
{
return (nu >= rhs.nu && me >= rhs.me);
}
// provide an overload of "<<" for easy display
ostream& operator<< (ostream& out, const Distance& d)
{
out << "(" << d.number() << ", ";
switch (d.measure())
{
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;
cin.ignore();
return 0; // normal termination
}
#endif