OPERATOR OVERLOADING
Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type.
Operators which cannot be overloaded:
1.Scope Resolution Operator
2.Conditional Operator
3. Class Access Operator
4. Size Of Operator
It can be done either by friend function or member function:
The difference is:
Member function takes One argument for binary operator and none for unary operator whereas Member takes two for binary and one for unary operators
Program Code:
using namespace std;
class integer
{
int no;
public:
void input()
{
cout<<"Enter Number";
cin>>no;
}
void display()
{
cout<<"Number is "<<no<<endl;
}
void operator -()
{
no=-no;
}
};
int main()
{
integer o;
o.input();
o.display();
-o; // unary minus is called
cout<<"After operator overloading\n";
o.display();
return 0;
}
Comments
Post a Comment