Constructor problem

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

    #16
    Re: Constructor problem

    Thanks Mark,

    I'm trying to write a class (Distance.h file and a Distance.cpp) file that
    will accept two arguments, one being a value and the other being a unit (ie
    km, m, or cm). I'm trying to write the class to include overloads for ==,
    !=, <, <=, > and >= which will return a boolean value. As well as overloads
    of + and -. Obviously the overloads will only work on multimple instances,
    ie when I enter more than a single value.

    What I've written so far is included within this thread.

    Thanks


    Comment

    • Chiller

      #17
      Re: Constructor problem

      Thanks Mark,

      I'm trying to write a class (Distance.h file and a Distance.cpp) file that
      will accept two arguments, one being a value and the other being a unit (ie
      km, m, or cm). I'm trying to write the class to include overloads for ==,
      !=, <, <=, > and >= which will return a boolean value. As well as overloads
      of + and -. Obviously the overloads will only work on multimple instances,
      ie when I enter more than a single value.

      What I've written so far is included within this thread.

      Thanks


      Comment

      • Chiller

        #18
        Re: Constructor problem

        Thanks John,

        I obviously need to take a break for a while.

        Regards


        Comment

        • Chiller

          #19
          Re: Constructor problem

          Thanks John,

          I obviously need to take a break for a while.

          Regards


          Comment

          • Chiller

            #20
            Re: Constructor problem

            John,

            I've taken your suggestion and included enum; however, when I output the
            stored values to screen, all the values are int values. What I'm trying to
            do is to be able to enter a value and unit and be able to output the same
            value and unit. ie, if I input Distance a (5, m) I'd like to be able to
            output the same. Once I've got this working my next step will be to
            implement some overloaded operators to perform addition, subtraction etc on
            any objects created by the class.

            I've included what I've done so far below. If you compile and execute the
            program you'll see what I mean by the output being in the wrong format.I'd
            appreciate any advice on this.

            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



            Comment

            • Chiller

              #21
              Re: Constructor problem

              John,

              I've taken your suggestion and included enum; however, when I output the
              stored values to screen, all the values are int values. What I'm trying to
              do is to be able to enter a value and unit and be able to output the same
              value and unit. ie, if I input Distance a (5, m) I'd like to be able to
              output the same. Once I've got this working my next step will be to
              implement some overloaded operators to perform addition, subtraction etc on
              any objects created by the class.

              I've included what I've done so far below. If you compile and execute the
              program you'll see what I mean by the output being in the wrong format.I'd
              appreciate any advice on this.

              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



              Comment

              • SaltPeter

                #22
                Re: Constructor problem


                "Chiller" <...@...> wrote in message
                news:59b0c5bce8 57990952dc322b6 a1458b2@news.te ranews.com...[color=blue]
                > Is there a way that I can write the constructor so that it will simply
                > accept two variables, ie. when I create an object I can do so by simply
                > typing
                >
                > Distance a = Distance (5, m); or
                > Distance a = Distance (5, km); etc
                >
                > without having to use ' ' or declaring m, km?
                >
                > Thanks
                >[/color]
                You need to refine your knowledge of constants.
                The arguements provided are not variables. 5 is a constant ( a literal
                value). The second arguement can be a std::string or enum but its also a
                constant here. You don't have to use the second arguement if you plan on
                providing a consistant measurement base (like meters). Personally, i'ld
                prefer a constructor with an init list and passing constants by reference:

                #include <iostream>
                #include <string>

                class Distance
                {
                public:
                Distance(const int& r_distance) : meters(r_distan ce) { }
                virtual ~Distance() { }
                void display() const { std::cout << "distance = " << meters <<
                std::endl; }
                private:
                int meters;
                };

                int main()
                {
                Distance dist(5000);
                dist.display();
                return 0;
                }



                Comment

                • SaltPeter

                  #23
                  Re: Constructor problem


                  "Chiller" <...@...> wrote in message
                  news:59b0c5bce8 57990952dc322b6 a1458b2@news.te ranews.com...[color=blue]
                  > Is there a way that I can write the constructor so that it will simply
                  > accept two variables, ie. when I create an object I can do so by simply
                  > typing
                  >
                  > Distance a = Distance (5, m); or
                  > Distance a = Distance (5, km); etc
                  >
                  > without having to use ' ' or declaring m, km?
                  >
                  > Thanks
                  >[/color]
                  You need to refine your knowledge of constants.
                  The arguements provided are not variables. 5 is a constant ( a literal
                  value). The second arguement can be a std::string or enum but its also a
                  constant here. You don't have to use the second arguement if you plan on
                  providing a consistant measurement base (like meters). Personally, i'ld
                  prefer a constructor with an init list and passing constants by reference:

                  #include <iostream>
                  #include <string>

                  class Distance
                  {
                  public:
                  Distance(const int& r_distance) : meters(r_distan ce) { }
                  virtual ~Distance() { }
                  void display() const { std::cout << "distance = " << meters <<
                  std::endl; }
                  private:
                  int meters;
                  };

                  int main()
                  {
                  Distance dist(5000);
                  dist.display();
                  return 0;
                  }



                  Comment

                  • John Harrison

                    #24
                    Re: Constructor problem


                    "Chiller" <...@...> wrote in message
                    news:63fb2e2141 4512839f2c71f99 5182bf0@news.te ranews.com...[color=blue]
                    > John,
                    >
                    > I've taken your suggestion and included enum; however, when I output the
                    > stored values to screen, all the values are int values.[/color]

                    That's because enum's are integer constants.
                    [color=blue]
                    > What I'm trying to
                    > do is to be able to enter a value and unit and be able to output the same
                    > value and unit. ie, if I input Distance a (5, m) I'd like to be able to
                    > output the same. Once I've got this working my next step will be to
                    > implement some overloaded operators to perform addition, subtraction etc[/color]
                    on[color=blue]
                    > any objects created by the class.[/color]

                    There's no way to do that other than the hard way

                    enum { m, km, };

                    switch (d.measure())
                    {
                    case m:
                    out << "m";
                    break;
                    case km:
                    out << "km";
                    break;
                    }

                    There's various ways of tarting this up, for instance you could have an
                    array of unit strings.

                    enum { m, km, };

                    const char* const unit_str[] = { "m", "km" };

                    out << unit_str[d.measure()];

                    but essentially you have to test the int value of your units and print out
                    the appropriate string. There is no way of automatically converting the name
                    of something in your program to a string.

                    john


                    Comment

                    • John Harrison

                      #25
                      Re: Constructor problem


                      "Chiller" <...@...> wrote in message
                      news:63fb2e2141 4512839f2c71f99 5182bf0@news.te ranews.com...[color=blue]
                      > John,
                      >
                      > I've taken your suggestion and included enum; however, when I output the
                      > stored values to screen, all the values are int values.[/color]

                      That's because enum's are integer constants.
                      [color=blue]
                      > What I'm trying to
                      > do is to be able to enter a value and unit and be able to output the same
                      > value and unit. ie, if I input Distance a (5, m) I'd like to be able to
                      > output the same. Once I've got this working my next step will be to
                      > implement some overloaded operators to perform addition, subtraction etc[/color]
                      on[color=blue]
                      > any objects created by the class.[/color]

                      There's no way to do that other than the hard way

                      enum { m, km, };

                      switch (d.measure())
                      {
                      case m:
                      out << "m";
                      break;
                      case km:
                      out << "km";
                      break;
                      }

                      There's various ways of tarting this up, for instance you could have an
                      array of unit strings.

                      enum { m, km, };

                      const char* const unit_str[] = { "m", "km" };

                      out << unit_str[d.measure()];

                      but essentially you have to test the int value of your units and print out
                      the appropriate string. There is no way of automatically converting the name
                      of something in your program to a string.

                      john


                      Comment

                      • DaKoadMunky

                        #26
                        Re: Constructor problem

                        >enum[color=blue]
                        >{
                        > m,
                        > km,
                        > miles,
                        >};
                        >
                        >etc.
                        >
                        >Put that at the top of you program and then change char to int. I.e. you
                        >constructor becomes
                        >
                        >Distance (int, int);
                        >[/color]

                        I am curious about your suggestion to change the second parameter from a char
                        to an int.

                        Wouldn't it be better to have the enumeration typed and then use that type as
                        the second parameter? Then the author of the class need not worry that he has
                        been passed an invalid indicator of unit as the compiler will allow only values
                        specified in the enumeration to be passed. Of course the caller could cast any
                        value to the enumerated type, but then they get what they deserve.

                        Regards.


                        Brian F. Seaberg
                        Naperville, Illinois
                        Delray Beach, Florida

                        Comment

                        • DaKoadMunky

                          #27
                          Re: Constructor problem

                          >enum[color=blue]
                          >{
                          > m,
                          > km,
                          > miles,
                          >};
                          >
                          >etc.
                          >
                          >Put that at the top of you program and then change char to int. I.e. you
                          >constructor becomes
                          >
                          >Distance (int, int);
                          >[/color]

                          I am curious about your suggestion to change the second parameter from a char
                          to an int.

                          Wouldn't it be better to have the enumeration typed and then use that type as
                          the second parameter? Then the author of the class need not worry that he has
                          been passed an invalid indicator of unit as the compiler will allow only values
                          specified in the enumeration to be passed. Of course the caller could cast any
                          value to the enumerated type, but then they get what they deserve.

                          Regards.


                          Brian F. Seaberg
                          Naperville, Illinois
                          Delray Beach, Florida

                          Comment

                          • John Harrison

                            #28
                            Re: Constructor problem

                            > >[color=blue]
                            >
                            > I am curious about your suggestion to change the second parameter from a[/color]
                            char[color=blue]
                            > to an int.
                            >
                            > Wouldn't it be better to have the enumeration typed and then use that type[/color]
                            as[color=blue]
                            > the second parameter? Then the author of the class need not worry that he[/color]
                            has[color=blue]
                            > been passed an invalid indicator of unit as the compiler will allow only[/color]
                            values[color=blue]
                            > specified in the enumeration to be passed. Of course the caller could[/color]
                            cast any[color=blue]
                            > value to the enumerated type, but then they get what they deserve.
                            >[/color]

                            Maybe, but I was just trying to introduce one new concept at a time.

                            Incidentally the OP took my advice but if you read his latest post he's
                            changed back from an int to a char for some spurious reason.

                            john


                            Comment

                            • John Harrison

                              #29
                              Re: Constructor problem

                              > >[color=blue]
                              >
                              > I am curious about your suggestion to change the second parameter from a[/color]
                              char[color=blue]
                              > to an int.
                              >
                              > Wouldn't it be better to have the enumeration typed and then use that type[/color]
                              as[color=blue]
                              > the second parameter? Then the author of the class need not worry that he[/color]
                              has[color=blue]
                              > been passed an invalid indicator of unit as the compiler will allow only[/color]
                              values[color=blue]
                              > specified in the enumeration to be passed. Of course the caller could[/color]
                              cast any[color=blue]
                              > value to the enumerated type, but then they get what they deserve.
                              >[/color]

                              Maybe, but I was just trying to introduce one new concept at a time.

                              Incidentally the OP took my advice but if you read his latest post he's
                              changed back from an int to a char for some spurious reason.

                              john


                              Comment

                              Working...