Problem with operator overloading

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.

Until now i have come up with the following code:

#include <iostream>
using namespace std;

class Quadratic {
public:
Quadratic();
Quadratic (double value1,double value2,double value3);
~Quadratic ();
Quadratic(const Quadratic& a);
Quadratic& operator=(const Quadratic& a);
Quadratic& operator+(const Quadratic &a);
Quadratic& operator-(const Quadratic& c);
Quadratic& operator*();

void print() const;


private:

double* pointer;

};

Quadratic::Quadratic(double value1,double value2,double value3)
{
cout<<"Constructor: ";
pointer = new double[3];
pointer[0]=value1;
pointer[1]=value2;
pointer[2]=value3;
}
Quadratic::~Quadratic()
{
cout<<"Destructor: ";
delete pointer;
}
Quadratic::Quadratic(const Quadratic& a)
{
cout<<"Copy constructor:";
if(a.pointer == NULL) pointer = NULL;
else
pointer = a.pointer;
}

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.
Hello,

Try this link http://www.cplusplus.com/doc/tutorial/classes2/.
It enlightens really a bit.
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-.


you forgot to define

1
2
3
Quadratic::Quadratic()
{ pointer = new double[3];
}


Also your destructor is wrong:
1
2
3
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!!
} 
Last edited on
Ok guys,
so far i edited my overloaded operators + , - and * like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
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;
         
}  

Quadratic& Quadratic::operator-(const Quadratic& c)
{
           Quadratic result;
           for(int i=0;i<3;i++)
           result.pointer[i]=c.pointer[i]-this->pointer[i];
                   
               return result;
                   
}
Quadratic& Quadratic::operator*(const Quadratic& d)
{
           Quadratic result;
           for(int i=0;i<3;i++)
           
                   result.pointer[i]=d.pointer[i]*this->pointer[i];
                   return result;
                   }


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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Quadratic& Quadratic::operator=(const Quadratic& a)
{
           cout<<"Assignment: ";
           if (this != &a)
           {
                    delete pointer;
                    if (a.pointer == NULL ) pointer = NULL;
                    else
                    pointer = new double [3];
                    }
return  *this;
}    


Quadratic::Quadratic(const Quadratic& a)
{
                           cout<<"Copy constructor:"; 
                           if(a.pointer == NULL) pointer = NULL;
                           else
                           pointer = new double [3];
}


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).
Last edited on
"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.
Your operator= should do something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 = new double [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.
return result;

inside both operator+ and operator- should still be generating compile warnings.

umm, why, what is wrong? How should it be properly done?

[Edit:] I see... it should be
1
2
//good
Quadratic Quadratic::operator+(const Quadratic& a){/**/}

instead of
1
2
//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?
Last edited on
Return a reference (ie, pointer) to a stack variable that has since been destroyed and the memory freed.

operator+ should be a const method also.

Topic archived. No new replies allowed.