Operator Overloading in Fraction Class, Slides of Computer Science

How to overload operators in a fraction class to make fraction arithmetic more natural and convenient. The rationale behind overloading the '+' operator and provides code examples for overloading '+' for two fraction objects and for an integer and a fraction object. It also discusses the possibility of overloading other operators and relational operators.

Typology: Slides

2012/2013

Uploaded on 03/21/2013

dheeraj
dheeraj 🇮🇳

5

(4)

101 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Operator Overloading
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Operator Overloading in Fraction Class and more Slides Computer Science in PDF only on Docsity!

Operator Overloading

Consider the Fraction Class On object in the class Fraction encapsulates

two integer variables designated by numerand denom. These two variables togethersymbolize and contain the rational numberthat we typically think of as numer/denom. For Example:5/2, 3/5, 2/9, 100/3, etc.

Fraction Arithmetic We wrote a method to add two Fractions, and if C and

B are Fractions, their sum is computed by theexpression C.Add(B); Add is a

non-static method

. C (or always the object to

the left of the dot operator) is the object that callsthe Add method, and B is the argument. Their sumis computed and returned. C and B are notchanged in the call. Review the definition of Add. Why is it not static?

A more natural way to add. Since we are accustomed to doing Fraction

arithmetic with +, -, *, and /, it would be morenatural to write C+B instead of C.Add(B). This is easily accomplished by “overloading”

the + operator for the Fraction class. The next slide shows the overloaded method.

What happens? If C and B are Fraction objects, then the expression C

  • B invokes a call to the overloaded operator +,passing C to X and B to Y in the parameter list ofthe method operator+. The overloaded operator+then invokes the method Add passing alongreferences to the appropriate objects. Again, if Adddidn’t exist, we could do the work right in operator+and return the value.

Adding int’s and Fractions Could we use an expression N + C, where N is

an integer and C is a Fraction? Not yet. But we can add another overloaded

method that will do it.

Observation! In the previous overloaded method, we merely

use one of the Fraction class constructors tocreate a Fraction from N and then use thealready defined overloaded+ for two Fractionobjects. Will this work for C + N? No. But it’s easy to

add a third operator+ that takes a Fraction onthe left and an integer on the right.

Other Operators? Many operators can be overloaded. For the

Fraction class, we could overload the otherarithmetic operators -, *, and /. In doing so,we could, just as with +, use Sub, Mult, andDiv that have already been written.

operator < public

static bool

operator<

(Fraction

X,

Fraction

Y)

{

return

X.numerY.denom <Y.numerX.denom);

} Here we use the fact that a/b < c/d if and only if ad<bc, as long

as d and b are positive. (Multiplying both sides of an inequalityby a positive number preserves the inequality.)

A rule to follow When overloading relational operators, they

must be overloaded in pairs. For example ifyou overload <, you must overload >.Similarly for <= and >=; and == and !=;