473,701 Members | 2,735 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 1896
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
3045
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: http://indicator.amessage.info/indicator.php?param1=cobnet%40jabber.org&amp;param2=bounce&amp;param3=http%3A%2F%2Fstudent.ugent.be%2Fastrid%2Fpics%2Fjabber%2F&amp;param4=.png
8
2943
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 fairly easy to accomplish - a quick and dirty ... what? ... script? program?
4
1909
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
2450
by: Tee | last post by:
String Builder & String, what's the difference. and when to use which ? Thanks.
12
17408
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 http://www.nathansokalski.com/
0
4762
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 in http://www.connectionstring.com/ is wrong:
3
7427
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
8344
by: 2005 | last post by:
context of a C program reading from a file? I know its end of file but ...not sure?
2
4111
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
8732
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9221
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9082
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8975
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8933
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6567
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5904
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4410
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4661
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.