COP 3330 Object-oriented Programming in C++

Slides:



Advertisements
Similar presentations
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Advertisements

Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Operator Overloading Fundamentals
1 Overloading Operators COSC 1567 C++ Programming Lecture 7.
Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator Overloading in C++
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Case Study - Fractions Timothy Budd Oregon State University.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Operator Overloads Part2. Issue Provide member +(int) operator Rational + int OK int + Rational Error.
Operator Overloading Version 1.0. Objectives At the end of this lesson, students should be able to: Write programs that correctly overload operators Describe.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads.
Chapter 8 Operator Overloading, Friends, and References.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Chapter 13: Overloading and Templates. Objectives In this chapter, you will – Learn about overloading – Become familiar with the restrictions on operator.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 CSC241: Object Oriented Programming Lecture No 08.
Friend Functions. Problem u Assuming two Complex objects u How would one add two numbers? W + X Complex operator+(const Complex& w, const Complex& x);
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Andy Wang Object Oriented Programming in C++ COP 3330
CSE1002 – Problem Solving with Object Oriented Programming
Yan Shi CS/SE 2630 Lecture Notes
Overloading C++ supports the concept of overloading Two main types
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value A copy of the argument’s value.
Chapter 13: Overloading and Templates
Chapter 14: More About Classes.
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Operator Overloading; String and Array Objects
Visit for more Learning Resources
Object Oriented Programming COP3330 / CGS5409
Object-Oriented Design (OOD) and C++
Operator Overloading Part 2
Polymorphism in C++ Operator Overloading
Objectives Define polymorphism Overload functions
Jim Fawcett Copyright ©
Chapter 15: Overloading and Templates
Operator Overloading BCA Sem III K.I.R.A.S.
Chapter 14: More About Classes.
Operators Lecture 10 Fri, Feb 8, 2008.
Operator Overloading; String and Array Objects
Operator Overloading.
Andy Wang Object Oriented Programming in C++ COP 3330
Advanced Program Design with C++
Operator Overloading; String and Array Objects
Overview of C++ Overloading
Operator Overloading, Friends, and References
Operator Overloading.
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Operator Overloading; String and Array Objects
Andy Wang Object Oriented Programming in C++ COP 3330
Jim Fawcett Copyright ©
Presentation transcript:

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

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

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

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

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!

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

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., +, -, *, /

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)

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…)

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!

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)

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

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

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’);

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;

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

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

Questions