{"id":96,"date":"2018-07-10T05:49:39","date_gmt":"2018-07-10T05:49:39","guid":{"rendered":"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=96"},"modified":"2019-05-14T11:51:16","modified_gmt":"2019-05-14T11:51:16","slug":"96","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/chapter\/96\/","title":{"rendered":"Operator Overloading"},"content":{"raw":"\r\n<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/biSkYhIcy2U\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n\r\nIntroduction\r\n\r\n&nbsp;\r\n\r\nConstructs like structure and class allow us to create our own data type. For example if we want to store date, we can create appropriate data type by creating class Time as follows:\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n#include&lt;iostream&gt;\r\n\r\n#include&lt;conio.h&gt;\r\n\r\n&nbsp;\r\n\r\nusing namespace std;\r\n\r\n&nbsp;\r\n\r\nclass Time\r\n\r\n{\r\n\r\nprivate:\r\n\r\n&nbsp;\r\n\r\nint second;\r\n\r\nint minute;\r\n\r\nint hour;\r\n\r\n&nbsp;\r\n\r\npublic:\r\n\r\n&nbsp;\r\n\r\nTime();\r\n\r\n&nbsp;\r\n\r\nTime(int h, int m,int s);\r\n\r\nvoid displayTime();\r\n\r\nvoid readTime();\r\n\r\n&nbsp;\r\n\r\nTime addTime(Time t);\r\n\r\n&nbsp;\r\n\r\nTime subtractTime(Time t );\r\n\r\n&nbsp;\r\n\r\n};\r\n\r\n&nbsp;\r\n\r\nThis class will allow us to store time in a single variable like\r\n\r\n&nbsp;\r\n\r\nTime now;\r\n\r\n&nbsp;\r\n\r\nWhich otherwise would have required 3 integer variables for the same such as int hrs, min sec;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">C++ by means of providing class construct empowers us to create our own data type, but the data type thus created is inferior to standard data type in many ways:<\/p>\r\n&nbsp;\r\n<ol>\r\n \t<li style=\"text-align: justify\">We cannot use operators provided by the language with the user defined data types to perform arithmetic, logical or other operations. For example the difference of two Time objects end_time and start_time cannot be computed using arithmetic operator (\u2013) minus. We need to do the same as follows<\/li>\r\n<\/ol>\r\n&nbsp;\r\n\r\ntime_diff = end_time.subtractTime(start_time) ;\r\n\r\n&nbsp;\r\n\r\nThe above statement achieves the goal but it is not as readable and clearas the following statement.\r\n\r\n&nbsp;\r\n\r\ntime_diff = end time \u2013 start_time ;\r\n\r\n&nbsp;\r\n\r\nThe above statement though very clear and readable, is not valid for the Time class that we have defined.\r\n\r\n&nbsp;\r\n<ol start=\"2\">\r\n \t<li style=\"text-align: justify\">We cannot read the objects of our class through cin and cout object as we read variable of standard data type. For example we read and write integer variable x as<\/li>\r\n<\/ol>\r\ncin\u00a0\u00a0\u00a0 &gt;&gt;\u00a0 x ;\r\n\r\ncout &lt;&lt;\u00a0 x ;\r\n\r\n&nbsp;\r\n\r\nBut we cannot read or write object of Time class as:\r\n\r\n&nbsp;\r\n\r\nTime now;\r\n\r\ncin\u00a0 &gt;&gt; now ;\r\n\r\ncout &lt;&lt; now ;\r\n\r\n&nbsp;\r\n\r\nFor reading and writing Time object we will write following statements\r\n\r\n&nbsp;\r\n\r\nTime now;\r\n\r\nnow.readTime() ;\r\n\r\nnow.displayTime() ;\r\n<ol start=\"3\">\r\n \t<li style=\"text-align: justify\">Implicit type conversion is not done for the user defined data types. For example we cannot assign an integer value to our Time object as C++ does not provide implicit conversion of integer to Time object<\/li>\r\n<\/ol>\r\n<div>\r\n\r\nTime current_time;\r\n\r\ncurrent_time = 10 ;\r\n\r\n\/\/ This will not work\r\n\r\n<\/div>\r\n&nbsp;\r\n<p style=\"text-align: justify\">We cannot be content with the fact that data type defined by us is inferior to standard data type. Well we do not need to be. The good news is that with little more work, it is possible to modify our class to compensate for the said deficiencies. For the first two shortcomings the desired can be achieved with the addition of appropriately overloaded operators to our class, which is the subject of this module and the next module. The third deficiency can be overcome by providing appropriate constructors and conversion functions for our class. That we will discuss in later module.<\/p>\r\n&nbsp;\r\n\r\nOperator Overloading\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Operator overloading means overloading existing operators with more tasks. For example operations such as subtraction of integers and subtraction of real numbers have already been defined for minus (\u2013) operator, we can define another task of subtracting two time objects for minus (\u2013) operator. This process of overloading operator with one more task is called operator overloading. We can overload unary as well as binary operators. Let us see how we can overload operators.<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Overloading Binary Operator<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">C++ allows us to overload an operator with the help of special function that we can add to our class. The syntax of function for overloading binary operator is given below<\/p>\r\n&nbsp;\r\n\r\nreturn_type operator operator_to_be_overloaded (class object)\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">For our Time class we can overload the minus (\u2013 ) operator with yet another job, that is of finding difference of two Time objects. We can overload minus (\u2013) operator for finding difference of two Time objects as follows:<\/p>\r\n&nbsp;\r\n\r\nTime Time :: operator- ( Time t )\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nTime time;\r\n\r\nint seconds_1 , seconds_2 , seconds_diff ;\r\n\r\n\/\/ convert time into second\r\n\r\nseconds_1 = (hour *3600) + (minute * 60) + second ; seconds_2 = (t.hour * 3600) + (t.minute * 60) + t.second ;\r\n\r\nseconds_diff = seconds_1 - seconds_2 ;\r\n\r\ntime.hour = seconds_diff \/ 3600 ;\r\n\r\nseconds_diff = seconds_diff % 3600 ;\r\n\r\ntime.minute = seconds_diff \/60 ;\r\n\r\nseconds_diff = seconds_diff % 60 ;\r\n\r\ntime.second = seconds_diff ;\r\n\r\n&nbsp;\r\n\r\nreturn time;\r\n\r\n}\r\n\r\nOnce the above function is added to our class the following statement would work fine time_taken = end_time \u2013 start_time;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Here time_taken, end_time and start_time are objects of Time class. Above statement would result in invoking overloaded minus (\u2013) operator function through end_time object, and start_time object will be passed as argument. The object containing the difference of time will be returned by the overloaded function, which will be stored in the variable time_taken.<\/p>\r\n&nbsp;\r\n\r\nOperators can also be overloaded for adding objects of other data type with the object of our class. For example minus (\u2013) operator can also be overloaded to make the following statement work.\r\n\r\n&nbsp;\r\n\r\nTime_difference = current_time \u2013 5;\r\n\r\nTo make this work, we need to overload minus (\u2013) operator as follows\r\n\r\nTime Time :: operator\u2013 ( int h )\r\n\r\n{\r\n<table class=\"aligncenter\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 426.063px\">Time diff;<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 426.063px\">diff.hours<\/td>\r\n<td style=\"width: 226.063px\">= hours \u2013 h;<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 426.063px\">diff.minutes = minutes;<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 426.063px\">diff.seconds<\/td>\r\n<td style=\"width: 226.063px\">= seconds;<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 426.063px\">return diff;<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 426.063px\">}<\/td>\r\n<td style=\"width: 226.063px\"><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nLet us see few more binary operators that can be overloaded for our Time class\r\n\r\n&nbsp;\r\n\r\nOverloading Relational Operator\r\n\r\nOverloading == operator\r\n\r\nint Time::operator==(Time t)\r\n\r\n{\r\n\r\nif(hour==t.hour &amp;&amp; minute==t.minute &amp;&amp; seconds==t.seconds)\r\n\r\nreturn 1;\r\n\r\nelse\r\n\r\nreturn 0;\r\n\r\n}\r\n\r\nOverloading &gt; operator\r\n\r\n&nbsp;\r\n\r\nint Time :: operator &gt;( Time t )\r\n\r\n{\r\n\r\nif( hour &gt; t.hour )\r\n\r\nreturn 1;\r\n\r\nelse I f( hour == t.hour)\r\n\r\n{\r\n\r\nIf (minute &gt; t.minute )\r\n\r\nreturn 1;\r\n\r\nelse I f( minute == t.minute )\r\n\r\n{\r\n\r\nIf (seconds&gt;t.seconds)\r\n\r\nreturn 1;\r\n\r\nelse\r\n\r\nreturn 0;\r\n\r\n}\r\n\r\nelse\r\n\r\nreturn 0;\r\n\r\n}\r\n\r\nelse\r\n\r\nreturn 0;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nWe can add above two functions to our Time class to have following code work for us\r\n\r\nTime t1 , t2 ;\r\n\r\nIf ( t1 &gt; t2 )\r\n\r\nCout &lt;&lt; \"Time T1 is Greater\";\r\n\r\nelse if ( t1 == t2)\r\n\r\ncout &lt;&lt; \"Both time are equal\" ;\r\n\r\nelse\r\n\r\ncout &lt;&lt; \"Time T2 is greater\" ;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Here overloaded &gt; and == operator functions will be invoked on object t1 and object t2 will be passed as argument.<\/p>\r\n\r\n<div>\r\n\r\nOverloading shorthand operators\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We can also overload shorthand operators. Following function will overload shorthand operator (-=).<\/p>\r\n&nbsp;\r\n\r\nvoid Time :: operator-= ( Time t )\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nTime time;\r\n\r\n&nbsp;\r\n\r\nint seconds_1 , seconds_2 , seconds_diff ;\r\n\r\nseconds_1 = (hour *3600) + (minute * 60) + seconds ; seconds_2 = (t.hour * 3600) + (t.minute * 60) + t.seconds ;\r\n\r\nseconds_diff = seconds_1 - seconds_2 ;\r\n\r\nhour = seconds_diff \/ 3600 ;\r\n\r\nseconds_diff = seconds_diff % 3600 ;\r\n\r\nminute = seconds_diff \/60 ;\r\n\r\nseconds_diff = seconds_diff % 60 ;\r\n\r\nseconds = seconds_diff ;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nWe can add above function to our Time class to have following code work for us\r\n\r\n&nbsp;\r\n\r\nTime t1, t2;\r\n\r\n&nbsp;\r\n\r\nt1 -=\u00a0 t2;\r\n\r\n&nbsp;\r\n\r\nHere overloaded (-=) operator function will be invoked on object t1 and object t2 will be passed as argument. Function will update t1 object with computed difference of Time objects t1 and t2.\r\n\r\n&nbsp;\r\n\r\n<strong>Overloading array subscript operator []<\/strong>\r\n\r\n&nbsp;\r\n\r\nIt is interesting to see how operators like comma operator, function call operator (), array subscript operator [] can be meaningfully overloaded for user defined data types. Let us see how array subscript operator can be meaning fully overloaded for our Time class.\r\n\r\n&nbsp;\r\n\r\nint Time :: operator []( char unit )\r\n\r\n{\r\n\r\nswitch(unit)\r\n\r\n{\r\n\r\n<\/div>\r\n<div>\r\n\r\ncase 'h' :\r\n\r\ncase 'm' :\r\n\r\ncase 's'\u00a0 :\r\n\r\nreturn hour;\r\n\r\nreturn minute;\r\n\r\nreturn seconds;\r\n\r\n<\/div>\r\n<div>\r\n\r\n}\r\n\r\n<\/div>\r\n}\r\n<p style=\"text-align: justify\">With the above function added to our class, we can get the hours, minutes and seconds of Time class objects printed as follows<\/p>\r\n&nbsp;\r\n\r\ncout&lt;&lt;\"\\n T1 Second is =\"&lt;&lt;t1['s'];\r\n\r\n&nbsp;\r\n\r\ncout&lt;&lt;\"\\n T1 Minute is =\"&lt;&lt;t1['m'];\r\n\r\ncout&lt;&lt;\"\\n T1 Hour is =\"&lt;&lt;t1['h'];\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Here object t1 will invoke the function and character \u2018h\u2019\/\u2019 m\u2019\/\u2019s\u2019 will be passed as argument. The function will return hours, minutes or seconds stored in t1 depending on the character passed.<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Operator Functions<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We can also overload function call operator () for any class. Once we overload this operator, use of operator () with the object of the class will look like a function call. For example we can overload function call () operator on Matrix class to get the value stored on a specific row and column as follows<\/p>\r\n&nbsp;\r\n\r\nclass Matrix\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nint mat[20][20];\r\n\r\nint row, column;\r\n\r\npublic :\r\n\r\n\u2026\u2026\u2026\u2026..\r\n\r\n&nbsp;\r\n\r\n\u2026\u2026\u2026\u2026\r\n\r\nint operator() ( int r, int c);\r\n\r\n{\r\n\r\nreturn\u00a0 mat[r][c];\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n\u2026\u2026\u2026\u2026\u2026..\r\n\r\n&nbsp;\r\n\r\n\u2026\u2026\u2026\u2026\u2026..\r\n\r\n};\r\n\r\n&nbsp;\r\n\r\nWith the overloaded operator () for class Matrix, we can get the value of any matrix element as follows:\r\n<table class=\"aligncenter\" style=\"height: 126px\" border=\"1\" width=\"679\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 132.063px\">Matrix m;<\/td>\r\n<td style=\"width: 520.063px\">\/\/ element stored on 4th row and 5th column of matrix m<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 132.063px\">cout &lt;&lt; m(4,5);<\/td>\r\n<\/tr>\r\n<tr>\r\n<td style=\"width: 132.063px\"><\/td>\r\n<td style=\"width: 520.063px\">will be printed<\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Such objects are known as function objects. Here overloaded operator function will be invoked on object m and row and column will be passed as argument.<\/p>\r\n&nbsp;\r\n\r\n<strong>Overloading Unary Operator<\/strong>\r\n\r\n&nbsp;\r\n\r\nWe can also overload unary operators such as (++) and (--). The syntax of function for overloading unaryoperator is given below\r\n\r\nreturn_type operator operor_to_be_overloaded ();\r\n\r\nFor example operator function for overloading unary operator ++, when overloaded as member function will have following prototype:\r\n\r\nTime operator ++ ( );\r\n\r\nIt does not require any argument as unary operator has only one operand, which will be the invoking object, hence will be passed implicitly to the function.\r\n\r\n&nbsp;\r\n\r\n<strong>Overloading Increment and decrement operators<\/strong>\r\n\r\n&nbsp;\r\n\r\nC++ provides increment and decrement operator in two flavors - Postfix version &amp; Prefix version\r\n\r\n&nbsp;\r\n\r\nTime now;\r\n\r\n&nbsp;\r\n\r\nnow++;\r\n\r\n&nbsp;\r\n\r\n++now;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">If we want both the versions of increment operator (++) to be overloaded, the prototype for both the operator functions will turn out to be identical. It will result into ambiguous situation for the compiler. To resolve this, C++ compiler allows us to define different prototypes for both the versions. To overload prefix version, prototype of operator function will be as follows:<\/p>\r\n&nbsp;\r\n\r\nTime operator++ ( );\r\n\r\n&nbsp;\r\n\r\nand to overload postfix version of increment operator (++) function prototype will be as follows:\r\n\r\n&nbsp;\r\n\r\nTime operator++ (int x);\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Here x is a dummy argument. It has no role in the function and has been defined only to distinguish between infix and postfix versions of (++) operator.<\/p>\r\n\r\n<div>\r\n\r\nFollowing is the example of prefix and postfix increment (++) operator overloading:\r\n\r\n&nbsp;\r\n\r\n\/\/prefix version\r\n\r\nvoid Time :: operator ++( )\r\n\r\n{\r\n\r\n\/\/increment seconds\r\n\r\n&nbsp;\r\n\r\nseconds++ ;\r\n\r\n&nbsp;\r\n\r\nif(seconds==60)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nseconds=0;\r\n\r\nminute++;\r\n\r\n&nbsp;\r\n\r\nif(minute==60)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nminute=0;\r\n\r\nhour++;\r\n\r\n}\r\n\r\n}\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n\/\/postfix version\r\n\r\nvoid Time :: operator ++( int x )\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n\/\/increment seconds\r\n\r\nseconds++ ;\r\n\r\n&nbsp;\r\n\r\nif(seconds==60)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nseconds=0;\r\n\r\n&nbsp;\r\n\r\nminute++;\r\n\r\n&nbsp;\r\n\r\nif(minute==60)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\nminute=0;\r\n\r\nhour++;\r\n\r\n}\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nBoth the versions for decrement operator (--) can also be overloaded in the similar fashion.\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>Operators that cannot be overloaded<\/strong>\r\n\r\n&nbsp;\r\n\r\nBarring few operators almost all operators can be overloaded. List of operators which cannot be overloaded is given below:\r\n\r\n1.\u00a0\u00a0\u00a0\u00a0\u00a0 Dot operator for member access (.)\r\n\r\n2.\u00a0\u00a0\u00a0\u00a0\u00a0 Dereference member to class operator (.*)\r\n\r\n3.\u00a0\u00a0\u00a0\u00a0\u00a0 Scope resolution operator (::)\r\n\r\n4.\u00a0\u00a0\u00a0\u00a0\u00a0 Size of operator (sizeof)\r\n\r\n5.\u00a0\u00a0\u00a0\u00a0\u00a0 Conditional ternary operator (?:)\r\n\r\n6.\u00a0\u00a0\u00a0\u00a0\u00a0 Casting operator (static_cast&lt;&gt;, dynamic_cast&lt;&gt;, reinterpret_cast&lt;&gt;, and const_cast&lt;&gt;)\r\n\r\n7.\u00a0\u00a0\u00a0\u00a0\u00a0 # and ## tokens for macro preprocessors\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">One may ask why these operators cannot be overloaded. Consider overloading operator(.), which will give new meaning to member access function. This may result in creating ambiguous scenario for compiler \u2013 which can either result into serious errors or make compiler\u2019s job very complex.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Operator overloading as such is a complex problem to be managed by compiler, because of which perhaps many object oriented programming languages including JAVA do not provide operator overloading. Therefore this much liberty of not allowing few operators to be overloaded by C++ compiler perhaps is justified.<\/p>\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">By means of operator overloading we can use operators provided by the language, with the user defined data types to perform arithmetic, logical or other operations. Operator overloading is not essential feature for any object oriented programming language. As discussed before many object oriented programming languages including JAVA do not provide operator overloading. We can do without operator overloading but operator overloading provides significant advantages in terms of making code easy to read and manage (a very important quality factor for any program). It reduces the burden of programmer by eliminating the need to remember the function name and as name of function is not required; chances of spelling mistake is also not there. Also in context of C++ , overloading operators such as assignment operator = appropriately, resolve issues arising from scoping rules in certain cases. We will discuss this in detail in later module.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">However operator overloading will yield full advantage when the new definition added to the operator is intuitive resulting into simple and easy to understand code.<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Following is the complete C++ program for overloading operators discussed above.<\/p>\r\n<img class=\"size-full wp-image-97 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-29.png\" alt=\"\" width=\"578\" height=\"721\" \/>\r\n\r\n<img class=\"size-full wp-image-98 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-30.png\" alt=\"\" width=\"765\" height=\"839\" \/>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-99 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-31.png\" alt=\"\" width=\"879\" height=\"830\" \/>\r\n\r\n<img class=\"size-full wp-image-100 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-32.png\" alt=\"\" width=\"856\" height=\"836\" \/>\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-101 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-33.png\" alt=\"\" width=\"895\" height=\"721\" \/>\r\n\r\n&nbsp;\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Operator Overloading<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/biSkYhIcy2U\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n\r\n<strong>Additional References<\/strong>\r\n\r\n&nbsp;\r\n\r\n1) Stanley Lippmann, \u201cC++ Primer\u201d, Pearson Education.\r\n\r\n2) Bjarne Stroustrup, \u201cThe C++ Programming Language\u201d, Pearson Education.\r\n\r\n3) Scott Mayer, \u201cEffective C++\u201d, Addison Wesley.\r\n\r\n4) Bhushan Trivedi , Programming with ANSI C++, 2\/e , Oxford University Press\r\n\r\n5) Yashavant P. Kanetkar \u201cLet Us C++\u201d , Bpb Publications.\r\n\r\n6) Abhiram G. Ranade, \u201cAn Introduction to Programming through C++\u201d , McGraw Hill\r\n\r\n7) Ellis and B. Stroustrup \u201cAnnotated C++ Reference Manual\u201d, http:\/\/www.stroustrup.com\/arm.html\r\n\r\n8) Herbert Schildt, \u201cComplete Reference C++\u201d, McGraw Hill Publications.\r\n\r\n9) Ashok Kamthane, \u201cObject Oriented Programming with ANSI and Turbo C++\u201d, Pearson Education\r\n\r\n10) E Balaguruswami, \u201cObject Oriented Programming With C++\u201d, Tata McGraw Hill\r\n\r\n11) \u201cC++ FAQs\u201d, Pearson Education.","rendered":"<div><span style=\"float: right;\"><a href=\"https:\/\/youtu.be\/biSkYhIcy2U\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<p>Introduction<\/p>\n<p>&nbsp;<\/p>\n<p>Constructs like structure and class allow us to create our own data type. For example if we want to store date, we can create appropriate data type by creating class Time as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>#include&lt;iostream&gt;<\/p>\n<p>#include&lt;conio.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>using namespace std;<\/p>\n<p>&nbsp;<\/p>\n<p>class Time<\/p>\n<p>{<\/p>\n<p>private:<\/p>\n<p>&nbsp;<\/p>\n<p>int second;<\/p>\n<p>int minute;<\/p>\n<p>int hour;<\/p>\n<p>&nbsp;<\/p>\n<p>public:<\/p>\n<p>&nbsp;<\/p>\n<p>Time();<\/p>\n<p>&nbsp;<\/p>\n<p>Time(int h, int m,int s);<\/p>\n<p>void displayTime();<\/p>\n<p>void readTime();<\/p>\n<p>&nbsp;<\/p>\n<p>Time addTime(Time t);<\/p>\n<p>&nbsp;<\/p>\n<p>Time subtractTime(Time t );<\/p>\n<p>&nbsp;<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>This class will allow us to store time in a single variable like<\/p>\n<p>&nbsp;<\/p>\n<p>Time now;<\/p>\n<p>&nbsp;<\/p>\n<p>Which otherwise would have required 3 integer variables for the same such as int hrs, min sec;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">C++ by means of providing class construct empowers us to create our own data type, but the data type thus created is inferior to standard data type in many ways:<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"text-align: justify\">We cannot use operators provided by the language with the user defined data types to perform arithmetic, logical or other operations. For example the difference of two Time objects end_time and start_time cannot be computed using arithmetic operator (\u2013) minus. We need to do the same as follows<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>time_diff = end_time.subtractTime(start_time) ;<\/p>\n<p>&nbsp;<\/p>\n<p>The above statement achieves the goal but it is not as readable and clearas the following statement.<\/p>\n<p>&nbsp;<\/p>\n<p>time_diff = end time \u2013 start_time ;<\/p>\n<p>&nbsp;<\/p>\n<p>The above statement though very clear and readable, is not valid for the Time class that we have defined.<\/p>\n<p>&nbsp;<\/p>\n<ol start=\"2\">\n<li style=\"text-align: justify\">We cannot read the objects of our class through cin and cout object as we read variable of standard data type. For example we read and write integer variable x as<\/li>\n<\/ol>\n<p>cin\u00a0\u00a0\u00a0 &gt;&gt;\u00a0 x ;<\/p>\n<p>cout &lt;&lt;\u00a0 x ;<\/p>\n<p>&nbsp;<\/p>\n<p>But we cannot read or write object of Time class as:<\/p>\n<p>&nbsp;<\/p>\n<p>Time now;<\/p>\n<p>cin\u00a0 &gt;&gt; now ;<\/p>\n<p>cout &lt;&lt; now ;<\/p>\n<p>&nbsp;<\/p>\n<p>For reading and writing Time object we will write following statements<\/p>\n<p>&nbsp;<\/p>\n<p>Time now;<\/p>\n<p>now.readTime() ;<\/p>\n<p>now.displayTime() ;<\/p>\n<ol start=\"3\">\n<li style=\"text-align: justify\">Implicit type conversion is not done for the user defined data types. For example we cannot assign an integer value to our Time object as C++ does not provide implicit conversion of integer to Time object<\/li>\n<\/ol>\n<div>\n<p>Time current_time;<\/p>\n<p>current_time = 10 ;<\/p>\n<p>\/\/ This will not work<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We cannot be content with the fact that data type defined by us is inferior to standard data type. Well we do not need to be. The good news is that with little more work, it is possible to modify our class to compensate for the said deficiencies. For the first two shortcomings the desired can be achieved with the addition of appropriately overloaded operators to our class, which is the subject of this module and the next module. The third deficiency can be overcome by providing appropriate constructors and conversion functions for our class. That we will discuss in later module.<\/p>\n<p>&nbsp;<\/p>\n<p>Operator Overloading<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Operator overloading means overloading existing operators with more tasks. For example operations such as subtraction of integers and subtraction of real numbers have already been defined for minus (\u2013) operator, we can define another task of subtracting two time objects for minus (\u2013) operator. This process of overloading operator with one more task is called operator overloading. We can overload unary as well as binary operators. Let us see how we can overload operators.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Overloading Binary Operator<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">C++ allows us to overload an operator with the help of special function that we can add to our class. The syntax of function for overloading binary operator is given below<\/p>\n<p>&nbsp;<\/p>\n<p>return_type operator operator_to_be_overloaded (class object)<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For our Time class we can overload the minus (\u2013 ) operator with yet another job, that is of finding difference of two Time objects. We can overload minus (\u2013) operator for finding difference of two Time objects as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>Time Time :: operator- ( Time t )<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>Time time;<\/p>\n<p>int seconds_1 , seconds_2 , seconds_diff ;<\/p>\n<p>\/\/ convert time into second<\/p>\n<p>seconds_1 = (hour *3600) + (minute * 60) + second ; seconds_2 = (t.hour * 3600) + (t.minute * 60) + t.second ;<\/p>\n<p>seconds_diff = seconds_1 &#8211; seconds_2 ;<\/p>\n<p>time.hour = seconds_diff \/ 3600 ;<\/p>\n<p>seconds_diff = seconds_diff % 3600 ;<\/p>\n<p>time.minute = seconds_diff \/60 ;<\/p>\n<p>seconds_diff = seconds_diff % 60 ;<\/p>\n<p>time.second = seconds_diff ;<\/p>\n<p>&nbsp;<\/p>\n<p>return time;<\/p>\n<p>}<\/p>\n<p>Once the above function is added to our class the following statement would work fine time_taken = end_time \u2013 start_time;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Here time_taken, end_time and start_time are objects of Time class. Above statement would result in invoking overloaded minus (\u2013) operator function through end_time object, and start_time object will be passed as argument. The object containing the difference of time will be returned by the overloaded function, which will be stored in the variable time_taken.<\/p>\n<p>&nbsp;<\/p>\n<p>Operators can also be overloaded for adding objects of other data type with the object of our class. For example minus (\u2013) operator can also be overloaded to make the following statement work.<\/p>\n<p>&nbsp;<\/p>\n<p>Time_difference = current_time \u2013 5;<\/p>\n<p>To make this work, we need to overload minus (\u2013) operator as follows<\/p>\n<p>Time Time :: operator\u2013 ( int h )<\/p>\n<p>{<\/p>\n<table class=\"aligncenter\">\n<tbody>\n<tr>\n<td style=\"width: 426.063px\">Time diff;<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 426.063px\">diff.hours<\/td>\n<td style=\"width: 226.063px\">= hours \u2013 h;<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 426.063px\">diff.minutes = minutes;<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 426.063px\">diff.seconds<\/td>\n<td style=\"width: 226.063px\">= seconds;<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 426.063px\">return diff;<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<\/tr>\n<tr>\n<td style=\"width: 426.063px\">}<\/td>\n<td style=\"width: 226.063px\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Let us see few more binary operators that can be overloaded for our Time class<\/p>\n<p>&nbsp;<\/p>\n<p>Overloading Relational Operator<\/p>\n<p>Overloading == operator<\/p>\n<p>int Time::operator==(Time t)<\/p>\n<p>{<\/p>\n<p>if(hour==t.hour &amp;&amp; minute==t.minute &amp;&amp; seconds==t.seconds)<\/p>\n<p>return 1;<\/p>\n<p>else<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<p>Overloading &gt; operator<\/p>\n<p>&nbsp;<\/p>\n<p>int Time :: operator &gt;( Time t )<\/p>\n<p>{<\/p>\n<p>if( hour &gt; t.hour )<\/p>\n<p>return 1;<\/p>\n<p>else I f( hour == t.hour)<\/p>\n<p>{<\/p>\n<p>If (minute &gt; t.minute )<\/p>\n<p>return 1;<\/p>\n<p>else I f( minute == t.minute )<\/p>\n<p>{<\/p>\n<p>If (seconds&gt;t.seconds)<\/p>\n<p>return 1;<\/p>\n<p>else<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<p>else<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<p>else<\/p>\n<p>return 0;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>We can add above two functions to our Time class to have following code work for us<\/p>\n<p>Time t1 , t2 ;<\/p>\n<p>If ( t1 &gt; t2 )<\/p>\n<p>Cout &lt;&lt; &#8220;Time T1 is Greater&#8221;;<\/p>\n<p>else if ( t1 == t2)<\/p>\n<p>cout &lt;&lt; &#8220;Both time are equal&#8221; ;<\/p>\n<p>else<\/p>\n<p>cout &lt;&lt; &#8220;Time T2 is greater&#8221; ;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Here overloaded &gt; and == operator functions will be invoked on object t1 and object t2 will be passed as argument.<\/p>\n<div>\n<p>Overloading shorthand operators<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We can also overload shorthand operators. Following function will overload shorthand operator (-=).<\/p>\n<p>&nbsp;<\/p>\n<p>void Time :: operator-= ( Time t )<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>Time time;<\/p>\n<p>&nbsp;<\/p>\n<p>int seconds_1 , seconds_2 , seconds_diff ;<\/p>\n<p>seconds_1 = (hour *3600) + (minute * 60) + seconds ; seconds_2 = (t.hour * 3600) + (t.minute * 60) + t.seconds ;<\/p>\n<p>seconds_diff = seconds_1 &#8211; seconds_2 ;<\/p>\n<p>hour = seconds_diff \/ 3600 ;<\/p>\n<p>seconds_diff = seconds_diff % 3600 ;<\/p>\n<p>minute = seconds_diff \/60 ;<\/p>\n<p>seconds_diff = seconds_diff % 60 ;<\/p>\n<p>seconds = seconds_diff ;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>We can add above function to our Time class to have following code work for us<\/p>\n<p>&nbsp;<\/p>\n<p>Time t1, t2;<\/p>\n<p>&nbsp;<\/p>\n<p>t1 -=\u00a0 t2;<\/p>\n<p>&nbsp;<\/p>\n<p>Here overloaded (-=) operator function will be invoked on object t1 and object t2 will be passed as argument. Function will update t1 object with computed difference of Time objects t1 and t2.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Overloading array subscript operator []<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>It is interesting to see how operators like comma operator, function call operator (), array subscript operator [] can be meaningfully overloaded for user defined data types. Let us see how array subscript operator can be meaning fully overloaded for our Time class.<\/p>\n<p>&nbsp;<\/p>\n<p>int Time :: operator []( char unit )<\/p>\n<p>{<\/p>\n<p>switch(unit)<\/p>\n<p>{<\/p>\n<\/div>\n<div>\n<p>case &#8216;h&#8217; :<\/p>\n<p>case &#8216;m&#8217; :<\/p>\n<p>case &#8216;s&#8217;\u00a0 :<\/p>\n<p>return hour;<\/p>\n<p>return minute;<\/p>\n<p>return seconds;<\/p>\n<\/div>\n<div>\n<p>}<\/p>\n<\/div>\n<p>}<\/p>\n<p style=\"text-align: justify\">With the above function added to our class, we can get the hours, minutes and seconds of Time class objects printed as follows<\/p>\n<p>&nbsp;<\/p>\n<p>cout&lt;&lt;&#8220;\\n T1 Second is =&#8221;&lt;&lt;t1[&#8216;s&#8217;];<\/p>\n<p>&nbsp;<\/p>\n<p>cout&lt;&lt;&#8220;\\n T1 Minute is =&#8221;&lt;&lt;t1[&#8216;m&#8217;];<\/p>\n<p>cout&lt;&lt;&#8220;\\n T1 Hour is =&#8221;&lt;&lt;t1[&#8216;h&#8217;];<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Here object t1 will invoke the function and character \u2018h\u2019\/\u2019 m\u2019\/\u2019s\u2019 will be passed as argument. The function will return hours, minutes or seconds stored in t1 depending on the character passed.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Operator Functions<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We can also overload function call operator () for any class. Once we overload this operator, use of operator () with the object of the class will look like a function call. For example we can overload function call () operator on Matrix class to get the value stored on a specific row and column as follows<\/p>\n<p>&nbsp;<\/p>\n<p>class Matrix<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>int mat[20][20];<\/p>\n<p>int row, column;<\/p>\n<p>public :<\/p>\n<p>\u2026\u2026\u2026\u2026..<\/p>\n<p>&nbsp;<\/p>\n<p>\u2026\u2026\u2026\u2026<\/p>\n<p>int operator() ( int r, int c);<\/p>\n<p>{<\/p>\n<p>return\u00a0 mat[r][c];<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026..<\/p>\n<p>&nbsp;<\/p>\n<p>\u2026\u2026\u2026\u2026\u2026..<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>With the overloaded operator () for class Matrix, we can get the value of any matrix element as follows:<\/p>\n<table class=\"aligncenter\" style=\"height: 126px; width: 679px;\">\n<tbody>\n<tr>\n<td style=\"width: 132.063px\">Matrix m;<\/td>\n<td style=\"width: 520.063px\">\/\/ element stored on 4th row and 5th column of matrix m<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 132.063px\">cout &lt;&lt; m(4,5);<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 132.063px\"><\/td>\n<td style=\"width: 520.063px\">will be printed<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Such objects are known as function objects. Here overloaded operator function will be invoked on object m and row and column will be passed as argument.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Overloading Unary Operator<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>We can also overload unary operators such as (++) and (&#8211;). The syntax of function for overloading unaryoperator is given below<\/p>\n<p>return_type operator operor_to_be_overloaded ();<\/p>\n<p>For example operator function for overloading unary operator ++, when overloaded as member function will have following prototype:<\/p>\n<p>Time operator ++ ( );<\/p>\n<p>It does not require any argument as unary operator has only one operand, which will be the invoking object, hence will be passed implicitly to the function.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Overloading Increment and decrement operators<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>C++ provides increment and decrement operator in two flavors &#8211; Postfix version &amp; Prefix version<\/p>\n<p>&nbsp;<\/p>\n<p>Time now;<\/p>\n<p>&nbsp;<\/p>\n<p>now++;<\/p>\n<p>&nbsp;<\/p>\n<p>++now;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">If we want both the versions of increment operator (++) to be overloaded, the prototype for both the operator functions will turn out to be identical. It will result into ambiguous situation for the compiler. To resolve this, C++ compiler allows us to define different prototypes for both the versions. To overload prefix version, prototype of operator function will be as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>Time operator++ ( );<\/p>\n<p>&nbsp;<\/p>\n<p>and to overload postfix version of increment operator (++) function prototype will be as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>Time operator++ (int x);<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Here x is a dummy argument. It has no role in the function and has been defined only to distinguish between infix and postfix versions of (++) operator.<\/p>\n<div>\n<p>Following is the example of prefix and postfix increment (++) operator overloading:<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/prefix version<\/p>\n<p>void Time :: operator ++( )<\/p>\n<p>{<\/p>\n<p>\/\/increment seconds<\/p>\n<p>&nbsp;<\/p>\n<p>seconds++ ;<\/p>\n<p>&nbsp;<\/p>\n<p>if(seconds==60)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>seconds=0;<\/p>\n<p>minute++;<\/p>\n<p>&nbsp;<\/p>\n<p>if(minute==60)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>minute=0;<\/p>\n<p>hour++;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/postfix version<\/p>\n<p>void Time :: operator ++( int x )<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>\/\/increment seconds<\/p>\n<p>seconds++ ;<\/p>\n<p>&nbsp;<\/p>\n<p>if(seconds==60)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>seconds=0;<\/p>\n<p>&nbsp;<\/p>\n<p>minute++;<\/p>\n<p>&nbsp;<\/p>\n<p>if(minute==60)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>minute=0;<\/p>\n<p>hour++;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Both the versions for decrement operator (&#8211;) can also be overloaded in the similar fashion.<\/p>\n<\/div>\n<div>\n<p><strong>Operators that cannot be overloaded<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Barring few operators almost all operators can be overloaded. List of operators which cannot be overloaded is given below:<\/p>\n<p>1.\u00a0\u00a0\u00a0\u00a0\u00a0 Dot operator for member access (.)<\/p>\n<p>2.\u00a0\u00a0\u00a0\u00a0\u00a0 Dereference member to class operator (.*)<\/p>\n<p>3.\u00a0\u00a0\u00a0\u00a0\u00a0 Scope resolution operator (::)<\/p>\n<p>4.\u00a0\u00a0\u00a0\u00a0\u00a0 Size of operator (sizeof)<\/p>\n<p>5.\u00a0\u00a0\u00a0\u00a0\u00a0 Conditional ternary operator (?:)<\/p>\n<p>6.\u00a0\u00a0\u00a0\u00a0\u00a0 Casting operator (static_cast&lt;&gt;, dynamic_cast&lt;&gt;, reinterpret_cast&lt;&gt;, and const_cast&lt;&gt;)<\/p>\n<p>7.\u00a0\u00a0\u00a0\u00a0\u00a0 # and ## tokens for macro preprocessors<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">One may ask why these operators cannot be overloaded. Consider overloading operator(.), which will give new meaning to member access function. This may result in creating ambiguous scenario for compiler \u2013 which can either result into serious errors or make compiler\u2019s job very complex.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Operator overloading as such is a complex problem to be managed by compiler, because of which perhaps many object oriented programming languages including JAVA do not provide operator overloading. Therefore this much liberty of not allowing few operators to be overloaded by C++ compiler perhaps is justified.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">By means of operator overloading we can use operators provided by the language, with the user defined data types to perform arithmetic, logical or other operations. Operator overloading is not essential feature for any object oriented programming language. As discussed before many object oriented programming languages including JAVA do not provide operator overloading. We can do without operator overloading but operator overloading provides significant advantages in terms of making code easy to read and manage (a very important quality factor for any program). It reduces the burden of programmer by eliminating the need to remember the function name and as name of function is not required; chances of spelling mistake is also not there. Also in context of C++ , overloading operators such as assignment operator = appropriately, resolve issues arising from scoping rules in certain cases. We will discuss this in detail in later module.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">However operator overloading will yield full advantage when the new definition added to the operator is intuitive resulting into simple and easy to understand code.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Following is the complete C++ program for overloading operators discussed above.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-97 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-29.png\" alt=\"\" width=\"578\" height=\"721\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-29.png 578w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-29-240x300.png 240w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-29-65x81.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-29-225x281.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-29-350x437.png 350w\" sizes=\"auto, (max-width: 578px) 100vw, 578px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-98 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-30.png\" alt=\"\" width=\"765\" height=\"839\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-30.png 765w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-30-274x300.png 274w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-30-65x71.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-30-225x247.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-30-350x384.png 350w\" sizes=\"auto, (max-width: 765px) 100vw, 765px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-99 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-31.png\" alt=\"\" width=\"879\" height=\"830\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-31.png 879w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-31-300x283.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-31-768x725.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-31-65x61.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-31-225x212.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-31-350x330.png 350w\" sizes=\"auto, (max-width: 879px) 100vw, 879px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-100 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-32.png\" alt=\"\" width=\"856\" height=\"836\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-32.png 856w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-32-300x293.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-32-768x750.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-32-65x63.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-32-225x220.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-32-350x342.png 350w\" sizes=\"auto, (max-width: 856px) 100vw, 856px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-101 aligncenter\" src=\"http:\/\/itp1.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-33.png\" alt=\"\" width=\"895\" height=\"721\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-33.png 895w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-33-300x242.png 300w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-33-768x619.png 768w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-33-65x52.png 65w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-33-225x181.png 225w, https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-content\/uploads\/sites\/20\/2018\/07\/Untitled-33-350x282.png 350w\" sizes=\"auto, (max-width: 895px) 100vw, 895px\" \/><\/p>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Operator Overloading<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/biSkYhIcy2U\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Additional References<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1) Stanley Lippmann, \u201cC++ Primer\u201d, Pearson Education.<\/p>\n<p>2) Bjarne Stroustrup, \u201cThe C++ Programming Language\u201d, Pearson Education.<\/p>\n<p>3) Scott Mayer, \u201cEffective C++\u201d, Addison Wesley.<\/p>\n<p>4) Bhushan Trivedi , Programming with ANSI C++, 2\/e , Oxford University Press<\/p>\n<p>5) Yashavant P. Kanetkar \u201cLet Us C++\u201d , Bpb Publications.<\/p>\n<p>6) Abhiram G. Ranade, \u201cAn Introduction to Programming through C++\u201d , McGraw Hill<\/p>\n<p>7) Ellis and B. Stroustrup \u201cAnnotated C++ Reference Manual\u201d, http:\/\/www.stroustrup.com\/arm.html<\/p>\n<p>8) Herbert Schildt, \u201cComplete Reference C++\u201d, McGraw Hill Publications.<\/p>\n<p>9) Ashok Kamthane, \u201cObject Oriented Programming with ANSI and Turbo C++\u201d, Pearson Education<\/p>\n<p>10) E Balaguruswami, \u201cObject Oriented Programming With C++\u201d, Tata McGraw Hill<\/p>\n<p>11) \u201cC++ FAQs\u201d, Pearson Education.<\/p>\n","protected":false},"author":4,"menu_order":11,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-jyoti-pareek"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-96","chapter","type-chapter","status-publish","hentry","contributor-dr-jyoti-pareek"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":7,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/96\/revisions"}],"predecessor-version":[{"id":399,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/96\/revisions\/399"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapters\/96\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/pressbooks\/v2\/chapter-type?post=96"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/contributor?post=96"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/itp1\/wp-json\/wp\/v2\/license?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}