What is operator overloading ? Operator overloading is the process of customizing C++ operators for operands of user defined types. When you have two objects of a class- num1 and num2 , you can write a function to add them such as ans = add(num1,num2); That does not look neither simple nor intuitive. You would prefer to write ans = num1+num2; as you would write expressions for basic data types like integers, floats etc. This can be done using Operator overloading. Operator overloading lets you write such statements. That is, it lets you call your functions on objects using +, - ,* etc. + operator will call addition function on the object (when you write op. overloading function for +). * will call multiply on objects etc. Names of overloaded operator functions start with keyword operator followed by symbol of the operator. e.g. +, - etc. Unary operator functions take 0 parameters for members. The operand for these function is the object cal
Comments
Post a Comment