Hi everyone, im fairly new to programming and i'm having problems solving my homework. My assignment is:
Define a class Quadratic that stores the coefficients of a quadratic polynomial in a dynamically allocated array of doubles. Supply the "big three" memory management functions. Use this class to demonstrate
(a) the difference between initialization
Quadratic s;
Quadratic t = s;
and assignment
Quadratic s;
Quadratic t;
s = t;
(b) the fact that all constructed objects are automatically destroyed
(c) the fact that the copy constructor is invoked if an object is passed by value to a function
(d) the fact that the copy constructor is not invoked when a parameter is passed by reference
(e) the fact that the copy constructor is used to copy a return value to the caller.
Supply a member function which calculates the value of a quadratic function for a given argument value. Overload + and - operators for addition and subtraction of two polynomials and the unary operator * which return true or false when the corresponding quadratic equation has or has not real roots. Overload the stream operators << and >>. Demonstrate all these functions and operators.
Quadratic& Quadratic::operator=(const Quadratic& a)
{
cout<<"Assignment: ";
if (this != &a)
{
delete pointer;
if (a.pointer == NULL ) pointer = NULL;
else
pointer = a.pointer;
}
return *this;
}
Quadratic& Quadratic::operator+(const Quadratic& a)
{
a.pointer[0];
double y;
double z;
}
Quadratic& Quadratic::operator-(const Quadratic& c)
{
a.pointer[0];
}
int main ()
{
return 0;
}
It's a sample code just to see if its working. Until now i have been using a peace of pseudo code for java, but I cannot proceed any further. The problem is that I cannot properly overload the operators + , - and * (I try to call two parameters and the compiler prints an error that the overloaded operator can have either zero or one implicit parameter). And i'm not sure that my constructor and copy constructor are written the best way. So i would appreciate any tips/guidance on how can i a) improve my code and b) actually solve the problem. I'm not asking you to write it for me , just ( if possible) give me some tips Thanks in advance to any1 who reads this thread.
Your copy constructor and assignment operators are both wrong. You need to take a deep copy of the pointer.
The signatures of your operator+ and operator- methods are also wrong. I assume you want to be able to add and subtract Quadratics.
Given the code:
1 2
int a = 5, b = 4;
int x = a + b;
The expression a + b does not modify a or b, but instead returns a third int which is the sum of a and b. Your operator+ should do the same if you replace "int" with "Quadratic". Furthermore, if a and b are Quadratics, then a + b invokes operator+ on the "a" object with "b" as parameter. Therefore, your operator+ should be a const member function (it will not modify a). Moreover, you need to return a third instance of Quadratic from the function, which means that you need to create an instance on the stack and return a copy of it, not a reference to it.
Lastly, I have no idea what you want to do with operator* (the dereference operator). Do you mean multiplication? If so, it needs to follow operator+ and operator-.
delete [] pointer; //correct when you allocated with pointer = new double[x];
delete pointer; // incorrect when you allocated with pointer = new double[x];
//correct when you allocated with pointer = new double;
Your operator+ must return a value!
[Edit:] made an important correction in the below, inspired by jsmith's comment!
1 2 3 4 5 6 7 8
Quadratic Quadratic::operator+(const Quadratic& a)
{ Quadratic result;
for(int i=0;i<3;i++)
{ result.pointer[i]=a.pointer[i]+this->pointer[i];
}
return result;//note: result is a temporary object.
//return result; in fact calls the Quadratic::Quadratic(const Quadratic& a) copy constructor!!
}
but i still cannot grasp the idea behind the overloading of =.
I read the links u've posted and i understood that by invoking (for example) a=b (a and b are objects of class Quadratic) i actually invoke the overloaded operator = with explicit parameter the object a and implicit parameter object b. But you say i shouldn't just redirect the pointers , instead i should create a new deep copy of object a array of pointers and direct object b pointer to the new dynamic array. Is this the way it is done ? (overloaded = and copy constructor)
I think i'm just creating new chunks of dynamic memory , not actually copying the values of the first object's array to the newly created one (dont know how).
"i still cannot grasp the idea behind the overloading of =."
Hi,
Well, it seemes to me C++ is having an endless array of options.
One can copy entire class holder with this operator
or make a link. The tutorial says that operator is not
inherited not specifiyng why.
Quadratic& Quadratic::operator=(const Quadratic& a)
{ cout<<"Assignment: ";
for (int i=0;i<3;i++)
{ this->pointer[i]=a.pointer[i];
}
return *this;
}
Quadratic::Quadratic(const Quadratic& a)
{ cout<<"Copy constructor:";
pointer = newdouble [3];
for (int i=0;i<3;i++)
{ this->pointer[i]=a.pointer[i];
}
}
Now, when you call x=y+z: (y+z) calls the operator+ of object y. The returned result is a "ghost" object, automatically created by the compiler, without a name. The ghost object is created when you call return result; in your operator+, and is initialized via the copy constructor above. Then x=ghost_object; is executed.
//bad
Quadratic& Quadratic::operator+(const Quadratic& a)
That is because when you call x+y a "ghost object" MUST be created, so the second declaration doesn't make sense. A question: what will the second wrong declaration *actually* do?