Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. OOPS PRESENTATION OPERATOR OVERLOADINGBy:-Aditi Chauhan (013)Akhilesh kr Jha (029)Arpan Rathi (056)Chandan Kumar (078)Vikas Verma (L.E.)
  2. 2. Overloading Operator  Operator overloading is a very neat feature of object oriented programming  Operator overloading allows it to give normal C++ operators such as +,-,==,< additional meanings  It makes statements more intuitive and readable for example: Date d1(12,3,1989); Date d2; d2.add_days(d1,45); // can be written with the + operator as
  3. 3. 3C++ Philosophy (continued) Operators retain their precedence and associativity, even when overloaded Operators retain their number of operands Cannot redefine operators on built-in types Not possible to define new operators  Only (a subset of) the built-in C++ operators can be overloaded Operator Overloading CS-2303, C-Term 2010
  4. 4. Defining Operator Overloading To define an additional task to an operator, we must specify what it means in relation to the class to which the operator is applied. This is done with the help of a special function called operator function.return type class-name : :operator op (arg-list){ Function body // task defined}
  5. 5. 5Operators that Can and Cannot beOverloaded Operators that can be overloaded + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete new[] delete[] Operators that cannot be overloaded . .* :: ?: Operator Overloading CS-2303, C-Term 2010
  6. 6. 6General FormatreturnType operator*(parameters);    any type keyword operator symbol Return type may be whatever the operator returns  Including a reference to the object of the operand Operator symbol may be any overloadable operator from the list. Operator Overloading CS-2303, C-Term 2010
  7. 7. Defining Operator Overloadingreturn type class-name : :operator op (arg-list){ Function body // task defined} return type is the type of value returned by the specified operation. op is the operator being overloaded. op is preceded by the keyword operator. operator op is the function name.
  8. 8. continue… Defining Operator OverloadingOperator Function must be either  member function (non-static) Or  friend function. The basic difference :  A friend function will have only one argument for unary operators and two for binary operators.  A member function has no arguments for unary operators and one argument for binary operators.  This is because the object used to invoke the member function is passed implicitly and therefore is available for the member function.  Arguments may be passed either by value or by reference.
  9. 9. Process of Operator OverloadingThe process of overloading involves the following steps:  Create a class that defines the data type that is to be used in the overloading operation.  Declare the operator function operator op( ) in the public part of the class. It may be either a member function or a friend function.  Define the operator function to implement the required operations.
  10. 10. Process of Operator OverloadingOverloaded operator functions can be invoked by expressions such as:For unary operators: op x or x opFor binary operators: x op yop x or x op would be interpreted as for a friend function: operator op (x) for a member function: x.operator op ( )x op y would be interpreted as for a friend function: operator op (x,y) for a member function: x.operator op (y)
  11. 11. Overloading Unary OperatorsConsider a unary minus operator:  It takes just one operand.  It changes the sign of an operand when applied to a basic data item.  The unary minus when applied to an object should change the sign of each of its data items.
  12. 12. Overloading Binary OperatorsAs a rule, in overloading binary operators,  the left-hand operand is used to invoke the operator function and  the right-hand operand is passed as an argument.
  13. 13. Overloading Binary Operatorsreturn complex((x+c.x), (y+c.y));The compiler invokes an appropriateconstructor, initializes an object with no name and returnsthe contents for copying into an object.Such an object is called a temporary object and goesout of space as soon as the contents are assigned toanother object.
  14. 14. Overloading Binary Operators Using Friends Friend function requires two arguments to be explicitly passes to it. Member function requires only one.friend complex operator+(complex, complex);complex operator+(complex a, complex b){ return complex((a.x + b.x),(a.y + b.y));}
  15. 15. Overloading Binary Operators Using Friends We can use a friend function with built-in type data as the left-hand operand and an object as the right-hand operand.
  16. 16. Rules For Overloading Operators Only existing operators can be overloaded. New operators cannot be created. The overloaded operator must have at least one operand that is of user-defined type. We cannot change the basic meaning of an operator. Overloaded operators follow the syntax rules of the original operators.
  17. 17. continue…Rules For Overloading Operators The following operators that cannot be overloaded: Size of Size of operator  . Membership operator  .* Pointer-to-member operator  :: Scope resolution operator  ?; Conditional operator
  18. 18. continue… Rules For Overloading Operators The following operators can be over loaded with the use of member functions and not by the use of friend functions:  Assignment operator =  Function call operator( )  Subscripting operator [ ]  Class member access operator -> Unary operators, overloaded by means of a member function, take no explicit arguments and return no explicit values, but, those overloaded by means of a friend function, take one reference argument.
  19. 19. continue…Rules For Overloading Operators Binary operators overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit arguments. When using binary operators overloaded through a member function, the left hand operand must be an object of the relevant class. Binary arithmetic operators such as +, -, * and / must explicitly return a value. They must not attempt to change their own arguments.
  20. 20. Overloading Unary Operatorsclass Date{ void operator++(); // prefix increment operator}void Date::operator++ (){ if (++day > days_in_month()) { day=1; if (++month > 12) month=1; year++; }}Date d1(31,12,1999);++d1; // results in 1.1.2000
  21. 21. Overloading Unary Operatorsclass Date{ Date operator++(); // prefix increment operator}Date Date::operator++ (){ if (++day > days_in_month()) { day=1; if (++month > 12) month=1; year++; } return *this; // self-reference to the object}Date d1(31,12,1999);Date d2=++d1; // results in 1.1.2000
  22. 22. Overloading Unary Operatorsclass Date{ Date operator++(int); // postfix increment operator notice int!!}Date Date::operator++ (int){ Date tmp=*this; if (++day > days_in_month()) { … } return tmp; // return copy of object}Date d1(31,12,1999);Date d2=d1++; // results in d2 = 31.12.1999, d1 = 1.1.2000
  23. 23. Overloading Binary Operatorsclass Date{ Date operator+(int days) const;};Date Date::operator+(int days) const;{ Date tmp=*this; // copy object for (int i=0; i < days; i++) tmp++; return tmp;}Date d1(1,4,1999);Date d2=d1+25; // results in 26.4.2000
  24. 24. Overloading Binary Operatorsclass Date{ Date& operator+=(int days); // must be reference as += modifies // the left hand argument};Date& Date::operator+=(int days) // return type reference to object{ for (int i=0; i < days; i++) *this++; return *this; // return reference to object}Date d1(1,4,1999);d1+=25; // results in 26.4.2000
  25. 25. Overloading Relational Operatorsclass Date{ bool operator==(Date d) { return (day==d.day) && (month=d.month) && (year==d.year); }; bool operator<(Date d) { if (year < d.year) return true; else if (year==d.year) && (month < d.month) return true; else return (month==d.month) && (day < d.day); };};
  26. 26. Overloading Binary Operatorsint Date::operator-(Date d) const{ int days=0; if (*this > d) while (*this != ++d) days++; else while (*this != --d) days--; return days;}Date d1(24,4,1988);Date d2(13,3,1998);int diff = d1-d2; // diff = 42
  27. 27. Non-Member Function Operator Operators can also be implemented as non-member functions instead of member functions However this approach is usually not recommended as member data is private to the operator Necessary for some operators in which the class object does not appear on the left hand side of the operator for example <<, >>
  28. 28. Non-Member Function Operator A binary operator can be defined either by a member function taking one argument or a non-member function taking two argumentsclass Date{ Date operator+(int n); //member function one argument}Date Date::operator+(int n){ … };Date operator+(Date d, int n) //non-member func two arguments{ Date temp=d; for (int i=0; i<n; i++) temp++; return temp;}
  29. 29. Non-Member Function Operator An unary operator can be defined either by a member function taking no argument or a non- member function taking one argumentclass Date{ Date operator++(); //member function postfix increment no argument}Date Date::operator++(){ … };Date operator++(Date& d) // non-member function one reference argument{ // more complicated as member data is private to non-member function int tmp_day = d.get_day(); int tmp_month = d.get_month(); d.set_day(tmp_day+1); …}
  30. 30. Overloading << Operator Overloading the << operator allows you to specify the way in which an object is displayed As the << operator expects a stream object as the left hand argument it must be overloaded as a non-member function :ostream& operator<<(ostream& os, Date d); It is possible to grant the non-member function access to the private data member of the class by declaring the function as a friend of the class.
  31. 31. Overloading << Operator#include <iostream>class Date{ friend ostream& operator<<(ostream& os, Date d); // this non-member function is a friend of class date …};ostream& operator<<(ostream& os, Date d){ os << d.day << ”.” << d.month << ”.” << d.year; //access private data as friend};Date d1(16,3,1998);cout << ”Today’s date is: ” << d1 << endl;
  32. 32. Thank You !!