Re: Class problem
From: Chiller (..._at_...)
Date: 04/10/04
- Next message: Robert Diamond: "Re: Making gcc a windows .exe file"
- Previous message: Robert Diamond: "MultiByteToWideChar converting %[code]... erf..."
- In reply to: Chiller: "Class problem"
- Next in thread: John Harrison: "Re: Class problem"
- Reply: John Harrison: "Re: Class problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 10 Apr 2004 12:32:33 GMT
John,
Thanks again, I included your code in the incorrect spot earlier so it
didn't compile correctly. It now compiles and outputs correctly.
My next step is to add an overload of "operator=" assignment operator to
allow one Distance value to be assigned to another.
I've added the code to both the .h and .cpp files to allow this. It compiles
and runs; however, the output shows that the assignment of values seems to
occur before the original values have been displayed. Could you please have
a look and advise where I'm going wrong and how I can correct the problem.
Thanks
.h file as follows:
************************************
#ifndef DISTANCE_H
#define DISTANCE_H
#include <iostream>
using namespace std;
class Distance
{
public :
Distance (int, int) ; // constructor - takes int values
Distance (void) ; // default - zero
//access member functions
int number (void) const;
int measure (void) const;
//overloads
Distance operator= (Distance) ; // overload of assignment operator
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
.cpp file as follows:
************************************
#include "Distance.h"
#include <iostream>
using namespace std;
/*-------------------------------------------------------*\
| implementation of member functions |
\*-------------------------------------------------------*/
// constructors
Distance :: Distance (int n, int m) : nu(n), me(m) {}
Distance :: Distance (void) : nu(0) {}
enum {cm, m, km};
// access functions
int Distance :: number (void) const
{
return nu;
}
int 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
- Next message: Robert Diamond: "Re: Making gcc a windows .exe file"
- Previous message: Robert Diamond: "MultiByteToWideChar converting %[code]... erf..."
- In reply to: Chiller: "Class problem"
- Next in thread: John Harrison: "Re: Class problem"
- Reply: John Harrison: "Re: Class problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|