C
Chiller
Ok, thanks to some good assistance/advice from people in this group I've
been able to further develop my Distance class.
Since previous posts I've refined my code to accept the unit measurement as
a char rather than incorrectly representing it as an int. I've done this
because I want to develop the class so that it will be able to convert
between values, ie if I add 500 m to 1 km I'd like a correct result given in
metres (1500 m in this case). I'd appreciate any suggestion on how best to
do this expanding on the code I've already written.
My next step is implementing an overload of the "operator=" assignment
operator that will allow one Distance value to be assigned to another
Distance value. I've already written code into the .h and .cpp files to do
this; however, following a compile and execution the output of the test
driver (DISTANCE_TEST) shows the assignment of values is occuring before the
original values have been output to screen.
I'd appreciate any advice/suggestions on what I'm doing wrong and how I can
go about fixing the problem. I'm only a novice so please make any
suggestions plain and simple. Further to this, please explain any code
examples so that I can understand and learn. The .h file and .cpp file is
below.
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 operator= (Distance) ; // overload of assignment operator
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 :: operator= (Distance right_operand)
{
return Distance ( nu = right_operand.nu, me = right_operand.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;
cin.ignore();
return 0; // normal termination
}
#endif
been able to further develop my Distance class.
Since previous posts I've refined my code to accept the unit measurement as
a char rather than incorrectly representing it as an int. I've done this
because I want to develop the class so that it will be able to convert
between values, ie if I add 500 m to 1 km I'd like a correct result given in
metres (1500 m in this case). I'd appreciate any suggestion on how best to
do this expanding on the code I've already written.
My next step is implementing an overload of the "operator=" assignment
operator that will allow one Distance value to be assigned to another
Distance value. I've already written code into the .h and .cpp files to do
this; however, following a compile and execution the output of the test
driver (DISTANCE_TEST) shows the assignment of values is occuring before the
original values have been output to screen.
I'd appreciate any advice/suggestions on what I'm doing wrong and how I can
go about fixing the problem. I'm only a novice so please make any
suggestions plain and simple. Further to this, please explain any code
examples so that I can understand and learn. The .h file and .cpp file is
below.
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 operator= (Distance) ; // overload of assignment operator
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 :: operator= (Distance right_operand)
{
return Distance ( nu = right_operand.nu, me = right_operand.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;
cin.ignore();
return 0; // normal termination
}
#endif