CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2007
    Posts
    134

    Add complex number using new and delete operator

    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 :isplay()
    {

    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

  2. #2
    Join Date
    Jan 2003
    Posts
    615

    Re: Add complex number using new and delete operator

    Use code tags.

    Use int main not void main.
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  3. #3
    Join Date
    Aug 2007
    Posts
    135

    Re: Add complex number using new and delete operator

    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.
    Last edited by ChessMaster; May 20th, 2008 at 02:55 AM.

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Add complex number using new and delete operator

    The signature for operator + is
    Code:
    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 +=

    Code:
    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.
    Last edited by JohnW@Wessex; May 20th, 2008 at 03:08 AM.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Add complex number using new and delete operator

    Quote Originally Posted by JohnW@Wessex
    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.h...%20reason%20to

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Add complex number using new and delete operator

    Quote Originally Posted by anandtugaon
    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.
    Code:
    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:
    Code:
    Complex operator +(const Complex& lhs, const Complex &rhs);
    However, it is better to write operator + in terms of the member operator +=
    Code:
    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

  7. #7
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Add complex number using new and delete operator

    Quote Originally Posted by Paul McKenzie
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured