• The existing operators defined by C++ compiler such as +, -, +=, >, >> etc are designed to operate only on standard data types C/C++.
• For example, the operator + can be used to perform the addition operation on basic data types like integer, floating point etc
• In C++ , we can re-define the operator + to act on user defined data types – Objects of class. This new definition of operator does not change the existing role but gives additional task to the existing operator.
• Operator overloading provides a flexible option for the creation of new definition for most of the C++ operators.
• Operator overloading is the feature of C++ realizing the polymorphism.
• Most of C++ operators can be overloaded but following operator can not be overloaded
– Class member access operators (. , ->)
– Dereference Operator (*) in case of pointer.
– Scope resolution operator (:: )
– Size operator (sizeof)
– Conditional operator (? :)
• By overloading the semantic of an operator is extended but syntax can not be changed
• The number of operands, precedence, and associatively of the operator remain the same for overloaded operators.
• The concept of operator overloading can also be applied to data conversion.
• C++ offers automatic conversion of primitive data types.
• But the conversion of user defined data types requires some effort on the part of the programmer.
• Hence we see that operator overloading concepts are applied for extending capability of operators to operate on user defined data and also for data conversion.
Overloading Operators
• To overload any operator, it should be defined in the class for which it has to be overloaded.• The operator’s new definition to operate on the user defined data type is written in the class as an operator member function.
• The operator keyword is used to define the operator for overloading C++ operator.
• The syntax of overloading is:
retrunType operator operatorSymbol(argu_list)
{
//body of function.
}
• Overloading without any explicit arguments to an operator function is used for unary operator overloading.
• For binary operator overloading a single explicit argument is required in operator function.
• However, with friend functions, for operator overloading unary operators take one explicit argument and binary operators take two explicit arguments.
Unary Operator Overloading
• We can overload unary operator to act on an object in the same way as is applied to a basic type like int or float.• The syntax for overloading the unary operator is:
retrunType operator operatorSymbol()
{
//body of function.
}
• Let Counter is the user defined class then the following examples illustrate the overloading of unary operators:
Counter operator +( ); //Counter is user-defined type
Counter operator –( );
void operator ++( );
void opertor --();
• The process of operator overloading generally involves following steps.
– Declare a class whose objects are to be manipulated using operators.
– Declare the operator function, in public part of class.
– It can either normal member function or friend function.
– Define operator function within the body of a class or outside the class like other member functions
• By overloading we can also use ++ operator for decrement and -- operator for increment giving different semantics.
Example: Unary operator overloading
Pre/Post increment Example
Nameless temporary object
• A convenient way to return an object is to create a nameless temporary object in the return statement itself .• In the above program, modify class definition by
• Here statement return Sample(x++); creates a nameless object by passing an initialization value
Example: increase time by 1 second
Binary operator overloading
• Binary operators can be overloaded similar to unary operator but it takes one explicit argument.• The first argument for binary operator function is implicit argument and second argument to be passed explicitly.
• The exception is for friend function.
• The syntax for overloading the binary operator is:
retrun_type operator operator_symbol(argtype arg_var)
{
//body of function.
}
• The data members of first argument are accessed without dot operator and the members of second argument are accessed by argument object by dot operator inside the operator function
Prototypes –Binary operator
• The following examples illustrate the overloading of binary operators:Example: Member Function, friend function, operator function, friend operator function
Using Friend Function addPoint()
• Inside the class declare friend function as :friend Point addPoint(Point,Point);
• The definition of friend function addPoint () is done as:
Point addPoint(Point P1,Point P2)
{
Point sum;
sum.x=P1.x+P2.x;
sum.y=P1.y+P2.y;
ret
urn sum;
} • Call of friend function will be as below:
p3= addPoint(p1,p2);
Using operator Function Operator+()
• The definition of operator in class is done as:
Point Operator +(Point P2)
{
Point sum;
sum.x=x+P2.x;
sum.y=y+P2.y;
return sum;
}
• The operator + can be called as below:
p3= p1+p2;
• Inside the class declare friend function as :
friend Point operator+(Point,Point);
• The definition of friend function Operator () is done as:
Point operator+(Point P1,Point P2)
{
Point sum;
sum.x=P1.x+P2.x;
sum.y=P1.y+P2.y;
return sum;
}
• Call of friend function will be as below
p3= p1+p2;
Giving extra role to + operator
• Look at the following code for operator+()
Giving extra role to - operator
• Look at the following code for operator-()Giving extra role to + operator:- Concatenating string Objects
• In C++, the operator + can be used to concatenate two string objects.• For concatenation, we have to define the + operator as member function above for concatenating definition for string class.
• The original mining of + can not be altered for basic data type but we are giving additional meaning to this + defining it for concatenation.
Overloaded relational operator
• The relational operators (<, <= , > , >= , == , != ) can also be overloaded as other binary operators to extend their semantics.• Overloading relational operator we can directly compare the user defined objects’ value like as basic data type.
• For basic data type (like int,float,char), if a and b are two variables then we can compare the value of a and b directly as a>b, a• But for any two user defined objects obj1 and obj2 of a class, we cannot directly apply relational operator as obj1>obj2, obj1
• The syntax for definition of the operator function will be like:
• Boolean operator rel_op_symbol(classType obj)
• {
// definition
}
Example: Overloaded relational operator >
Overloading Stream : [insertion (<<) and extraction(>>)] operator
• One of the advantages of using the iostream objects is that we can customize them to support our own classes.• We can not directly apply the stream I/O operator for user defined object for read and write operation.
• For applying to our own class object to be R/W, these operators are to be overloaded
• We need to use friend operator function to overload these type of operators
• The syntax of overloading these operators is:
Note: ostream and istream are predefined C++ library classes for output and input stream, will discuss later in Chapter File I/O.
Output of above program
Enter currency c1:10 55
Enter currency c2:20 80
c1 is :Rs:10 Ps:55
c2 is :Rs:20 Ps:80
c3 is :Rs:41 Ps:52
0 Comments