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.