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 | | | | 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. | | | | 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? | | | | re: Adding Objects
"Ross Olney" <rmolney@adelphia.net> wrote in message
news:JaOdnQH9jNuKegvcRVn-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? | | | | 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@optusnet.com.au> wrote in message
news:4196f271$0$27451$afc38c87@news.optusnet.com.a u...[color=blue]
> "Ross Olney" <rmolney@adelphia.net> wrote in message
> news:JaOdnQH9jNuKegvcRVn-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] | | | | 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@mariani.ws> wrote in message
news:8cGdnUJwvejJcAvcRVn-oQ@speakeasy.net...[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] | | | | re: Adding Objects
"Rv5" <rmolney@adelphia.net> wrote in message
news:2LWdnePco8VLagvcRVn-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, PolynomialOperations say. This new class can do the
work of adding and multiplying polynomials for you.
class PolynomialOperations
{
public:
Polynomial add(Polynomial x, Polynomial y) const { /* ... */ }
Polynomial multiply(Polynomial x, Polynomial y) const { /* ... */ }
};
Then for convenience you can define global functions that overload + and *
as follows:
Polynomial operator+(Polynomial x, Polynomial y)
{ return PolynomialOperations().add(x, y); }
Polynomial operator*(Polynomial x, Polynomial y)
{ return PolynomialOperations().multiply(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. | | | | re: Adding Objects
Jason, thats great, thanks. Ill try that. Didnt think of it quite like
that.
Thanks again
"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
news:4196fc31$0$25321$afc38c87@news.optusnet.com.a u...[color=blue]
> "Rv5" <rmolney@adelphia.net> wrote in message
> news:2LWdnePco8VLagvcRVn-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, PolynomialOperations say. This new class can
> do the work of adding and multiplying polynomials for you.
>
> class PolynomialOperations
> {
> public:
> Polynomial add(Polynomial x, Polynomial y) const { /* ... */ }
> Polynomial multiply(Polynomial x, Polynomial y) const { /* ... */ }
> };
>
> Then for convenience you can define global functions that overload + and *
> as follows:
>
> Polynomial operator+(Polynomial x, Polynomial y)
> { return PolynomialOperations().add(x, y); }
>
> Polynomial operator*(Polynomial x, Polynomial y)
> { return PolynomialOperations().multiply(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] | | | | re: Adding Objects
"Rv5" <rmolney@adelphia.net> wrote in message
news:efqdnR2S-PQkagvcRVn-pw@adelphia.com...[color=blue]
> "Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
> news:4196f271$0$27451$afc38c87@news.optusnet.com.a u...[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. | | | | re: Adding Objects
"Ross Olney" <rmolney@adelphia.net> skrev i en meddelelse
news:JaOdnQH9jNuKegvcRVn-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+(polynomial lhs,polynomial const& rhs)
// same with operator -,*,/
{
return lhs += rhs;
}
/Peter |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|