How to equal using assignment operators
it shows insertion operators error but works when i use string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include<stdexcept>
#include<conio.h>
#include <iostream>
using namespace std;
class vector
{
public:
int x ,y;
vector(){};
vector(int a ,int b)
{
x=a;
y=b;
}
//: x(a),y(b){}
vector vector::operator= (vector b1)
{
vector b2;
b2.x=b1.x;
b2.y=b2.y;
return (b2);
}
};
int main ()
{
vector b1 (3,1) , b2(1,3);
vector b3;
b2= b1;
cout<<b2;
}
|
cout << b2.x << " " << b2.y;
I think this is right, though I may have overlooked something:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <conio.h>
#include <iostream>
using namespace std;
class vector
{
public:
int x ,y;
vector(){};
vector(int a ,int b)
{
x=a;
y=b;
}
vector & operator=(const vector & b1)
{
x = b1.x;
y = b1.y;
return *this;
}
friend ostream & operator<<(ostream & os, const vector & v);
};
ostream & operator<<(ostream & os, const vector & v)
{
os << '(' << v.x << ',' << v.y << ')';
return os;
}
int main ()
{
vector b1 (3,1) , b2(1,3);
vector b3;
b3 = b2;
b2 = b1;
cout << b2 << " ";
cout << b3;
}
|
Thank you everythings clear good programming can't we do that without using operator overload for<<
can't we do that without using operator overload for<< |
Of course we can. I gave you that as a free gift, take it if it's useful, ignore it if it isn't.
Topic archived. No new replies allowed.