I have a class to create an object that forms a polynomial of any degree.
And the class with its private part would be like this according to the example I'm trying to understand:
class polinomio
{
public:
//...
private:
double* coeficiente;
unsigned gradoMax;
unsigned grado;
};
Therefore I understand that:
unsigned gradoMax;
takes care of keeping the maximum degree of the polynomial.
unsigned grado;
If I'm not mistaken, it is the current degree that a coefficient has at a given moment. For example, if the polynomial is of degree 3 and I want to check what coefficient it has in degree 2, then this attribute will have a value of degree 2.
I think that coefficient is a vector of coefficients in which, according to its position in the vector, it will correspond to the independent term (degree 0), coefficient of degree 1 (position 1 of the vector) but in reality a pointer is declared there but... to what? This is what I can't understand:
double* coeficiente
What does that pointer do? Where does it point? Am I on the right track according to what I have stated above to achieve a polynomial object?
Thank you very much.
CLARIFICATION/EXTENSION
I have already managed to know what each thing consists of:
double *coeficiente;
It is a pointer that points to a dynamic vector with the coefficients, in the position i of the vector it will store the degree i.
unsigned gradoMax;
It is the maximum degree that can be stored.
unsigned grado;
It is the degree of the stored polynomial. It is the highest value of the index in the coefficient vector.
Therefore, in order not to open another question, I continue with the doubt, if you create a pointer within a class, as in the case of double *coeficiente;
Where is the dynamic vector created? I can't understand that.
Thanks again.
usually in the constructor. It would be something like this:
where
MAX_COEFICIENTES
it would be a number assigned by you or a constant that indicates the size of the array.The constructor implementation should look something like this:
At the end, the dynamically allocated memory must be freed in the class destructor, like this:
I was mostly asking what the pointer was:
And once I was able to with the help of @carlosezam along with some books and a little while I managed to understand it.
double* coeficiente
is a pointer that will store the memory address of a vector (vector that is defined in its constructor).When I use a constructor like this:
What is done is that
coef = new double[gradoMax];
(the pointer declared in the class) I tell it to storegradoMax
consecutive memory spaces from that memory address.Therefore, that is exactly where the vector is created that will store each coefficient in said consecutive positions according to the size declared in the Maxdegree variable.
Thank you all as always for your help! :)