Overloading Operator+

  • Thread starter Thread starter Pranav Shah
  • Start date Start date
P

Pranav Shah

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.
 
Thus spake Pranav Shah:
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.

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'.
 
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<iostream>

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.myImaginaryPart;
cout << "\nfriend " << b.myRealPart << " " << b.myImaginaryPart;

myComplex.RealPart() = a.RealPart() + b.RealPart;
myComplex.ImaginaryPart() = a.ImaginaryPart() + b.ImaginaryPart();
}

int main()
{
myComplex object1(1,2),object2(3,4);
myComplex object3;
int i;
object3 = object1+object2;
cout << "\nreal part ";
cout << object3.RealPart();
cout << " imaginary part ";
cout << object3.ImaginaryPart();
cin >> i;
return 0;
}
 
Pranav Shah said:
Here is the code. It should explain my problem.

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 :)
 
The heart of C# is still C++. Anywyas here is the overloaded operator
function that solves my problem.

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

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


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

return *this;
}
 
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
 
Thus spake Pranav Shah:
The heart of C# is still C++.

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.
 
Pranav Shah said:
The heart of C# is still C++.

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.
 
Back
Top