Pages

Tuesday, October 11, 2011

4.Operator Overloading

WAP USING CONSTRUCTOR OVERLOADING TO ENTER THE VALUES OF X & Y AND ADD AND SUB THEM

#include<conio.h>
#include<iostream.h>
class vector
{
float x,y;
public:
void getdata()
{
cout<<"\nEnter the value of x & y::";
cin>>x>>y;
}



vector operator+(vector v)
{
vector temp;
temp.x=x+v.x;
temp.y=y+v.y;
return(temp);
}

vector operator-(vector v)
{
vector temp;
temp.x=x-v.x;
temp.y=y-v.y;
return(temp);
}

void disp()
{
cout<<x<<"\t"<<y<<endl;
}

};

void main()
{
clrscr();
vector a,b,c,d;
a.getdata();
b.getdata();
c=a+b;
d=a-b;
cout<<"Addition::";
c.disp();

cout<<"Subtraction::";
d.disp();
getch();
}

No comments: