473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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!
Nov 15 '05 #1
7 2042
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'.

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

http://code.acadx.com
Nov 15 '05 #2
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;
}


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
Pranav Shah <sh***@aetna.com> wrote:
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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
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;
}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #5
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.

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

http://code.acadx.com
Nov 15 '05 #6
Pranav Shah <sh***@aetna.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
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" <sh***@aetna.com> wrote in message
news:uA**************@tk2msftngp13.phx.gbl...
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;
}

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

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.