Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3330 Object-oriented Programming in C++

Similar presentations


Presentation on theme: "COP 3330 Object-oriented Programming in C++"— Presentation transcript:

1 COP 3330 Object-oriented Programming in C++
Operator Overloading Spring 2019

2 Motivation We are pretty familiar with arithmetic operators
int x = 3, y = 6, z; float a = 3.4, b = 2.1, c; z = x + y; c = a / b + 1/3; Question: Can we use arithmetic operators on user-defined classes, say, Fraction? Fraction n1, n2, n3; n3 = n1 + n2; NO! Because Fraction is a user-defined type, the compiler feels confused about this statement: n1 + n2

3 Motivation We are pretty satisfied with screen-output/input operators for primitive types int x = 5, y = 0; cout << x << y; How about … Fraction f(3, 4); cout << “The fraction f = “ << f << endl; NO! The iostream library knows nothing about Fraction We need a new mechanism to endow meanings for operators, such as + and <<

4 Fundamentals Many existing operators that work on built-in types (e.g., int, double) Ex. +, -, *, /, %, ++, --, …… Ex. [], <<, >>, …… Operator overloading allows programmers to define new versions of these operators in programmer-defined classes A C++ operator is a function called with special notations Overloading a function involves writing a function C++ already has operator overloading + operator works on ints, doubles, chars, and strings

5 Rules Regarding Operator Overloading
CANNOT change a number of properties Precedence: a + b*c Associativity: (a + b) + c = a + (b + c) Number of operands CANNOT create new operators Can only overload the existing ones Operator meaning on the built-in types CANNOT be changed By overloading you CAN change the meaning of the operator in your class E.g., you can define ‘+’ as subtraction!

6 Overloading Operator in Functions
Some operators can be written as member functions Functions defined inside the scope of a class Some operators can be written as stand-alone friend functions Functions defined outside of the class’ scope but have access to all private data and functions of the class Even though the prototypes for friend functions appear in the class declaration, friends are NOT member functions Some operators can be written as either member functions or stand-alone friend functions

7 Type of Operators A unary operator has one operand
As a stand-alone function, it will take a parameter As a member function, one calling object with no parameters E.g., - (minus) A binary operator has two operands As a stand-alone function, it will take two parameters As a member function, the first operand is the calling object Taking the second operand as a parameter E.g., +, -, *, /

8 Format of Overloading Operators
An operator is just a function! It requires a name, a return type, and a parameter list Name of an operator is always a conjunction of the keyword operator and the operator symbol operator+ operator++ operator<< operator== Format of operator overload declaration return_type operator<operator_symbol> (parameter_list)

9 Overloading Arithmetic Operators
Can be overloaded either as stand-alone functions or as member functions With operator overloading, we can have the following, implemented as a stand-alone friend function friend Fraction operator+(const Fraction &f1, const Fraction &f2) We can invoke the function like n3 = operator+(n1, n2); Or more frequently, we can use the infix notation n3 = n1 + n2; // cascading also works (n1 + n2 + n3 + n4…)

10 Overloading Arithmetic Operators
Let’s overload the operator, +, as a member function Fraction operator+(const Fraction &f) const; To call, either way will work // hardly anyone will do this n3 = n1.operator+(n2); // n1 is the calling object, n2 is the parameter n3 = n1 + n2; Consider these expressions (work for friend functions) n3 = n1 + 5; //Good: conversion constructor for 5 n3 = 5 + n1; //Won’t work for member function The calling object is the integer 10, not a Fraction object!

11 Overloading Comparison Operators
Can be overloaded either as stand-alone or member functions Here is the original equals function friend bool equals(const Fraction &f1, const Fraction &f2); We can write it as an operator overload friend bool operator==(const Fraction &f1, const Fraction &f2); Here are the corresponding calls Fraction n1, n2; if (equals(n1, n2)) cout << “n1 and n2 are equal”; If (n1 == n2)

12 Overloading Comparison Operators
We Can Overload All Six operator== // Equals operator!= // Not equals operator< // Less than operator> // Greater than operator<= // Less or equal operator>= // Greater or equal

13 Overloading << and >> Operators
Insertion << and Extraction >> are binary operators The first operator is always an IO stream object (e.g., cout) They cannot be defined as a member function! Should be defined as outside (usually friend) functions The second parameter is the user-defined type Here is the overloading function friend ostream& operator<<(ostream &s, const Fraction &f) friend istream& operator>>(istream &s, Fraction &f) Notice that f is passed by reference, NO const Need to modify the original

14 Overloading << and >> Operators
In these functions, we must return ostream or istream S (not cin/cout) is used as a formal parameter, a nickname for whatever is passed in (could be cin/cout, could be file streams) How to use the overloaded operator <<? Fraction f1; cout << “f1 = ” << f1 << endl; // same as (((cout << “f1 is ”) << f1) << ‘\n’);

15 Overloading Prefix/Postfix Operators
int j = 0; while (j < 10) { cout << ++j << endl; } // same as j = j + 1; cout << j << endl; int j = 0; while (j < 10) { cout << j++ << endl; } // same as cout << j << endl; j = j + 1;

16 Overloading Prefix/Postfix Operators
Overloaded as member functions Fraction& operator++(); // prefix increment Fraction operator++(int); // postfix increment Fraction& operator--(); // prefix decrement Fraction operator--(int); // postfix decrement The int parameter is just a note to the compiler to indicate that it is a postfix operator

17 Overloading Prefix/Postfix Operators
Fraction& Fraction::operator++() { numerator += denominator; return *this; } Fraction Fraction::operator++ (int) Fraction temp = *this; return temp; // return OLD value Notice that we never use this int

18 Questions


Download ppt "COP 3330 Object-oriented Programming in C++"

Similar presentations


Ads by Google