473,545 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is the difference between with & and withou &

Hi,
I compiled some code. In the function friend ostream&
operator<<(ostr eam& 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<<(ostr eam& os, const complex c);

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

ostream& operator<<(ostr eam& 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 1887
learning_C++ wrote:
Hi,
I compiled some code. In the function friend ostream&
operator<<(ostr eam& 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<<(ostr eam& os, const complex c);

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

ostream& operator<<(ostr eam& 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
3023
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 image:...
8
2941
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 appeared on my news server, so I'm trying again - this time posting a separate copy of the message to each group. I'm thinking this should be...
4
1887
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
2429
by: Tee | last post by:
String Builder & String, what's the difference. and when to use which ? Thanks.
12
17363
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, and I would like to know what it is so that I can take advantage of it in my code. Thanks. -- Nathan Sokalski njsokalski@hotmail.com...
0
4758
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 for a connection string wihout DSN using IBM DB2 OLE PROVIDER in windows environment, but unfortunately, I found none. The DB2 connection string...
3
7402
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 ( maximum element ) while set and heap can erase any element ...
9
8339
by: 2005 | last post by:
context of a C program reading from a file? I know its end of file but ...not sure?
2
4103
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 e) { if (!IsPostBack) {
0
7475
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7409
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7664
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7921
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7771
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5982
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1900
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
720
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.