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 {
      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;
              double* pointer;
Quadratic::Quadratic(double value1,double value2,double value3)
  cout<<"Constructor: ";
  pointer = new double[3];
     cout<<"Destructor: ";
     delete pointer;
Quadratic::Quadratic(const Quadratic& a)
                           cout<<"Copy constructor:"; 
                           if(a.pointer == NULL) pointer = NULL;
                           pointer = a.pointer;
Quadratic& Quadratic::operator=(const Quadratic& a)
           cout<<"Assignment: ";
           if (this != &a)
                    delete pointer;
                    if (a.pointer == NULL ) pointer = NULL;
                    pointer = a.pointer;
return  *this;

Quadratic& Quadratic::operator+(const Quadratic& a)
         double y;
         double z;

Quadratic& Quadratic::operator-(const Quadratic& c)

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.

You haven't even tried to overload + and - operator. First read about it,try it and come back to us with errors or bugs,then we will help you. And next time put your code inside code tags.

P.S : We are not a bunch of jobless people.

Let's look at your Quadratic::operator=().
It's funny but (not indented properly in your code)

if (a.pointer == NULL ) 
    pointer = NULL;
    pointer = a.pointer;

has exactly the same effect as

pointer = a.pointer;

Why if-else? Think a little before coding...

However it's a wrong assignment operator. Now both objects refer (point) to the same dynamically allocated memory chunk. It's impossible to destroy this pair correctly. The first destructor deallocates memory then the second destructor deallocates the same (already deallocated) memory!

You must provide the left side object (this) with a copy of the right side (a) object. Possible variants:

1. This object has not (yet) its own array referred via pointer member (note: it's a bad name). No need to delete this->pointer: it's equal to 0.
1.1. The right object has not its array too - do nothing, empty = empty...
1.2. the right object has an array - make new array and fill it with right array contents.
2. This object has an array (this->pointer != 0).
2.1. The right object has not its array - delete this array, make this object empty.
2.2. The right object has an array - copy the right array elements to this array.

You have wrote the same code (except if (this != &a) in the copy constructor. Feel the difference: now you have raw memory to construct this object. No sensible this->pointer value: you must construct this object from the scratch.

Regrettably, there are lots of defects in all other class Quadratic members (including senseless statements like a.pointer[0]; )...

Quadratic multilication:

If you multiply two quadratics together you don't normally get another quadratic, (normally it is a quartic (x^4) ). What that really means is that quadratic(ness) should a property of a class, not itself a class. [Assuming that you are going to require multiplication]

So write a polynominal class and then has methods like bool isQuadratic() const or int order() const .
That makes everything lots easier.

Poly& operator*=(const Poly&); 
Poly operator*(const Poly&) const;

I normally set a nice little problem with template<int NVariable> class Poly when I teach a C++ for scientists course. [ Poly<3> represents a polynominal in three variables e.g x,y,z ]

Shouldnt pointer be destructed using a delete []; and not delete; ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.