Class problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chiller

    Class problem

    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





  • Nick Hounsome

    #2
    Re: Class problem


    "Chiller" <...@...> wrote in message
    news:d980cd9776 cb95d9f7be97949 cbb6316@news.te ranews.com...[color=blue]
    > Ok, this is a continuation of a problem I posted on an earlier thread.[/color]
    I've[color=blue]
    > 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[/color]
    value[color=blue]
    > 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[/color]
    the[color=blue]
    > values to screen I don't get the display I'm looking for. I realise this[/color]
    is[color=blue]
    > because of the way I've used "enum"; however, I'm not sure on how to use[/color]
    it[color=blue]
    > 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 :
    >[/color]

    enum Unit { m,km };
    [color=blue]
    > Distance (int, int) ; // constructor - takes int values[/color]

    Distance(size_t distance, Unit unit);
    [color=blue]
    >
    > Distance (int) ; // constructor - takes int value
    >[/color]

    Is this supposed to default the distance or the unit?
    Use enum for unit.
    [color=blue]
    > Distance (void) ; // default - zero[/color]

    What unit?
    [color=blue]
    >
    > //access member functions
    >
    > int number (void) const;
    >
    > int measure (void) const;
    >
    >
    > private :
    >
    > int nu ; // the value[/color]

    size_t nu;
    [color=blue]
    >
    > int me ; // the unit of measure (m)[/color]

    Unit me;[color=blue]
    >
    > } ;
    >
    > // provide an overload of "<<" for easy display
    >
    > ostream& operator<< (ostream&, const Distance&);[/color]

    and
    ostream& operator<<(ostr eam&,const Distance::Unit& ); // output lookup of
    string
    istream& operator>>(istr eam&,const Distance::Unit& ); // read string and
    lookup value
    istream& operator>>(istr eam&,const Distance&); // use above func

    [color=blue]
    >
    > #endif[/color]

    It seems unlikely to me that you actually want dynamic units.
    I suggest that you fix and document the units and handle unit conversion
    only on input.
    If you really want different units internally as well a sexternally then
    consider a template class
    parameterized by the enum.


    Comment

    • Nick Hounsome

      #3
      Re: Class problem


      "Chiller" <...@...> wrote in message
      news:d980cd9776 cb95d9f7be97949 cbb6316@news.te ranews.com...[color=blue]
      > Ok, this is a continuation of a problem I posted on an earlier thread.[/color]
      I've[color=blue]
      > 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[/color]
      value[color=blue]
      > 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[/color]
      the[color=blue]
      > values to screen I don't get the display I'm looking for. I realise this[/color]
      is[color=blue]
      > because of the way I've used "enum"; however, I'm not sure on how to use[/color]
      it[color=blue]
      > 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 :
      >[/color]

      enum Unit { m,km };
      [color=blue]
      > Distance (int, int) ; // constructor - takes int values[/color]

      Distance(size_t distance, Unit unit);
      [color=blue]
      >
      > Distance (int) ; // constructor - takes int value
      >[/color]

      Is this supposed to default the distance or the unit?
      Use enum for unit.
      [color=blue]
      > Distance (void) ; // default - zero[/color]

      What unit?
      [color=blue]
      >
      > //access member functions
      >
      > int number (void) const;
      >
      > int measure (void) const;
      >
      >
      > private :
      >
      > int nu ; // the value[/color]

      size_t nu;
      [color=blue]
      >
      > int me ; // the unit of measure (m)[/color]

      Unit me;[color=blue]
      >
      > } ;
      >
      > // provide an overload of "<<" for easy display
      >
      > ostream& operator<< (ostream&, const Distance&);[/color]

      and
      ostream& operator<<(ostr eam&,const Distance::Unit& ); // output lookup of
      string
      istream& operator>>(istr eam&,const Distance::Unit& ); // read string and
      lookup value
      istream& operator>>(istr eam&,const Distance&); // use above func

      [color=blue]
      >
      > #endif[/color]

      It seems unlikely to me that you actually want dynamic units.
      I suggest that you fix and document the units and handle unit conversion
      only on input.
      If you really want different units internally as well a sexternally then
      consider a template class
      parameterized by the enum.


      Comment

      • John Harrison

        #4
        Re: Class problem


        "Chiller" <...@...> wrote in message
        news:d980cd9776 cb95d9f7be97949 cbb6316@news.te ranews.com...[color=blue]
        > Ok, this is a continuation of a problem I posted on an earlier thread.[/color]
        I've[color=blue]
        > 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[/color]
        value[color=blue]
        > 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[/color]
        the[color=blue]
        > values to screen I don't get the display I'm looking for. I realise this[/color]
        is[color=blue]
        > because of the way I've used "enum"; however, I'm not sure on how to use[/color]
        it[color=blue]
        > correctly.[/color]

        I think the answer I gave earlier works, was there a problem with it?

        In any case see below.

        [snip]
        [color=blue]
        >
        > 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() << ")" ;[/color]

        Replace above line with this

        out << "(" << d.number() << ",";
        switch (d.measure())
        {
        case m:
        out << "m";
        break;
        case km:
        out << "km";
        break;
        }
        out << ")";
        [color=blue]
        >
        > return out;
        >
        > }[/color]

        It's pretty simple, I think maybe you are looking for a more automatic
        solution which doesn't exist.

        Also have a look at my earlier post for a slightly more elegant solution.

        john


        Comment

        • John Harrison

          #5
          Re: Class problem


          "Chiller" <...@...> wrote in message
          news:d980cd9776 cb95d9f7be97949 cbb6316@news.te ranews.com...[color=blue]
          > Ok, this is a continuation of a problem I posted on an earlier thread.[/color]
          I've[color=blue]
          > 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[/color]
          value[color=blue]
          > 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[/color]
          the[color=blue]
          > values to screen I don't get the display I'm looking for. I realise this[/color]
          is[color=blue]
          > because of the way I've used "enum"; however, I'm not sure on how to use[/color]
          it[color=blue]
          > correctly.[/color]

          I think the answer I gave earlier works, was there a problem with it?

          In any case see below.

          [snip]
          [color=blue]
          >
          > 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() << ")" ;[/color]

          Replace above line with this

          out << "(" << d.number() << ",";
          switch (d.measure())
          {
          case m:
          out << "m";
          break;
          case km:
          out << "km";
          break;
          }
          out << ")";
          [color=blue]
          >
          > return out;
          >
          > }[/color]

          It's pretty simple, I think maybe you are looking for a more automatic
          solution which doesn't exist.

          Also have a look at my earlier post for a slightly more elegant solution.

          john


          Comment

          • Chiller

            #6
            Re: Class problem

            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.n u, me = right_operand.m e);

            }

            // 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






            Comment

            • Chiller

              #7
              Re: Class problem

              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.n u, me = right_operand.m e);

              }

              // 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






              Comment

              • John Harrison

                #8
                Re: Class problem


                "Chiller" <...@...> wrote in message
                news:835125bcd5 a9215b8edd83fae 621e778@news.te ranews.com...[color=blue]
                > 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.[/color]

                There's no need to do that. You only need to do write an operator= if the
                default one isn't correct. In your case the default assignment operator will
                work fine.

                For the record however here's how you should define operator=

                Distance& Distance :: operator= (const Distance& right_operand)
                {
                nu = right_operand.n u;
                me = right_operand.m e;
                return *this;
                }

                But as I said, you don't need to do this, just remove all traces of
                operator= from your code and you'll find that you can still assign one
                Distance value to another perfectly well.

                john


                Comment

                • John Harrison

                  #9
                  Re: Class problem


                  "Chiller" <...@...> wrote in message
                  news:835125bcd5 a9215b8edd83fae 621e778@news.te ranews.com...[color=blue]
                  > 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.[/color]

                  There's no need to do that. You only need to do write an operator= if the
                  default one isn't correct. In your case the default assignment operator will
                  work fine.

                  For the record however here's how you should define operator=

                  Distance& Distance :: operator= (const Distance& right_operand)
                  {
                  nu = right_operand.n u;
                  me = right_operand.m e;
                  return *this;
                  }

                  But as I said, you don't need to do this, just remove all traces of
                  operator= from your code and you'll find that you can still assign one
                  Distance value to another perfectly well.

                  john


                  Comment

                  • Daniel T.

                    #10
                    Re: Class problem

                    "Chiller" <...@...> wrote:
                    [color=blue]
                    > 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[/color]

                    I don't think the above is such a good idea. It requires a global enum {
                    m, km }; which would cause problems with any use of those 1 and 2
                    character identifiers elsewhere in a program. Another important point, I
                    think your class should be able to handle decemal point input (5.4, 8.7
                    and such.)

                    Better would be to put the enum inside the Distance class, so the input
                    would be:

                    Distance a(5.3, Distance::m);
                    Distance b(8.7, Distance::km);

                    I'd hate to say it, but I think you should throw out what you have so
                    far and start over.

                    Start with this main as a test case:

                    ----------------------------------------------------------------------
                    #include "Distance.h "
                    #include <string>
                    #include <sstream>
                    #include <iostream>

                    template < typename T >
                    void check( const T& expected, const T& got )
                    {
                    if ( expected != got ) {
                    std::cerr << "Expected: '" << expected
                    << "' got: '" << got << "'" << std::endl;
                    abort();
                    }
                    }

                    int main()
                    {
                    using namespace std;
                    Distance a(5.3, Distance::m);
                    Distance b(8.7, Distance::km);
                    ostringstream oss;
                    oss << a;
                    check( string( "5.3m" ), oss.str() );
                    oss.str( "" );
                    oss << b;
                    check( string( "8.7km" ), oss.str() );

                    cout << "OK";
                    }
                    ----------------------------------------------------------------------

                    Just plug this into your compiler and try to compile it. Fix the first
                    error and try again, keep doing this until you get no compile time
                    errors. Then the code will run, but you will see the "Expected: got:"
                    error show up on the screen. Keep fixing it until the only thing that
                    outputs is "OK".

                    Once you get it to that point, post what you have and request help, and
                    I'll provide you with another test.

                    (For right now, don't worry about how the test above works, just accept
                    that it does, you can study it later when you know more about C++.)

                    Comment

                    • Daniel T.

                      #11
                      Re: Class problem

                      "Chiller" <...@...> wrote:
                      [color=blue]
                      > 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[/color]

                      I don't think the above is such a good idea. It requires a global enum {
                      m, km }; which would cause problems with any use of those 1 and 2
                      character identifiers elsewhere in a program. Another important point, I
                      think your class should be able to handle decemal point input (5.4, 8.7
                      and such.)

                      Better would be to put the enum inside the Distance class, so the input
                      would be:

                      Distance a(5.3, Distance::m);
                      Distance b(8.7, Distance::km);

                      I'd hate to say it, but I think you should throw out what you have so
                      far and start over.

                      Start with this main as a test case:

                      ----------------------------------------------------------------------
                      #include "Distance.h "
                      #include <string>
                      #include <sstream>
                      #include <iostream>

                      template < typename T >
                      void check( const T& expected, const T& got )
                      {
                      if ( expected != got ) {
                      std::cerr << "Expected: '" << expected
                      << "' got: '" << got << "'" << std::endl;
                      abort();
                      }
                      }

                      int main()
                      {
                      using namespace std;
                      Distance a(5.3, Distance::m);
                      Distance b(8.7, Distance::km);
                      ostringstream oss;
                      oss << a;
                      check( string( "5.3m" ), oss.str() );
                      oss.str( "" );
                      oss << b;
                      check( string( "8.7km" ), oss.str() );

                      cout << "OK";
                      }
                      ----------------------------------------------------------------------

                      Just plug this into your compiler and try to compile it. Fix the first
                      error and try again, keep doing this until you get no compile time
                      errors. Then the code will run, but you will see the "Expected: got:"
                      error show up on the screen. Keep fixing it until the only thing that
                      outputs is "OK".

                      Once you get it to that point, post what you have and request help, and
                      I'll provide you with another test.

                      (For right now, don't worry about how the test above works, just accept
                      that it does, you can study it later when you know more about C++.)

                      Comment

                      Working...