C
Chiller
Ok, this is a continuation of a problem I posted on an earlier thread. I've
started another thread because my problem has progressed from the initial
constructor problem into a general method problem.
I'm trying to write a class (Distance.h & Distance.cpp) that will perform
operations on distances. ie compare, add, subtract etc distance values.
I'm still in the early stages of developing the class; however, I'm stuck
with how best to enter the values. The class needs to accept a number value
and a unit of measure (cm, m or km). This should be input as
Distance a (5, m);
Distance b (8, km); etc
What I've written so far will accept the values; however, when I output the
values to screen I don't get the display I'm looking for. I realise this is
because of the way I've used "enum"; however, I'm not sure on how to use it
correctly. The input is given at the botom of the Distance.cpp file under
TEST_DISTANCE, this is a test driver that will need to be declared in the
preprocessor to be able to complile correctly.
I'd greatly appreciate any advice on where I'm going wrong and how to
fix/improve what I've written. I'm still very much learning so I'd
appreciate clear and simple explanations and examples of how to make this
work.
The code is as follows:
Thanks
Distance.h
************************************
#ifndef DISTANCE_H
#define DISTANCE_H
#include <iostream>
using namespace std;
class Distance
{
public :
Distance (int, int) ; // constructor - takes int values
Distance (int) ; // constructor - takes int value
Distance (void) ; // default - zero
//access member functions
int number (void) const;
int measure (void) const;
private :
int nu ; // the value
int me ; // the unit of measure (m)
} ;
// provide an overload of "<<" for easy display
ostream& operator<< (ostream&, const Distance&);
#endif
Distance.cpp as follows:
*****************************************
#include "Distance.h"
#include <iostream>
using namespace std;
/*-------------------------------------------------------*\
| implementation of member functions |
\*-------------------------------------------------------*/
// constructor
Distance :: Distance (int n, int m) : nu(n), me(m) {}
Distance :: Distance (int n) : nu(n) {}
Distance :: Distance (void) : nu(0) {}
enum
{
m,
km,
};
// access functions
int Distance :: number (void) const
{
return nu;
}
int Distance :: measure (void) const
{
return me;
}
// provide an overload of "<<" for easy display
ostream& operator<< (ostream& out, const 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);
Distance b (4);
Distance c (2);
Distance d;
Distance e (5, m);
cout << a << endl << b << endl << c << endl << d << endl << e << endl;
cin.ignore();
return 0; // normal termination
}
#endif
started another thread because my problem has progressed from the initial
constructor problem into a general method problem.
I'm trying to write a class (Distance.h & Distance.cpp) that will perform
operations on distances. ie compare, add, subtract etc distance values.
I'm still in the early stages of developing the class; however, I'm stuck
with how best to enter the values. The class needs to accept a number value
and a unit of measure (cm, m or km). This should be input as
Distance a (5, m);
Distance b (8, km); etc
What I've written so far will accept the values; however, when I output the
values to screen I don't get the display I'm looking for. I realise this is
because of the way I've used "enum"; however, I'm not sure on how to use it
correctly. The input is given at the botom of the Distance.cpp file under
TEST_DISTANCE, this is a test driver that will need to be declared in the
preprocessor to be able to complile correctly.
I'd greatly appreciate any advice on where I'm going wrong and how to
fix/improve what I've written. I'm still very much learning so I'd
appreciate clear and simple explanations and examples of how to make this
work.
The code is as follows:
Thanks
Distance.h
************************************
#ifndef DISTANCE_H
#define DISTANCE_H
#include <iostream>
using namespace std;
class Distance
{
public :
Distance (int, int) ; // constructor - takes int values
Distance (int) ; // constructor - takes int value
Distance (void) ; // default - zero
//access member functions
int number (void) const;
int measure (void) const;
private :
int nu ; // the value
int me ; // the unit of measure (m)
} ;
// provide an overload of "<<" for easy display
ostream& operator<< (ostream&, const Distance&);
#endif
Distance.cpp as follows:
*****************************************
#include "Distance.h"
#include <iostream>
using namespace std;
/*-------------------------------------------------------*\
| implementation of member functions |
\*-------------------------------------------------------*/
// constructor
Distance :: Distance (int n, int m) : nu(n), me(m) {}
Distance :: Distance (int n) : nu(n) {}
Distance :: Distance (void) : nu(0) {}
enum
{
m,
km,
};
// access functions
int Distance :: number (void) const
{
return nu;
}
int Distance :: measure (void) const
{
return me;
}
// provide an overload of "<<" for easy display
ostream& operator<< (ostream& out, const 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);
Distance b (4);
Distance c (2);
Distance d;
Distance e (5, m);
cout << a << endl << b << endl << c << endl << d << endl << e << endl;
cin.ignore();
return 0; // normal termination
}
#endif