Connecting Tech Pros Worldwide Help | Site Map

issue with << and >> overloading

ceo
Guest
 
Posts: n/a
#1: Jul 23 '05
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)

Andrew Koenig
Guest
 
Posts: n/a
#2: Jul 23 '05

re: issue with << and >> overloading


"ceo" <ceo.msft@gmail.com> wrote in message
news:1110218105.927758.70530@l41g2000cwc.googlegro ups.com...
[color=blue]
> 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 {[/color]

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>>

[color=blue]
> ostream & operator << (ostream & dout, vector & b) {
> dout << "(" << b.v[0];[/color]

Here you print b.v[0]
[color=blue]
> for(int i=0; i<size; i++) {
> dout << ", " << b.v[i];[/color]

and here you print b.v[0] again, followed by b.v[1] and so on.
[color=blue]
> }
> dout << ")";
> return dout;
> }[/color]

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



ceo
Guest
 
Posts: n/a
#3: Jul 23 '05

re: issue with << and >> overloading


> and here you print b.v[0] again, followed by b.v[1] and so on.

oh, sorry, that's a huge blunder.

Closed Thread