![]() |
|
#1
|
|||
|
|||
in great need of creating polynomial classok... i have an assignment due in 14 hours and am having trouble creating and implementing a polynomial class. i have a few strict rules to follow. i will first start out by explaining what i expected and what i am and am not allowed to do.
my instructor has created the main program, some of the Poly.h, and exceptionClass.h. I am to finish poly.h, and implement Poly.cc on my own. i will only turn in Poly.h and Poly.cc. I am not allowed to make any changes to the main program or to the display function in Poly.h, and Poly.cc. The polynomial will only consist of one variable, eg.. only an x (not an x and y for instance). I must create the interface file Poly.h which MUST contain a basic constructor, a copy constructor, operator assignment, operator +, operator *,degree, and display. the basic constructor should contain 2 arguments, a pointer to and integer array, and an int which is the number of non zero terms. the integer array contains the non zero elements in decreasing order of degree. each term consists of the degree followed by the term. for example, 3x^5 + 2X^2 + x.. would be represented as [5, 3, 2, 2, 1, 1]. cinPoly () is provided in the main program to obtain these two pieces of info. the degree function will return the highest degree of the polynomial( which will always be the first element here). the private member of the Class must not be changed and none may be added. i may add public members and member functions as needed. the private member is a pointer to an integer and must stay that way. i am not understanding how my class actually will create the polynomial. My main misunderstanding is how to create and use the constructor to create a polynomial object, and how everything relates to the operator over loads. i am in the process of doing the operators (im not being lazy i promise). i would appreciate if anyone can walk me through this process. i have been working on Poly.h and Poly.ccand will show all codes below... Poly.h... CPP / C++ / C Code:
Poly.cc... CPP / C++ / C Code:
exceptionClass.h... CPP / C++ / C Code:
and main.cc... CPP / C++ / C Code:
there we go. ANYTHING anyone can do to help is greatly appreciated. Please help me out. thank you. |
|||
|
#2
|
||||
|
||||
Re: in great need of creating polynomial classHere is a rough list of things that I see need attention:
Matthew __________________
I was born not knowing and have only had a little time to change that here and there. -- Richard P. Feynman Boris Podolsky: James! How's the rat business? James Moreland: Well, actually it's mostly students I'm experimenting on now. Kurt Gödel: My God, the mazes must be enormous. |
|
#3
|
|||
|
|||
Re: in great need of creating polynomial classOk... the assignment has passed now and i figure i didnt do too well on it. But, i still want to learn and understand how to do it. i will start off by saying that i am confused how the constructor should create a Poly object with the 2 parameters...
(i) a pointer to an integer array (ii) the number of non zero terms now, i understand that when i create the object the pointer will point to the address of the first element in the array i am trying to create. The number of non zero terms can be multiplied by 2 to get the length of the array. my trouble i think, is getting the actual values into the array created. if i am not mistaken, if this is done i have created my Poly object correctly? can someone take me a step further? im am still confused by this although typing this reply has added some spark in my brain. |
|
#4
|
||||
|
||||
Re: in great need of creating polynomial classAs I mentioned above, look at the function cinPoly for guidance:
CPP / C++ / C Code:
How might you use a similar approach in your constructor? How about: CPP / C++ / C Code:
The copy constructor will do something similar, except that it will get the array ptr and size from another polynomial. If you pay close attention, you'll realize that this means that your Polynomial class is missing something, namely a length or size variable. So, you'll need to add that before you can properly implement the copy constructor. Give this a try. Matthew __________________
I was born not knowing and have only had a little time to change that here and there. -- Richard P. Feynman Boris Podolsky: James! How's the rat business? James Moreland: Well, actually it's mostly students I'm experimenting on now. Kurt Gödel: My God, the mazes must be enormous. |
|
#5
|
|||
|
|||
Re: in great need of creating polynomial classnow i understand that cinPoly reads in the values for the polynomial. i understand why you multiply nzero by two, but i am confused by the dynamic allocation.
for this piece of code... CPP / C++ / C Code:
i see that it will dynamically allocate memory for the (size of int) * len. basically it will allocate memory for my array. Will pol still point to the first element in the array? and as for the for loop i see that the elements of parameter (intptr) passed in are copied to pol for each element in the array. Thus creating a polynomial object? So can anybody clear up the line of code above for me? Does pol point to the first element of the array? It seems it must for the code above to work the way i am thinking. Thank you all. |
|
#6
|
||||
|
||||
Re: in great need of creating polynomial classQuote:
Quote:
Matthew __________________
I was born not knowing and have only had a little time to change that here and there. -- Richard P. Feynman Boris Podolsky: James! How's the rat business? James Moreland: Well, actually it's mostly students I'm experimenting on now. Kurt Gödel: My God, the mazes must be enormous. |
|
#7
|
|||
|
|||
Re: in great need of creating polynomial classThank you for the assistance on the constructor. i understand it much better now. Now i am working on the copy constructor. i have made changes to it. i feel i am much closer.
the code below represents my copy constructor... CPP / C++ / C Code:
here is what i am trying to do. the polynomial is passed in by alias (rhs). initially i allocate memory for my "copied" polynomial. here is where i am unsure... i use a public member function to access and return the address of the first element in rhs. Is this possible to use a member function within another member function? the result is stored in npol. which is a pointer to an integer, just as pol. Then i do a similar loop as used before to copy the element from the passed in polynomial to the copy of the polynomial. Is my reasoning correct? If i cannot use member functions withen member functions, how can i access the private members of a class? thank you. |
|
#8
|
|||
|
|||
Re: in great need of creating polynomial classi cant really test the constructor and copy constructor in my "assignment" because every thing has to work with the instructors main program. and i have still got to get my operator functions to work. so either way it will not compile. I think my copy constructor is correct. Can i get a second opinion please? Also can some body answer my question about using the member functions withen the class for other functions and constructors? thank you.
|
|
#9
|
||||
|
||||
Re: in great need of creating polynomial classIf you encounter compile-time errors, please post the error messages verbatim. Chances are that if you don't understand the message enough to find the mistake, then you are unlikely to explain it very well in your own words.
Looking at the code I can see one problem in the copy constructor. You are right that you are "much closer". Very close, in fact. The problem is len. Inside the copy constructor, what does len mean? The compiler doesn't know. There is not parameter named len, you have not declared a local variable named len, and probably there is neither a global nor member variable named len. In other words, similar to the member variable pol in your Polynomial class, you need an rhs Polynomial to the new one being constructed. Hope that makes sense. Matthew __________________
I was born not knowing and have only had a little time to change that here and there. -- Richard P. Feynman Boris Podolsky: James! How's the rat business? James Moreland: Well, actually it's mostly students I'm experimenting on now. Kurt Gödel: My God, the mazes must be enormous. |
|
#10
|
|||
|
|||
Re: in great need of creating polynomial classQuote:
im not really sure what your saying here, but i am having trouble figuring out how to incorporate the length of the polynomial i am trying to copy into my new polynomial. I am trying to figure it out. Any suggestions here? That may be what you are trying to say but i am not understanding. |
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| determinant algorithms | Blake | C++ Forum | 11 | 13-Mar-2006 19:36 |
| polynomial program | jake_jeckel | C++ Forum | 0 | 29-Oct-2005 15:16 |
| Error C2146: syntax error : missing ',' before identifier 'C4' | mattchew008 | C++ Forum | 2 | 19-Dec-2004 07:06 |
| problem with creating class | mohammed | C++ Forum | 1 | 11-Oct-2003 10:04 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The