Wednesday 17 July 2013

Operator overloading in c++ with example

Operator overloading

This gives us flexibility to use operators with class objects.

Operator overloading is done by two ways

1. Member function : Take atmost one argument
2. Friend function: Take at most two arguments

This limits the overloading to apply for uninary and binary operators.

// The Test Class show the opertor overloading
// Overloads + operator using Friend function
// Overloads - operator using Member function
class test
{
public:
int a;
int b;
friend test operator +(test &, test &);
test operator -(test &);
test()
{
}
test(int x, int y)
{
a=x;
b=y;
}};test test::operator -(test &t1)
{
test temp;
temp.a= t1.a-a;
temp.b= t1.b-b;
return temp; }test operator +(test &t1, test &t2)
{
test temp;
temp.a=t1.a+t2.a;
temp.b=t2.b+t2.b;
return temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
test t1(3000,2000);
test t2(1000,1000);
test t3;t3= t1-t2;
cout<<t3.a<<t3.b<<endl;
t3=t1+t2;
cout<<t3.a<<t3.b<<endl;
return 0;
}



// Class Balance Perfom operator Overloading
/// Overloads + * using member function
// Overloads / - using friend function
class balance
{
public:
int start;
int end;
public:
friend balance operator /(balance &, balance &);
balance()
{
start=end=0;
}
balance(int x,int y)
{
start=x;
end=y;
}
balance operator +(balance&);
friend balance operator -(balance &, balance &);
balance operator *(balance &);
};
balance balance::operator*(balance &b1)
{
balance temp;
temp.start= b1.start*start;
temp.end=b1.end*end;
return temp;
 }balance operator /(balance &b1,balance &b2)
{
balance temp;
temp.start= b1.start / b2.start;
temp.end =b1.end /b2.end;
return temp;
}
 
 
 
 
balance balance:: operator +(balance &b1)
{
balance temp;
temp.start=b1.start+start;
temp.end=b1.end+end;
return temp;
}
balance operator -(balance &t1, balance &t2)
{
balance temp;
temp.start=t1.start -t2.start;
temp.end=t1.end- t2.end;
return temp; }int _tmain(int argc, _TCHAR* argv[])
{
 balance b1(100,0);
balance b2(100,20);
balance b3;
cout<<"------------START---------------------\n";
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;
cout<<"------------add---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1+b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;
cout<<"------------sub---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1-b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;

cout<<"------------mul---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1*b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;
cout<<"------------div---------------------\n";
cout<<"b1.start"<<b1.start<<endl;
cout<<"b1.end"<<b1.end<<endl;
cout<<"b2.start"<<b2.start<<endl;
cout<<"b2.end"<<b2.end<<endl;
b3=b1/b2;
cout<<"b3.start"<<b3.start<<endl;
cout<<"b3.end"<<b3.end<<endl;
return 0;
}

No comments:

Post a Comment

Apache Beam Learning In Java Tutorials

 Wow  https://youtu.be/9kGETU63rkc