Overloading Operator+

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pranav Shah

    Overloading Operator+

    I want to Overload operator "+" such that I can add two complex numbers
    and but the sum into a third one, but I only want to call the
    constructor 3 times.

    Here is the current algorithm:

    Declare Three complex objects:
    Complex a,b,c;

    Sum a and b and save the result in c:
    c = a + b;


    The current function that I have to do this looks something like:
    return( new Complex(a.Real + b.Real, a.Imaginary + b.Imaginary);


    Because of this the constructor is called 4 times.
    3 times when the objects are defined in the main program and once more
    when the Operator overloading is called.
    How can I cut this down to 3 Constructor calls? I know I am supposed to
    use pointers, but I do not know the right syntax.
    Any help will be great. Thanks.




    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
  • Frank Oquendo

    #2
    Re: Overloading Operator+

    Thus spake Pranav Shah:
    [color=blue]
    > Declare Three complex objects:
    > Complex a,b,c;
    >
    > Because of this the constructor is called 4 times.
    > 3 times when the objects are defined in the main program and once more
    > when the Operator overloading is called.[/color]

    Not so. Declaring an object does not instantiate it. Unless you've
    already created a new Complex object and stored its reference in 'c',
    'c' is not initialized until you add 'a' and 'b'.

    --
    There are 10 kinds of people. Those who understand binary and those who
    don't.




    Comment

    • Pranav Shah

      #3
      Re: Overloading Operator+

      Here is the code. It should explain my problem.


      #include "stdafx.h"

      // Defines the entry point for the console application.
      // purpose: very very simple complex class with initial constructor
      // class : myComplex
      #include<iostre am>

      using namespace std;
      class myComplex {
      double myRealPart, myImaginaryPart ;
      public:
      myComplex( double x, double y){
      myRealPart = x;
      myImaginaryPart =y;
      cout << "\nconstructor( 2)parameter" << myRealPart << " " <<
      myImaginaryPart ;
      cout << "\n";

      }
      myComplex( ){
      myRealPart = myImaginaryPart =0;
      cout << "\nconstructor( )parameter "<< myRealPart << " " <<
      myImaginaryPart ;
      cout << "\n";
      }
      double RealPart(){
      return myRealPart ;
      }
      double ImaginaryPart() {
      return myImaginaryPart ;
      }
      friend myComplex operator + (myComplex , myComplex );
      };
      myComplex operator + (myComplex a, myComplex b){
      cout << "\nfriend " << a.myRealPart << " " << a.myImaginaryPa rt;
      cout << "\nfriend " << b.myRealPart << " " << b.myImaginaryPa rt;

      myComplex.RealP art() = a.RealPart() + b.RealPart;
      myComplex.Imagi naryPart() = a.ImaginaryPart () + b.ImaginaryPart ();
      }

      int main()
      {
      myComplex object1(1,2),ob ject2(3,4);
      myComplex object3;
      int i;
      object3 = object1+object2 ;
      cout << "\nreal part ";
      cout << object3.RealPar t();
      cout << " imaginary part ";
      cout << object3.Imagina ryPart();
      cin >> i;
      return 0;
      }




      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Jon Skeet

        #4
        Re: Overloading Operator+

        Pranav Shah <shahp@aetna.co m> wrote:[color=blue]
        > Here is the code. It should explain my problem.[/color]

        Well the first problem is that it's in C++ rather than C#. I don't know
        about anyone else, but I'd been expecting the code to be in C#, given
        that this is the C# group :)

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Pranav Shah

          #5
          Re: Overloading Operator+

          The heart of C# is still C++. Anywyas here is the overloaded operator
          function that solves my problem.

          myComplex& myComplex::oper ator + (myComplex &b){
          cout << "\nfriend " << myRealPart << " " << myImaginaryPart ;

          cout << "\nfriend " << b.RealPart() << " " << b.ImaginaryPart ();


          myRealPart = myRealPart + b.RealPart();
          myImaginaryPart = myImaginaryPart + b.ImaginaryPart ();

          return *this;
          }



          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • Frank Oquendo

            #6
            Re: Overloading Operator+

            Thus spake Pranav Shah:
            [color=blue]
            > The heart of C# is still C++.[/color]

            That's like saying the heart of Java is C++. They may look alike but
            they're not the same so there's not much point in asking for C++ help in
            a C# group.

            --
            There are 10 kinds of people. Those who understand binary and those who
            don't.




            Comment

            • Jon Skeet

              #7
              Re: Overloading Operator+

              Pranav Shah <shahp@aetna.co m> wrote:[color=blue]
              > The heart of C# is still C++.[/color]

              They're *very* different languages which may look similar but have very
              different semantics. The natural implementation of your problem in C#
              *would* only have called the constructor 3 times.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Magnus Lidbom

                #8
                Re: Overloading Operator+

                This code may appear to fix your current problem, but it's fundamentally
                broken in several ways. I suggest you post this code in comp.lang.c++ and ask
                them why it's a very bad idea implement the operator that way.

                Best wishes. /Magnus Lidbom

                "Pranav Shah" <shahp@aetna.co m> wrote in message
                news:uAuJW5ggDH A.2080@tk2msftn gp13.phx.gbl...[color=blue]
                > The heart of C# is still C++. Anywyas here is the overloaded operator
                > function that solves my problem.
                >
                > myComplex& myComplex::oper ator + (myComplex &b){
                > cout << "\nfriend " << myRealPart << " " << myImaginaryPart ;
                >
                > cout << "\nfriend " << b.RealPart() << " " << b.ImaginaryPart ();
                >
                >
                > myRealPart = myRealPart + b.RealPart();
                > myImaginaryPart = myImaginaryPart + b.ImaginaryPart ();
                >
                > return *this;
                > }
                >
                >
                >
                > *** Sent via Developersdex http://www.developersdex.com ***
                > Don't just participate in USENET...get rewarded for it![/color]


                Comment

                Working...