Polynomial Class
polynomial::polynomial() { degree = 0; expr = NULL; } polynomial::polynomial(const polynomial& p) { } The copy constructor will perform a deep copy of the polynomial passed to it. I don't understand how to deep copy into the second function.
Also this one • The constructor that takes an integer pointer and an integer. It will set the degree field with the integer value and the integer pointer will be a dynamic array so a deep copy of that to expr will be done. polynomial::polynomial(int* poly_expr, int d) { }
For the first post: polynomial::polynomial(const polynomial& p) { degree = p.degree; expr = p.expr; } You are passing a reference of a polynomial object, as a parameter, to the copy constructor. Thus you can call the copy constructor (like this): polynomial A; A.degree = 60; A.expr = 1; polynomial B = A; - Copies the members of object A into object B 'polynomial B = A' is equivalent to 'polynomial B(A)' and might make more sense when looking at the copy constructor as well. Moving on and looking from the previous POV, look to the copy constructor I wrote. polynomial::polynomial(const polynomial& p) { degree = p.degree; expr = p.expr; } Replace with: polynomial::polynomial(A) { degree = A.degree; expr = A.expr; } See now how the copy constructor works?
There is also an interesting tidbit on why you need to pass the object as a reference instead of by value in case you are wondering. You will some interesting behavior when you use the regular constructor to copy one object onto another of the same class.
I am going to assume this is c++
Join our real-time social learning platform and learn together with your friends!