Adding Objects

R

Ross Olney

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
 
A

Alf P. Steinbach

* Ross Olney:
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.

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;
}
 
G

Gianni Mariani

Ross said:
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.

Why do you think it is inefficient ?

Or, more importantly, why are you concerned about efficiency ?
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...

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.
 
J

Jason Heyes

Ross Olney said:
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

Can your polynomials be really big?
 
R

Rv5

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.
 
R

Rv5

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
 
J

Jason Heyes

Rv5 said:
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

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.
 
R

Rv5

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

Thanks again
 
J

Jason Heyes

Rv5 said:
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.

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.
 
P

Peter Koch Larsen

Ross Olney said:
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top