Adding Objects

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

    #1

    Adding Objects

    Im starting a program that is supposed to take in polynomials from the user.
    From there they can add them, subract, multiply, or divide them.

    My question is more of the design. What would be a good way to design it
    be? I have a polynomial class and the only way Ive figured out to do this
    is put in add, subtract etc functions into the polynomial class and pass one
    polynomial to the other. But that doesnt seem very good. Seems inefficient
    somehow.

    I hope I explained that enough for people to understand. I did some C back
    in college about 5 years ago but have spent my professinal career doing ASP
    and net languages which drive me crazy. Trying to get back into something
    more real...

    Thanks


  • Gianni Mariani

    #2
    Re: Adding Objects

    Ross Olney wrote:[color=blue]
    > Im starting a program that is supposed to take in polynomials from the user.
    > From there they can add them, subract, multiply, or divide them.
    >
    > My question is more of the design. What would be a good way to design it
    > be? I have a polynomial class and the only way Ive figured out to do this
    > is put in add, subtract etc functions into the polynomial class and pass one
    > polynomial to the other. But that doesnt seem very good. Seems inefficient
    > somehow.[/color]

    Why do you think it is inefficient ?

    Or, more importantly, why are you concerned about efficiency ?
    [color=blue]
    >
    > I hope I explained that enough for people to understand. I did some C back
    > in college about 5 years ago but have spent my professinal career doing ASP
    > and net languages which drive me crazy. Trying to get back into something
    > more real...[/color]

    You could use a non-member operator function - e.g.

    template <typename T=double>
    class Poly;

    template <typename T=double>
    Poly<T> operator+( const Poly<T> &, const Poly<T> & );

    template <typename T=double>
    Poly<T> operator-( const Poly<T> &, const Poly<T> & );

    template <typename T=double>
    Poly<T> operator*( const Poly<T> &, const Poly<T> & );

    template <typename T=double>
    Poly<T> operator/( const Poly<T> &, const Poly<T> & );

    template <typename T=double>
    Poly<T> operator*( const Poly<T> &, const T & );

    template <typename T=double>
    Poly<T> operator*( const T &, const Poly<T> & );

    This way, you could write code like:

    Poly< complex<double> > a, b;

    ....
    b = 3 * a;

    BTW - dividing polynomials to produce a polynomial will be an
    interesting trick.

    Comment

    • Alf P. Steinbach

      #3
      Re: Adding Objects

      * Ross Olney:[color=blue]
      > Im starting a program that is supposed to take in polynomials from the user.
      > From there they can add them, subract, multiply, or divide them.
      >
      > My question is more of the design. What would be a good way to design it
      > be? I have a polynomial class and the only way Ive figured out to do this
      > is put in add, subtract etc functions into the polynomial class and pass one
      > polynomial to the other. But that doesnt seem very good. Seems inefficient
      > somehow.[/color]

      For efficiency you might consider implementing Polynomial as a
      smart-pointer like class, each object containing a refcounted pointer to


      But first check whether you really need that, by measuring.

      The code below exemplifies how to do the add, subtract etc. regardless
      of whether the internal representation is direct or smartpointer-like:


      #include <iostream>

      class Integer
      {
      private:
      int myValue;
      public:
      Integer( int aValue = 0 ): myValue( aValue ) {}

      int const value() const { return myValue; }

      Integer& operator+=( Integer const& rhs )
      {
      myValue += rhs.value();
      return *this;
      }
      };

      inline Integer operator+( Integer const& a, Integer const& b )
      {
      Integer result = a;
      return result += b;
      }

      int main()
      {
      Integer a = 3;
      Integer b = 5;
      std::cout << (a+b).value() << std::endl;
      }

      --
      A: Because it messes up the order in which people normally read text.
      Q: Why is it such a bad thing?
      A: Top-posting.
      Q: What is the most annoying thing on usenet and in e-mail?

      Comment

      • Jason Heyes

        #4
        Re: Adding Objects

        "Ross Olney" <rmolney@adelph ia.net> wrote in message
        news:JaOdnQH9jN uKegvcRVn-rw@adelphia.com ...[color=blue]
        > Im starting a program that is supposed to take in polynomials from the
        > user. From there they can add them, subract, multiply, or divide them.
        >
        > My question is more of the design. What would be a good way to design it
        > be? I have a polynomial class and the only way Ive figured out to do this
        > is put in add, subtract etc functions into the polynomial class and pass
        > one polynomial to the other. But that doesnt seem very good. Seems
        > inefficient somehow.
        >
        > I hope I explained that enough for people to understand. I did some C
        > back in college about 5 years ago but have spent my professinal career
        > doing ASP and net languages which drive me crazy. Trying to get back into
        > something more real...
        >
        > Thanks[/color]

        Can your polynomials be really big?


        Comment

        • Rv5

          #5
          Re: Adding Objects

          Yeah sure. Ideally Id like to get it so they can enter in as many terms as
          they want. For starters I might stick with just a standard 3 term poly
          though.
          "Jason Heyes" <jasonheyes@opt usnet.com.au> wrote in message
          news:4196f271$0 $27451$afc38c87 @news.optusnet. com.au...[color=blue]
          > "Ross Olney" <rmolney@adelph ia.net> wrote in message
          > news:JaOdnQH9jN uKegvcRVn-rw@adelphia.com ...[color=green]
          >> Im starting a program that is supposed to take in polynomials from the
          >> user. From there they can add them, subract, multiply, or divide them.
          >>
          >> My question is more of the design. What would be a good way to design it
          >> be? I have a polynomial class and the only way Ive figured out to do
          >> this is put in add, subtract etc functions into the polynomial class and
          >> pass one polynomial to the other. But that doesnt seem very good. Seems
          >> inefficient somehow.
          >>
          >> I hope I explained that enough for people to understand. I did some C
          >> back in college about 5 years ago but have spent my professinal career
          >> doing ASP and net languages which drive me crazy. Trying to get back
          >> into something more real...
          >>
          >> Thanks[/color]
          >
          > Can your polynomials be really big?
          >[/color]


          Comment

          • Rv5

            #6
            Re: Adding Objects

            somehow it just seemed like one object being passed to another object of the
            same type wasn't quite right. I guess something about having the passed
            object contain all these same functions that it wont ever use is wasteful
            somehow. From a performance perspective I dont care at all. I just want to
            be sure Im designing it right and not getting into bad habits

            "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
            news:8cGdnUJwve jJcAvcRVn-oQ@speakeasy.ne t...[color=blue]
            > Ross Olney wrote:[color=green]
            >> Im starting a program that is supposed to take in polynomials from the
            >> user. From there they can add them, subract, multiply, or divide them.
            >>
            >> My question is more of the design. What would be a good way to design it
            >> be? I have a polynomial class and the only way Ive figured out to do
            >> this is put in add, subtract etc functions into the polynomial class and
            >> pass one polynomial to the other. But that doesnt seem very good. Seems
            >> inefficient somehow.[/color]
            >
            > Why do you think it is inefficient ?
            >
            > Or, more importantly, why are you concerned about efficiency ?
            >[color=green]
            >>
            >> I hope I explained that enough for people to understand. I did some C
            >> back in college about 5 years ago but have spent my professinal career
            >> doing ASP and net languages which drive me crazy. Trying to get back
            >> into something more real...[/color]
            >
            > You could use a non-member operator function - e.g.
            >
            > template <typename T=double>
            > class Poly;
            >
            > template <typename T=double>
            > Poly<T> operator+( const Poly<T> &, const Poly<T> & );
            >
            > template <typename T=double>
            > Poly<T> operator-( const Poly<T> &, const Poly<T> & );
            >
            > template <typename T=double>
            > Poly<T> operator*( const Poly<T> &, const Poly<T> & );
            >
            > template <typename T=double>
            > Poly<T> operator/( const Poly<T> &, const Poly<T> & );
            >
            > template <typename T=double>
            > Poly<T> operator*( const Poly<T> &, const T & );
            >
            > template <typename T=double>
            > Poly<T> operator*( const T &, const Poly<T> & );
            >
            > This way, you could write code like:
            >
            > Poly< complex<double> > a, b;
            >
            > ....
            > b = 3 * a;
            >
            > BTW - dividing polynomials to produce a polynomial will be an interesting
            > trick.
            >[/color]


            Comment

            • Jason Heyes

              #7
              Re: Adding Objects

              "Rv5" <rmolney@adelph ia.net> wrote in message
              news:2LWdnePco8 VLagvcRVn-uQ@adelphia.com ...[color=blue]
              > somehow it just seemed like one object being passed to another object of
              > the
              > same type wasn't quite right. I guess something about having the passed
              > object contain all these same functions that it wont ever use is wasteful
              > somehow. From a performance perspective I dont care at all. I just want
              > to
              > be sure Im designing it right and not getting into bad habits[/color]

              If it worries you that a polynomial object doesn't use one of its member
              functions then take that function out of the Polynomial class altogether and
              put it in another class, PolynomialOpera tions say. This new class can do the
              work of adding and multiplying polynomials for you.

              class PolynomialOpera tions
              {
              public:
              Polynomial add(Polynomial x, Polynomial y) const { /* ... */ }
              Polynomial multiply(Polyno mial x, Polynomial y) const { /* ... */ }
              };

              Then for convenience you can define global functions that overload + and *
              as follows:

              Polynomial operator+(Polyn omial x, Polynomial y)
              { return PolynomialOpera tions().add(x, y); }

              Polynomial operator*(Polyn omial x, Polynomial y)
              { return PolynomialOpera tions().multipl y(x, y); }

              You might find this design more attactive than one that defines functions
              for addition and multiplication as members of the Polynomial class itself.


              Comment

              • Rv5

                #8
                Re: Adding Objects

                Jason, thats great, thanks. Ill try that. Didnt think of it quite like
                that.

                Thanks again
                "Jason Heyes" <jasonheyes@opt usnet.com.au> wrote in message
                news:4196fc31$0 $25321$afc38c87 @news.optusnet. com.au...[color=blue]
                > "Rv5" <rmolney@adelph ia.net> wrote in message
                > news:2LWdnePco8 VLagvcRVn-uQ@adelphia.com ...[color=green]
                >> somehow it just seemed like one object being passed to another object of
                >> the
                >> same type wasn't quite right. I guess something about having the passed
                >> object contain all these same functions that it wont ever use is wasteful
                >> somehow. From a performance perspective I dont care at all. I just want
                >> to
                >> be sure Im designing it right and not getting into bad habits[/color]
                >
                > If it worries you that a polynomial object doesn't use one of its member
                > functions then take that function out of the Polynomial class altogether
                > and put it in another class, PolynomialOpera tions say. This new class can
                > do the work of adding and multiplying polynomials for you.
                >
                > class PolynomialOpera tions
                > {
                > public:
                > Polynomial add(Polynomial x, Polynomial y) const { /* ... */ }
                > Polynomial multiply(Polyno mial x, Polynomial y) const { /* ... */ }
                > };
                >
                > Then for convenience you can define global functions that overload + and *
                > as follows:
                >
                > Polynomial operator+(Polyn omial x, Polynomial y)
                > { return PolynomialOpera tions().add(x, y); }
                >
                > Polynomial operator*(Polyn omial x, Polynomial y)
                > { return PolynomialOpera tions().multipl y(x, y); }
                >
                > You might find this design more attactive than one that defines functions
                > for addition and multiplication as members of the Polynomial class itself.
                >[/color]


                Comment

                • Jason Heyes

                  #9
                  Re: Adding Objects

                  "Rv5" <rmolney@adelph ia.net> wrote in message
                  news:efqdnR2S-PQkagvcRVn-pw@adelphia.com ...[color=blue]
                  > "Jason Heyes" <jasonheyes@opt usnet.com.au> wrote in message
                  > news:4196f271$0 $27451$afc38c87 @news.optusnet. com.au...[color=green]
                  >> Can your polynomials be really big?
                  >>[/color]
                  > Yeah sure. Ideally Id like to get it so they can enter in as many terms
                  > as they want. For starters I might stick with just a standard 3 term poly
                  > though.[/color]

                  It sounds like a good programming exercise. Support polynomials of any
                  degree from the start. You won't find life any easier limiting the size of
                  your polynomials. Use a resizable array such as std::vector to hold your
                  coefficients. You'd be surprised how easy it is to use.


                  Comment

                  • Peter Koch Larsen

                    #10
                    Re: Adding Objects


                    "Ross Olney" <rmolney@adelph ia.net> skrev i en meddelelse
                    news:JaOdnQH9jN uKegvcRVn-rw@adelphia.com ...[color=blue]
                    > Im starting a program that is supposed to take in polynomials from the
                    > user. From there they can add them, subract, multiply, or divide them.
                    >
                    > My question is more of the design. What would be a good way to design it
                    > be? I have a polynomial class and the only way Ive figured out to do this
                    > is put in add, subtract etc functions into the polynomial class and pass
                    > one polynomial to the other. But that doesnt seem very good. Seems
                    > inefficient somehow.
                    >
                    > I hope I explained that enough for people to understand. I did some C
                    > back in college about 5 years ago but have spent my professinal career
                    > doing ASP and net languages which drive me crazy. Trying to get back into
                    > something more real...
                    >
                    > Thanks
                    >[/color]

                    Do it like this:

                    class polynomial
                    {
                    public:
                    polynomial& operator +=(const polynomial &rhs);
                    // same with *= -= /= and =
                    };

                    polynomial operator+(polyn omial lhs,polynomial const& rhs)
                    // same with operator -,*,/
                    {
                    return lhs += rhs;
                    }


                    /Peter


                    Comment

                    Working...