I'm working on a project for my first use of classes and OOP. I've read all our slides (only provided examples though) and two tutorials on classes, but I confused about a few things. I'll show first the assignment then I'll post my specific questions after.

You will need to implement a class called Polynomial10 and use it in the main function of
your solution. This class represent a set of all polynomial of a degree not higher than 10. Note,
that such a set is closed with respect to addition, subtraction and scaling by constant. Private
members coef and degree store the array of polynomial coe!cients and the degree. Public
methods are responsible for interaction between the objects of this class and the program.
Member functions print() and read() provide console output and input. Member
functions add() and subtract() update a polynomial object by performing an appropriate
arithmetic operation with another object. Member function multc() scales the polynomial by
a constant. You are welcome to update the code you created for previous projects.
This class has the following prototype:

``````class Polynomial10
{
private:
double coef[MAX_DEGREE+1];
int degree;
public:
Polynomial10(); //Constructor for a new Polynomial10
int getDegree(){return degree;};
void print(); //Print the polynomial in standard form
void multc(double factor); //Multiply the poly by scalar
void subtract(const Polynomial10& pol); //Subtract polynom
};``````

1. I'm confused about the constructor. What would it's purpose be here? I read the tutorial http://www.cplusplus.com/doc/tutorial/classes/ and it designated the purpose of the constructor as to " initialize variables or assign dynamic memory". If I use the read() function to input the polynomial, then where does the constructor come into play?

2. "const Polynomial10& pol"
I am confused mainly about the &. I assume it is in association with as a pointer, but a little clarification would help a lot. I want to start writing but I'm limited to what I can do now when I don't know what some of the assigned functions mean. I appreciate any help.

5
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by gnarlyskim

1. The constructor is for exactly what you have stated "initialize variables or assign dynamic memory". In this case there is no dynamic memory so it is for initialisation of member variables. If you don't implement the constructor then between constructing the object and calling read() your object will contain garbage. You use the constructor to initialise the member variables to some zero state.

2. "const Polynomial10& pol" - that is a constant reference to a Polynomial10 object. References have some similarities with pointers (what you learnt pointers yet?). Some differences are

• A reference must be initialised so you can not have a NULL reference like you can have a NULL pointer. It also makes it quite a lot harder (but not impossible) to have an invalid reference where as producing an invalid pointer is quite easy. They are considered to be safer than pointers.

• Once assigned a reference can not be change to reference a different object. All operations on a reference are operations on the object it references.
• References use the member access operator . not the pointer to member access operator ->

The const is important to, it means that the function called is guaranteeing not to change the object that being reference.

In the constructor, you would probably want to set all of the coefficients to 0. The idea is just that you really NEVER want to have unassigned variables. What if you forgot they were assigned in the read function and tried to access them before that? You would end up with a junk value.

Dave

Edited by daviddoria: n/a

very clear in explanations

Dave, thanks for the help. That actually helps a lot in what I need to do, since (in addition to what you said) I'll have to have an option to add another polynomial, so I'll have to set the previous back to void.

Banfa, thank you for your explanation as well. I will take that information and work on my project a bit more now that these parts are explained.

If you post your code for your program that you have so far I can take a look at for you if you like.

I am curious as to what the rest of the code looks like