Click to See Complete Forum and Search --> : Add complex number using new and delete operator


anandtugaon
May 20th, 2008, 01:42 AM
Hi All,

I am learning c++, i am facing one problem adding the two objects , please help me How can i proced??


---------------------Code -------------------------------------------------------------------

#include<iosteream>

Using namespace std;

class Complex
{

double Real, imag;

public:

Complex()
{
Real = 0;
imag = 0;
}

Complex(double);
Complex(double, double);
};



void Complex ::display()
{

cout << Real << "+i" << imag << "\n" ;;

}


Complex ::Complex(double aa)
{
Real = aa;
imag = 0;
}

Complex :: Complex( double aa, double bb)
{

Real = aa;
imag = bb;

}

void main()
{

Complex *p = NULL;

Complex *a = new Complex(2, 4);
Complex *b = new Complex(2, 4);

a->display();
b->display();


//p = a->operator +(b); // Here i am trying to add the complex number ??

}


Please how can add a and b complex number.

Thanks,
Anand

laasunde
May 20th, 2008, 02:07 AM
Use code tags.

Use int main not void main.

ChessMaster
May 20th, 2008, 02:48 AM
Firstly,
Complex *a = new Complex(2, 4);
Complex *b = new Complex(2, 4);

when you new an object in c++, always delete it! No exceptions!! Never!!!


Second, coming to your problem.
Complex is a class defined by you. Someone needs to define what is the outcome when you add 2 Complex objects. Since you wrote the class, and since you know what it(the class) defines, you need to define what the + operation would result in. Hence overload the operator+ and define what it would result in. For more on how to, Google.

JohnW@Wessex
May 20th, 2008, 03:06 AM
The signature for operator + is
T operator + (const T &lhs, const T &rhs)
{
T result(lhs);

result += rhs;

return result;
}
where T is the type. This is implemented as a friend function.

Also you may want to implement operator +=which will be a member function. It's usual to implement operator + in terms of operator +=

T &operator += (const T &rhs)
{
// Add rhs here.

return *this;
}
Notice that operator + returns a value but operator += returns a reference.

BTW Is there any good reason why you must new and delete the complex numbers? Just declaring them would be good enough.

Paul McKenzie
May 20th, 2008, 03:12 AM
BTW Is there any good reason why you must new and delete the complex numbers? Just declaring them would be good enough.Of course there is no good reason. The OP should read this:

http://www.flounder.com/badprogram.htm#Using%20new%20without%20any%20reason%20to

Regards,

Paul McKenzie

Paul McKenzie
May 20th, 2008, 03:22 AM
Hi All,

I am learning c++,
First, why are you using new and delete anyway? There is absolutely no reason to be using it for the code you posted. Here is the main() function, rewritten as a coherent C++ function.

int main()
{
Complex p;
Complex a(2, 4);
Complex b(2, 4);

a.display();
b.display();

p = a + b;
}

Please how can add a and b complex number.The operator + must be a friend function that has the following signature:

Complex operator +(const Complex& lhs, const Complex &rhs);

However, it is better to write operator + in terms of the member operator +=

Complex::Complex& operator += (const Complex& rhs)
{
// add rhs to this
// you fill this in....
//.....
return *this;
}

//...
Complex operator +(const Complex& lhs, const Complex &rhs)
{
Complex temp = lhs;
temp += rhs;
return temp;
}

Also,

1) Use code tags when posting code. The code you posted is unreadable.

2) Do not hastily type your code in the code window. There is no such include file as <iosteream>. Instead, copy the code directly from your code editor and paste it in the message, so that there are no mistakes that could mislead us.

Regards,

Paul McKenzie

JohnW@Wessex
May 20th, 2008, 03:49 AM
Of course there is no good reason. The OP should read this:I didn't really think so, but I'd thought I'd give him the benefit of the doubt just in case there was a lot more to his application than was actually posted.