473,412 Members | 2,277 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,412 software developers and data experts.

what is the difference between with & and withou &

Hi,
I compiled some code. In the function friend ostream&
operator<<(ostream& os, const complex c);

I use the later argument complex c and complex& c. I can get the same
values and there is no error when I compile this code.
Please tell me the difference.
#include <iostream>
using namespace std;

class complex{
double re, im;
friend ostream& operator<<(ostream& os, const complex c);

public:
complex(double r, double i):re(r), im(i){}
complex operator+(complex& c){
return complex(re + c.re, im + c.im);
};
//complex operator*(complex);
};

ostream& operator<<(ostream& os, const complex c){
os <<"("<<c.re<<","<<c.im<<")";
return os;
}
int main(){
complex a =complex(1, 3.1);
complex b=complex(1.2, 2);
complex c=b;
a =b+c;
cout<<a<<endl;
return 0;
}
Jul 22 '05 #1
1 1872
learning_C++ wrote:
Hi,
I compiled some code. In the function friend ostream&
operator<<(ostream& os, const complex c);

I use the later argument complex c and complex& c. I can get the same
values and there is no error when I compile this code.
Please tell me the difference.

Google for 'references' in C++.

Briefly, 'complex & c' in a function parameter list , indicates you are
passing the reference instead of copy of the object.


#include <iostream>
using namespace std;

class complex{
double re, im;
friend ostream& operator<<(ostream& os, const complex c);

public:
complex(double r, double i):re(r), im(i){}
complex operator+(complex& c){
return complex(re + c.re, im + c.im);
};
//complex operator*(complex);
};

ostream& operator<<(ostream& os, const complex c){
os <<"("<<c.re<<","<<c.im<<")";
return os;
}
Well - there is a STL library called complex, that would much more
than what you had mentioned before. You can use that if you are writing
an application.
int main(){
complex a =complex(1, 3.1);
complex b=complex(1.2, 2);
complex c=b;
a =b+c;
cout<<a<<endl;
return 0;
}

--
Karthik.
Jul 22 '05 #2

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

Similar topics

13
by: Mattias Campe | last post by:
Hi, Depending on if I get an image or a text of a certain URL, I want to do something different. I don't know in advance whether I'll get an image or a text. This is a URL that returns an...
8
by: Al Davis | last post by:
Note: I tried cross-posting this message to several newsgoups, including comp.lang.perl.misc, c.l.p.moderated, comp.infosystems.www.authoring.cgi, comp.lang.javascript and comp.lang.php. Nothing...
4
by: Joel | last post by:
Run this method: public void test() { bool b; int i=0; b=false; i=0; b=(b && i++==1);
12
by: Tee | last post by:
String Builder & String, what's the difference. and when to use which ? Thanks.
12
by: Nathan Sokalski | last post by:
What is the difference between the Page_Init and Page_Load events? When I was debugging my code, they both seemed to get triggered on every postback. I am assuming that there is some difference,...
0
by: alingsjtu | last post by:
What's the DB2 connection string wihout DSN when use IBM DB2 OLE PROVIDER in windows environment, such as ADO or Creation of Linked Server in MS SQL Server ? I searched for a long time to look...
3
by: PicO | last post by:
i need some explanation about the difference between priority queue & set & heap ... as they all sort the data in ( n log n ) ... but the only i see that priority queue only can pop the top (...
9
by: 2005 | last post by:
context of a C program reading from a file? I know its end of file but ...not sure?
2
by: qwedster | last post by:
Folks! What is the difference between PostBack and Callback ( !IsPostBack and if(!IsCallback)) Like in the following Code Snippet: protected void Page_Load(object sender, EventArgs...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.