Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros

Similar presentations


Presentation on theme: "Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros"— Presentation transcript:

1 Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu

2 Operator 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.

3 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.

4 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.

5 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.

6 Operator Overloading Operators that can be overloaded: +-*/%^ &|~!=< >+=-=*=/=%= ^=&=|=<<>>>>= <<===!=<=>=&& ||++--->*,-> []()newdeletenew[]delete[]

7 Operator Overloading as a Friend Function friend Fraction Add (Fraction f1, Fraction f2); // Sample Call Fraction n1, n2, n3; n3= Add(n1,n2);

8 The “Fraction” Class Makefile driver: driver.o fraction.o g++ -o driver driver.o fraction.o driver.o: driver.cpp g++ -c driver.cpp fraction.o: fraction.h fraction.cpp g++ -c fraction.cpp

9 The “Fraction Class” Header File #ifndef FRACTION_H #define FRACTION_H // ************************************** // * is needed here so the compiler * // * is aware of the overload of ostream and * // * istream. * // ************************************** #include using namespace std; class Fraction { friend ostream& operator<<(ostream &S, const Fraction &f2); friend istream& operator>>(istream &S, Fraction &f2);

10 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; }; #endif

11 The “Fraction Class” Implementation File (.cpp) #include #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); }

12 The “Fraction Class” The constructors in the.cpp file Fraction::Fraction() { numerator=1; denominator=1; } Fraction::Fraction(int n, int d) { this->setnumerator(n); this->setdenominator(d); }

13 The “Fraction Class” Set functions in the.cpp file bool 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; }

14 The “Fraction Class” get functions in the.cpp file bool Fraction::setnumerator(int n) { numerator = n; return true; } int Fraction::getdenominator() { return denominator; } int Fraction::getnumerator() { return numerator; }

15 The “Fraction Class” The Add Function in the.cpp file Fraction 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; }

16 Overload of the “+” operator for the Fraction class as a “Friend” // ******************************** // * This goes in the.h file * // ******************************** friend Fraction &operator+( Fraction f1, Fraction f2);

17 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;}

18 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 = operator+(F1,F2); F1= F1 + F2; F1= F1 + F2 + F3 + F4;

19 Operator Overloading as a member function // ******************************** // * This goes in the.h file * // * Note, there is only one * // * parameter. * // ******************************** Fraction operator+( Fraction f1);

20 Operator Overloading as a member function // * **************************************** // * Note: Only passing in one parameter. * // ****************************************** Fraction Fraction::operator+(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; }

21 Operator Overloading as a member function // * ******************************* // * 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 = F1.operator+(F2); F1= F1 + F2; F1= F1 + F2 + F3 + F4;

22 Some other possible arithmetic operators Prototypes only // multiplication overload for Fractions friend Fraction operator*(Fraction f1, Fraction f2); // addition operator to add a Fraction and an integer friend Fraction operator+(Fraction f, int n); // same as above, but this one allows the int to come first in the call friend Fraction operator+(int n, Fraction f); Note: The last two are not really needed but will they both work for both the Friend and Member function. Remember that in the case of the member function, the first operator Is the calling function and must be a member of the class.

23 Overloading of comparison operators Very handy to do this. Many times we want to compare two objects just as we would an integer, real, or character. Allows us to maintain the consistency of the code. // As a friend function friend bool Equals(Fraction f1, Fraction f2); // Sample call if(Equals(n1,n2)) cout << “n1 is equal to n2”)<< endl; // As an operator overload friend bool operator==( Fraction f1, const Fraction f2); // Sample Call if (n1 == n2 ) cout << “n1 is equal to n2”)<< endl;

24 Overloading the > extraction operators > work with basic types already defined by the C++ language and included in iostream. They ( >) do not work with user defined types and classes. Your own classes or types must be converted into one of the basic types in order for these to work. This is done through overloading.

25 Overloading the > extraction operators The > are binary operators. – The first parameter MUST be an ostream object. – The > are almost always defined as friends (outside functions or members). – The second parameter is whatever type you define. – Your new defined function must convert the new type into a basic type expected by iostream.

26 Overloading the > extraction operators // ************************************ // * Another version with a leading * // * whole number plus the fraction. * // ************************************ istream & operator >> (istream& inputStream, Fraction & f1) { integer i1, i2, i3; char slash; cin >> i1; cin >> i2; cin >> slash; cin >> i3; // add error checking here }


Download ppt "Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros"

Similar presentations


Ads by Google