Operator Overloading Programming in C++ Fall 2008Operator OverloadingSlide 3Slide 4Slide 5Slide 6Operator Overloading as a Friend FunctionThe “Fraction” ClassThe “Fraction Class”The “Fraction Class”Slide 11Slide 12Slide 13Slide 14Slide 15Overload of the “+” operator for the Fraction class as a “Friend”Slide 17Slide 18Operator Overloading as a member functionSlide 20Slide 21Some other possible arithmetic operatorsOverloading of comparison operatorsOverloading the << insertion and >> extraction operatorsSlide 25Slide 26Operator OverloadingProgramming in C++Fall 2008 Dr. David A. [email protected] Overloading In working with machine code and assembly language programs, there are different operations that perform the same basic function for different types of data. For instance, the machine instruction to add two binary numbers together differs from that operation that is meant to add two packed decimal numbers. In Higher level languages when adding numbers, we want to abstract the notion of basic arithmetic and not worry about using the correct machine function. Thus, the concept of operation overloading. There are built-in operations already such as +, - , *, /, =, ==, %, etc.Operator Overloading•We can already overload functions. –Overloaded functions have the same name but different signatures ( different parameters and return values). •Operator Overload in C++–In reality it is just a function call using special notation. –C++ already does some operator overloading. Example: The “+” operator works with floats, nits, doubles, and chars. –Operator overloading is done for the purpose of making the use of familiar notation in your program. For instance, let us say you wanted to add two matrices. You could overload the “+” operator to accomplish this.Operator Overloading•Rules for Operator Overloading–Overloading an operator cannot change it’s precedence. –Overloading an operator cannot change it’s associativity. –Overloading an operator cannot change the number of operands. –It is not possible to create new ones, only new versions of existing ones. –Operator meaning on built-in features cannot be changes. For instance you cannot change the “+” for integers.Operator Overloading•Format–The name of an operator is always a conjunction of the keyword operator and than the symbol itself.–Example: •operator+•Operator++•Operator- -•Operator –•Operator <<•Operator == •Operator = –NOTE: Overloaded operators are not member functions.Operator Overloading•Operators that can be overloaded:+ - * / % ^& | ~ ! = <> += -= *= /= %=^= &= |= << >> >>=<<= == != <= >= &&|| ++ -- ->* , ->[] () new delete new[] delete[]Operator Overloading as a Friend Functionfriend Fraction Add (Fraction f1, Fraction f2); // Sample CallFraction n1, n2, n3;n3= Add(n1,n2);The “Fraction” Class•Makefiledriver: driver.o fraction.og++ -o driver driver.o fraction.odriver.o: driver.cppg++ -c driver.cppfraction.o: fraction.h fraction.cppg++ -c fraction.cppThe “Fraction Class”•Header File#ifndef FRACTION_H#define FRACTION_H// **************************************// * <iostream> is needed here so the compiler *// * is aware of the overload of ostream and *// * istream. *// **************************************#include <iostream>using namespace std; class Fraction { friend ostream& operator<<(ostream &S, const Fraction &f2); friend istream& operator>>(istream &S, Fraction &f2);The “Fraction Class” public: Fraction(); Fraction(int, int); bool setdenominator(int ); bool setnumerator(int); int getdenominator(); int getnumerator(); Fraction Add(Fraction &f1); private: int denominator; int numerator;};#endifThe “Fraction Class” •Implementation File (.cpp) #include <iostream>#include "fraction.h"using namespace std;ostream & operator<<(ostream &S,Fraction &f2){ S << f2.getnumerator() << '/' <<f2.getdenominator() ; return S;} istream & operator>>(istream &S, Fraction &f2){ int n,d; char c; S >> n>>c>>d; f2.setnumerator(n); f2.setdenominator(d);}The “Fraction Class” •The constructors in the .cpp fileFraction::Fraction(){ numerator=1; denominator=1;}Fraction::Fraction(int n, int d){ this->setnumerator(n); this->setdenominator(d); }The “Fraction Class” •Set functions in the .cpp filebool Fraction::setdenominator(int d){ if(d !=0) { denominator = d; return true; } else denominator=1; return false; }bool Fraction::setnumerator(int n){ numerator = n;return true; }The “Fraction Class” •get functions in the .cpp filebool Fraction::setnumerator(int n){ numerator = n;return true; }int Fraction::getdenominator(){ return denominator;}int Fraction::getnumerator(){ return numerator;}The “Fraction Class” •The Add Function in the .cpp fileFraction Fraction::Add(Fraction &f1){ Fraction R1=f1; Fraction R2; R2.numerator =this->numerator; R2.denominator = this->denominator; R1.numerator = R1.numerator * R2.denominator; R2.numerator = R2.numerator * R1.denominator; R1.denominator = R1.denominator * R2.denominator; R2.denominator = R1.denominator; R1.numerator = R1.numerator + R2.numerator; return R1;}Overload of the “+” operator for the Fraction class as a “Friend”// ********************************// * This goes in the .h file *// ********************************friend Fraction &operator+( Fraction f1, Fraction f2);Overload of the “+” operator for the Fraction class as a “Friend”// **********************************// * This goes in the .cpp file *// **********************************Fraction &operator+(Fraction f1, Fraction f2){ Fraction R1=f1; Fraction R2=f2; R1.numerator = R1.numerator * R2.denominator; R2.numerator = R2.numerator * R1.denominator; R1.denominator = R1.denominator * R2.denominator; R2.denominator = R1.denominator; R1.numerator = R1.numerator + R2.numerator; return R1;}Overload of the “+” operator for the Fraction class as a “Friend”// * *******************************// * What the calls look like in *// * the driver or main routine. *// *********************************Fraction F1,F2,F3,F4; cin >> F1; cin >> F2; cin >> F3;cin >> F4; F1 =
View Full Document