Operator Overloading :-
Operator overloading means giving additional meaning to normal operators when they are applied to user defined data types. How will the compiler recognize this? The compiler identifies that it is an overloaded operator by looking at the data type of the operand (i.e. if it is a user defined data type then the compiler knows it is an overloaded operator; if it is a built-in data type then the compiler will do the normal operation).
You have to remember that objects are defined by you (or the programmer). You choose the data that comes under an object. The data type that you use will be in-built data types like integer or character but the object as a whole is user defined. Hence it is not possible to add two objects (even though they belong to the same class) using the + sign. The addition operator is designed to work only on in-built data types like integers, float etc. It cannot add two objects. But using operator overloading you could use the same + sign to
add two objects. Operator overloading is a form of polymorphism(for example, the + operator once overloaded in a class can be used in different ways depending on the operands. If the operands are objects then the overloaded + is used. If the operands are fundamental data types then the normal + operation is carried out).
Syntax:
return-data-type operator symbol-of-operator (parameters)
{
//body of the function
}
Operator overloading means giving additional meaning to normal operators when they are applied to user defined data types. How will the compiler recognize this? The compiler identifies that it is an overloaded operator by looking at the data type of the operand (i.e. if it is a user defined data type then the compiler knows it is an overloaded operator; if it is a built-in data type then the compiler will do the normal operation).
You have to remember that objects are defined by you (or the programmer). You choose the data that comes under an object. The data type that you use will be in-built data types like integer or character but the object as a whole is user defined. Hence it is not possible to add two objects (even though they belong to the same class) using the + sign. The addition operator is designed to work only on in-built data types like integers, float etc. It cannot add two objects. But using operator overloading you could use the same + sign to
add two objects. Operator overloading is a form of polymorphism(for example, the + operator once overloaded in a class can be used in different ways depending on the operands. If the operands are objects then the overloaded + is used. If the operands are fundamental data types then the normal + operation is carried out).
Syntax:
return-data-type operator symbol-of-operator (parameters)
{
//body of the function
}
0 comments:
Post a Comment