[OVERLOADING OPERATORS USING FRIENDS]
<iostream.h >
size = 3;
class vector
{
v[size];
:
vector();
vector( *x);
operator *( a, vector b);
operator *(vector b, a);
friend istream & operator >> (istream &, vector &);
friend ostream & operator << (ostream &,vector &);
};
vector : : vector()
{
for ( i=0; i < size; i++)
v[i] =0;
}
vector : : vector( * x)
{
for ( i =0; i< size; i++)
v[i] = x[i];
}
vector operator *( a, vector b)
{
vector c;
for ( i=0; i< size; i++)
c.v[i] = a*b.v[i];
c;
}
vector operator *(vector b, a)
{
vector c;
for ( i=0; i< size; i++)
c.v[i] = b.v[i], *a;
c;
}
istream & operator >> (istream &din, vector &b)
{
for( i=0; i< size; i++)
din >> b.v[i];
(din);
}
ostream & operator << (ostream &dout, vector &b)
{
dout << "(" << b.v[0];
for( i=1; i < size; i++)
dout << ", " << b.v[i];
dout << ")";
(dout);
}
x[size] = {2,4,6};
main()
{
vector m;
vector n= x;
std::cout<< "Enter elements of vector m" << "\n";
std::cin>> m;
std::cout<< "\n";
std::cout << "m= " << m << "\n";
vector p, q;
p = 2 * m;
q = n * 2;
std::cout<< "\n";
std::cout<< "p = " << p << "\n";
std::cout<< "q = " << q << "\n";
0;
}
|