Operator Overloading: Presented By: Er. Simarpreet Kaur Subject: Programming in C++
Operator overloading allows operators like + and << to be used with user-defined types by defining corresponding operator functions. It is a type of polymorphism. Common operators that can be overloaded include +, -, *, /, <<, and >>. There are some restrictions like preserving precedence and arity of operators. Overloaded operators can be class member, non-member, or friend functions. Examples demonstrate overloading + to add two time objects and overloading << and >> as non-member friend functions for input/output of user-defined types.
We take content rights seriously. If you suspect this is your content, claim it here.
0 ratings0% found this document useful (0 votes)
38 views10 pages
Operator Overloading: Presented By: Er. Simarpreet Kaur Subject: Programming in C++
Operator overloading allows operators like + and << to be used with user-defined types by defining corresponding operator functions. It is a type of polymorphism. Common operators that can be overloaded include +, -, *, /, <<, and >>. There are some restrictions like preserving precedence and arity of operators. Overloaded operators can be class member, non-member, or friend functions. Examples demonstrate overloading + to add two time objects and overloading << and >> as non-member friend functions for input/output of user-defined types.
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 10
OPERATOR
Presented by: Er. Simarpreet Kaur
OVERLOADING Subject: Programming in C++ Operator Overloading • Operator overloading is an important concept in C++. It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform operation on user-defined data type. For example '+' operator can be overloaded to perform addition on various data types, like for Integer, String(concatenation) etc. Operator that cannot be overloaded • scope operator - :: • sizeof • member selector - . • member pointer selector - * • ternary operator - ?: Operator overloading syntax Implementing operator overloading • Operator overloading can be done by implementing a function which can be : 1. Member Function 2. Non-Member Function 3. Friend Function • Operator overloading function can be a member function if the Left operand is an Object of that class, but if the Left operand is different, then Operator overloading function must be a non-member function. • Operator overloading function can be made friend function if it needs access to the private and protected members of class. Restriction on operator overloading • Following are some restrictions to be kept in mind while implementing operator overloading. • Precedence and Associativity of an operator cannot be changed. • Arity (numbers of Operands) cannot be changed. Unary operator remains unary, binary remains binary etc. • No new operators can be created, only existing operators can be overloaded. Example: overloading '+' Operator to add two time object Cont.. Cont.. Overloading I/O operator • Overloaded to perform input/output for user defined datatypes. • Left Operand will be of types ostream& and istream& • Function overloading this operator must be a Non-Member function because left operand is not an Object of the class. • It must be a friend function to access private data members.