473,569 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

issue with << and >> overloading

ceo
Hi there,

I'm trying to overload insertion (<<) and extraction (>>) operators and
the program yields output I didn't expect. Could someone please clarify
what's wrong with my program.

Thanks,
Ceo

// program start

#include <iostream.h>

const int size=3;

class vector {

private:
int v[size];

public:
vector();
vector(int *x);
friend vector operator *(int a, vector b);
friend vector operator *(vector b, int a);
friend istream & operator >>(istream &, vector &);
friend ostream & operator <<(ostream &, vector &);
};

vector :: vector() {
for(int i=0; i<size; i++) {
v[i] = 0;
}
}

vector :: vector(int *x) {
for(int i=0; i<size; i++) {
v[i] = x[i];
}
}

vector operator *(int a, vector b) {
vector c;
for(int i=0; i<size; i++) {
c.v[i] = a * b.v[i];
}
return c;
}

vector operator *(vector b, int a) {
vector c;
for(int i=0; i<size; i++) {
c.v[i] = b.v[i] * a;
}
return c;
}

istream & operator >> (istream & din, vector & b) {
for(int i=0; i<size; i++) {
din >> b.v[i];
}
return din;
}

ostream & operator << (ostream & dout, vector & b) {
dout << "(" << b.v[0];
for(int i=0; i<size; i++) {
dout << ", " << b.v[i];
}
dout << ")";
return dout;
}

int x[size] = {2,4,6};

int main() {
vector m;
vector n = x;
cout << "size = " << size << "\n\n";
cout << "Enter elements of vector m " << "\n";
cin >> m;
cout << "\n";
cout << "m = " << m << "\n";
vector p, q;
p = 2 * m;
q = n * 2;

cout << "\n";
cout << "p = " << p << "\n";
cout << "q = " << q << "\n";

return 0;
}

// program end

// output start

size = 3

Enter elements of vector m
1 2 3

m = (1, 1, 2, 3)

p = (2, 2, 4, 6)
q = (4, 4, 8, 12)

// output end

// But I want the output to be:

size = 3

Enter elements of vector m
1 2 3

m = (1, 2, 3)

p = (2, 4, 6)
q = (4, 8, 12)

Jul 23 '05 #1
2 1383
"ceo" <ce******@gmail .com> wrote in message
news:11******** *************@l 41g2000cwc.goog legroups.com...
I'm trying to overload insertion (<<) and extraction (>>) operators and
the program yields output I didn't expect. Could someone please clarify
what's wrong with my program.

Thanks,
Ceo

// program start

#include <iostream.h>

const int size=3;

class vector {
It's probably not a good idea to name your own class "vector" because it
might get confused with the vector template from the standard library.

<<snip>>

ostream & operator << (ostream & dout, vector & b) {
dout << "(" << b.v[0];
Here you print b.v[0]
for(int i=0; i<size; i++) {
dout << ", " << b.v[i];
and here you print b.v[0] again, followed by b.v[1] and so on.
}
dout << ")";
return dout;
}


Be careful about what you ask for; you might get it :-)

Jul 23 '05 #2
ceo
> and here you print b.v[0] again, followed by b.v[1] and so on.

oh, sorry, that's a huge blunder.

Jul 23 '05 #3

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

Similar topics

2
3197
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are writing one object in C# which will take the entire table tag contents and renders. Ie., we need to pass "<table>………… <thead>……</thead>. <tr>.<td>...
2
10548
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script type="text/javascript"> <!]> </script> <script type="text/javascript"
2
2052
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 overloading of operator<<. In the .cc of the respective class or in a file where I am overloading operator<< for all classes? Cheers,
1
2687
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName> <EmployeeStrength>> 1000</EmployeeStrength> When I do a Xmltextreader.read() and then check the contents of the xml node by...
14
1807
by: SoilMan | last post by:
Consider the following: class xyz { public: template <typename T> void foo(T x) { cout << "foo<T> " << x << endl; }
2
2239
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 uses map<and heavy overloading. The problem is, the output file differs from input, and for the love of me I can't figure out why ;p #include...
11
5017
by: Squeamizh | last post by:
class my_class { public: my_class() : value(0) { } int& get_value() { return value; } const int& get_value() const { my_class& c = const_cast<my_class&>(*this); return c.get_value(); }
3
3357
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt, &gt ,&ltCompany&gt to display '<', '>' and '<Company>' respectively. But is there any freeware code available which could implement the above...
45
18809
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies of their elements? Why can't I change the element itself? class Program { private struct MyStruct
14
3127
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the PHP interpreter works like any other, that is, it first expands all the include files, and then parses the resulting text. Can anyone help with an...
0
7609
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
7921
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. ...
1
7666
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...
0
7964
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
6278
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...
1
5504
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...
0
5217
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...
1
2107
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
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.