473,322 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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
Jul 22 '05 #1
9 1565
Ross Olney wrote:
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.

Jul 22 '05 #2
* 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;
}

--
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?
Jul 22 '05 #3
"Ross Olney" <rm*****@adelphia.net> wrote in message
news:Ja********************@adelphia.com...
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?
Jul 22 '05 #4
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.
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41***********************@news.optusnet.com.a u...
"Ross Olney" <rm*****@adelphia.net> wrote in message
news:Ja********************@adelphia.com...
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?

Jul 22 '05 #5
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

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:8c********************@speakeasy.net...
Ross Olney wrote:
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.

Jul 22 '05 #6
"Rv5" <rm*****@adelphia.net> wrote in message
news:2L********************@adelphia.com...
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.
Jul 22 '05 #7
Rv5
Jason, thats great, thanks. Ill try that. Didnt think of it quite like
that.

Thanks again
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41***********************@news.optusnet.com.a u...
"Rv5" <rm*****@adelphia.net> wrote in message
news:2L********************@adelphia.com...
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.

Jul 22 '05 #8
"Rv5" <rm*****@adelphia.net> wrote in message
news:ef********************@adelphia.com...
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41***********************@news.optusnet.com.a u...
Can your polynomials be really big?

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.
Jul 22 '05 #9

"Ross Olney" <rm*****@adelphia.net> skrev i en meddelelse
news:Ja********************@adelphia.com...
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
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Geoff Biggs | last post by:
Evening all, I'm trying to add a new built-in number data type to Python with its own syntax, so I'm working directly with the interpreter rather than creating my own extension module (side...
5
by: cppaddict | last post by:
Hi, I have some code which uses std::map to associate cartesian POINTs with values. My current syntax for adding new (POINT,value) entries to the map is clunky -- it takes three lines for each...
11
by: Steven D'Aprano | last post by:
Suppose I create a class with some methods: py> class C: .... def spam(self, x): .... print "spam " * x .... def ham(self, x): .... print "ham * %s" % x .......
5
by: Troy | last post by:
Hello, I have a dumb question. I have two array objects of type double with 12 elements in each object. I'd like to add the two objects but dont know how to. Any ideas? thanks
0
by: MIGUEL | last post by:
Hi all! Be patient because what I'm going to explain all of you it's more than very strange. I've developed a webservice project that contains two classes. One of them is going to act as a...
47
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols...
47
by: Albert | last post by:
So structures are useful to group variables, so you can to refer to a collection as a single entity. Wouldn't it be useful to also have the ability to collect variable and functions? Ask K&R...
2
by: silversurfer | last post by:
Hello, I am a little unsure whether this method really makes sense. The goal is to add an element to a vector. This is the struct and method I am using: std::vector<Entry> models; struct...
1
by: =?Utf-8?B?TGVvbiBNYXluZQ==?= | last post by:
In a database project in VS2005 (not a datadude project), I need to add about 3,000 stored procedure scripts to my 'Stored Procedures' folder. I've dropped the scripts into the folder in my...
7
by: Maximus Decimus | last post by:
HI all, I am using python v2.5 and I am an amateur working on python. I am extending python for my research work and would like some help and guidance w.r.t this matter from you experienced...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.