Re: Class problem

From: Chiller (..._at_...)
Date: 04/10/04


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



Relevant Pages

  • misc idea: Mini-LZ
    ... one can copy the files, however, for finer grained usages (and where deflate is overkill), this is not optimal. ... in truth, the encoder may not work at all, or may be slow. ... next byte is the low 8 bits of distance. ... int MiniLZ2_Decode ...
    (comp.compression)
  • Re: A Lighting Model
    ... a good point of the inverse square law is that it's ... your distance calculations. ... slowdist(int x, int y) ...
    (rec.games.roguelike.development)
  • Re: Constructor problem
    ... overloaded function differs only by return type from 'int ... Distance.h: see declaration of 'Distance::measure' ... int measure (void) const; ... int Distance:: number const ...
    (comp.lang.cpp)
  • Re: Convertion problem
    ... I've included my revised .cpp file and .h file below. ... int Distance:: number ... char Distance:: measure (void) ...
    (comp.lang.cpp)
  • Re: Constructor problem
    ... all the values are int values. ... ie, if I input Distance a I'd like to be able to ... int measure (void) const; ...
    (comp.lang.cpp)

Loading