Overloading
Overloading
C++ overloads the built-in operators for the primitive data types
Example:
int x,y;
x=10+5;
y=10;
Overload class type
Example:
Employee e1(…);
Employee e2, e3;
e3=e1+e2; //compiler error unless you overload + operator for type employee
Example:
Assume A and B are programmer defined classes
A a;
B b;
a=b;
Overloading operator restrictions
o Cannot overload operators as they applied to built-in data-types
Can't overload (+) if it is used in the following statement:
int y=x+5;
o Must respect the original "functional" template of the operators.
Cant convert unary to binary
o Cannot change operators procedence
Can't make + operator a higher precedence
o C++ allows to overload any operator except class member operator
dot operator (.)
Resolution operator (::)
Conditional operator ( ? )
Class member deference operator
Overloading an operator
o Create a method to overload an operator
o Declaration
Inside class (public section)
Syntax
Return type operator op (arguments);
o operator-key word
o op: +,-,/,*, ….
o Definition (implementation)
Inside or outside of class
Syntax
Returned type classname::operator op (arguments) {…}
1
Example:
Test s;
cin>>s;
cout<<s;
Class Test
{
int a,b;
public:
Test()
{
a=0;
b=0;
}
Test(int x, int y)
{
a=x;
b=y;
}
//overload >>
//friend allows you to accept object outside of class
friend istream &operator >>(istream &in, Test &o)
//overload <<
friend ostream &operator << (ostream &out, Test &o)
};
//Define outside of class
ostream &operator << (ostream &out, test &o)
{
cout<<"Values of a & b: "<<endl;
cout<< o.a<<" "<<o.b<<endl;
return out;
}
//In Main
int main()
{
Test s;
cin>>s; //calls overloading
cout<<s //calls overloaded;
return 0;
}
2
Overloading binary operator with programmer defined class
Class Test
{
int a,b;
public:
Test()
{
a=0;
b=0;
}
Test(int x, int y)
{
a=x;
b=y;
}
Test operator + (Test &x);
};
Test::Test Operator +(Test &x)
{
Test result;
result.a=a+x.a;
result.b=b+x.b
return result;
}
Test cc;
Test aa(2,3);
Test bb(7,5);
cc=aa+bb;
Overloading binary operator with programmer defined class and built in data type
Problem:
Test s1, s2;
s2=s1+10;
s2=10+s1;
//In the class test
public:
friend Test operator + (test &x, int )
friend Test operator + (int, test &x)
//Outside of the class test
Test::operator +(Test &x, int y)
{
Test temp;
temp.a=x.a+y;
temp.b=x.b+y;
Return temp
}
3
Test::operator +(int y,Test &x)
{
Test temp;
temp.a=x.a+y;
temp.b=x.b+y;
return temp;
}
o Alternative
Test::Test operator ++()
{
++a;
4
++b;
return *this;
}
Note: This is a pointer reference to an operator calling the object function
5
Type conversion
6
e=y; //basic to class
Polar p;
Cartesian c;
p=c;
//or
c=p;
Class Cartesian
{
double x;
double y;
public:
Cartesian()
{x=0;y=0;}
Cartesian(doubly x, double y)
{
this.x=x;
this.y=y;
}
//added constructor
Cartesian(Polar p)
{
double r=P.getRadius();
double a=p.getAngle();
x=r*cos(a;)
y=r*cos(a);
}
};
Class Polar
{
double radius;
double angle;
public:
Polar()
{
radius=0;
angle=0;
}
Polar (double r, double a)
{
radius=r;
angle=a;
}
7
operator Cartesian()
{
double x=Radius*cos(angle);
double y=radius*sin(angle);
return Cartesian(x,y);
}
};