PROGRAM TO ILLUSTRATE THE CONCEPT OF OPREATOR OVELOADING





Hello Friends!!!! Today I am going to learn about the concept of operator overloading using a simple vector program. In this program we have a class vector and a multiply operator to use and oveload.

So Let's Start=>

//Name : Mayank Sharma //
//Roll No : 1101026 //
//CSE(4th Sem)

PROGRAM TO ILLUSTRATE THE CONCEPT OF OPREATOR OVELOADING

#include
#include
const size=3;
class vector
{
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 v[i]=0;
}
vector :: vector(int *x)
{
for(int i=0;i v[i]=x[i];
}
vector operator * (int a,vector b)
{
vector c;
for(int i=0;i c.v[i]=a*b.v[i];
return c;
}
vector operator *(vector b, int a)
{
vector c;
for(int i=0;i c.v[i]=b.v[i]*a;
return c;
}
istream & operator >> (istream &din, vector &b)
{
for(int i=0;i din>>b.v[i];
return(din);
}
ostream & operator << (ostream &dout, vector &b)
{
for(int i=0;i dout< return(dout);
}
int x[size]={2,4,6};
void main()
{
clrscr();
vector m;
vector n=x;
cout<<"Enter Elements of vector m"<<"\n";
cin>>m;
cout<<"\n";
cout<<"m="< vector p,q;
p=2*m;
q=n*2;
cout<<"\n";
cout<<"p="< cout<<"q="< getch();
}

I think this post will help you.I think this post will help you.
Thanks!!!
If any query about this post then put your comment in comment box below..

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites