COMP 53 – Week Two Operator Overloading.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 8 Operator Overloading, Friends, and References.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Introduction to C++ Systems Programming.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Chapter 8 Operator Overloading, Friends, and References.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
1 Chapter 8 Operator Overloading, Friends, and References.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
Friend Functions.  An ordinary function that is given special access to the private members of a class  NOT a member function of the class  Prototype.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Copyright 2006 Pearson Addison-Wesley, 2008, 2013 Joey Paquet 2-1 Concordia University Department of Computer Science and Software Engineering COMP345.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
1 CSC241: Object Oriented Programming Lecture No 08.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
CMSC 202 Lesson 8 Classes II. Warmup Declare a class (.h part) named “Cup” It has a data member that says how full it is One method called “Drink” that.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
General Updates ● The Wiki is now back up to date. I have also added a working link to the lecture slides online. ● Whenever you notice something missing.
Chapter 15 - C++ As A "Better C"
Operator Overloading Introduction
Structures and Classes
Chapter 1.2 Introduction to C++ Programming
Learning Objectives Pointers as dada members
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 13: Overloading and Templates
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Class and Objects UNIT II.
Operator Overloading; String and Array Objects
Introduction to C++ Systems Programming.
Motivation and Overview
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
COMP 51 Week Twelve Classes.
Basic Elements of C++.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Pointers and Pointer-Based Strings
Subroutines in Computer Programming
Operator Overloading BCA Sem III K.I.R.A.S.
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
11.0 Class parameters, const.
Basic Elements of C++ Chapter 2.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Operator Overloading; String and Array Objects
Chapter 5 Function Basics
Learning Objectives Classes Constructors Principles of OOP
Classes Short Review of Topics already covered Constructor
Java Classes and Objects 3rd Lecture
Constructors and Other Tools
Friends, Overloaded Operators, and Arrays in Classes
Operator Overloading, Friends, and References
Operator Overloading.
CISC/CMPE320 - Prof. McLeod
COP 3330 Object-oriented Programming in C++
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Pointers and Pointer-Based Strings
Operator Overloading; String and Array Objects
CMSC 202 Lesson 8 Classes II.
Class rational part2.
Presentation transcript:

COMP 53 – Week Two Operator Overloading

Topics Basic Operator Overloading References and More Overloading Unary operators As member functions References and More Overloading << and >> Operators: = , [], ++, --

Why Do We Care Simplify coding syntax Ability to use similar operations on abstract data types Polymorphism is key building block of Object Oriented Programming

Function Overloading Array Sum Example Function Signature Data types Number of Parameters Overloading Same function name But different signature Constructors work in same way Integer Version int sumarray(int a[], int size) { int sum = 0; for (int i = 0; i < size; i++) sum += a[i]; return(sum); } Double Version double sumarray(double a[], int size) { double sum = 0; Main Call Double total, scores[3] = {1.0, 2.5, 3.5}; Int amount, values[3] = {5, 10 , 15}; Total = sumarray(scores, 3); Amount = sumarray(values, 3); Same function name, but different data types

Class Constructors Overload Review Note the different versions of constructor

Operator Overloading Introduction What is a Math operation? Say x + 7 = Syntax details Binary operation (+, -, *, /, ==) Unary operation (leading – or !) What does an add function look like? result = add(x, 7); Could this be written like this? +(x, 7) "+" is the function name x, 7 are the arguments Function "+" returns "sum" of it’s arguments Simply "called" with different syntax: x + 7 "+" is binary operator with x & 7 as operands We "like" this notation as humans

Overloading Basics Given previous example: Overloading operators VERY similar to overloading functions Operator itself is "name" of function Work with OUR data types Operator overload definitions generally very simple Just perform "addition" particular to "your" type Example Declaration: const Money operator +( const Money& amount1, const Money& amount2); Overloads + for operands of type Money Uses constant reference parameters for efficiency Returned value is type Money Allows addition of "Money" objects Given previous example: Note: overloaded "+" NOT member function Definition is "more involved" than simple "add" Requires issues of money type addition Must handle negative/positive values Note keyword

Money "+" Definition: Display 8.1 Operator Overloading Note – function defined outside of class Previous examples: standalone functions Defined outside a class Can overload as "member operator" Considered "member function" like others When operator is member function: Only ONE parameter, not two! Calling object serves as 1st parameter

Constructors Returning Objects Constructor a "void" function? We "think" that way, but no A "special" function With special properties CAN return a value! Recall return statement in "+" overload for Money type: return Money(finalDollars, finalCents); Returns an "invocation" of Money class! So constructor actually "returns" an object! Called an "anonymous object"

Overloaded "==" Equality operator, == Enables comparison of Money objects Declaration: bool operator ==(const Money& amount1, const Money& amount2); Returns bool type for true/false equality Again, it’s a non-member function (like "+" overload)

Need for const Return Value Consider expression newM = m1 + m2 Where m1 & m2 are Money objects Object returned is Money object - which is assigned to newM Objects have methods, so these can be invoked in any expression (m1+m2).output(); //Legal (m1+m2).input(); //ILLEGAL Allows modification of "anonymous" object! Can’t allow that here!

const Functions When to make function const? Good style dictates: Constant functions not allowed to alter class member data Constant objects can ONLY call constant member functions Good style dictates: Any member function that will NOT modify data should be made const Use keyword const after function declaration and heading

Overloading Unary Operators C++ has unary operators: Defined as taking one operand e.g., - (negation) x = -y; // Sets x equal to negative of y Other unary operators: ++, -- Unary operators can also be overloaded

Overload "-" for Money Overloaded "-" function definition: Overloaded "-" function declaration Place outside class definition Single input parameter "-" operator is overloaded twice! For two operands/arguments (binary) For one operand/argument (unary) Definitions must exist for both Overloaded "-" function definition: Money operator –(Money& amount) { return Money( -amount.getDollars(), -amount.getCents()); }

Overloaded "-" Examples Money amount1(10), amount2(6), amount3; amount3 = amount1 – amount2; amount3.output(); //Displays $4.00 amount3 = -amount1; amount3.output() //Displays -$10.00 Binary overload Unary overload

Operator Overload Practice Create a project for DayOfYear class Overload the + operator to add two dates together Make sure to handle the cases of date wrap If month > 12, need to subtract 12 If day > 30, need to subtract 30 Or use the modulus operator Copy and paste the code in the notes section Notice there are some commented out lines that should be used once you get the operator function created. #include <iostream> #include <cstdlib> //for exit using namespace std; class DayOfYear { public: DayOfYear(int monthValue, int dayValue); //Initializes the month and day to arguments. DayOfYear(int monthValue); //Initializes the date to the first of the given month. DayOfYear(); //Initializes the date to January 1. void input(); void output(); int getMonthNumber(); //Returns 1 for January, 2 for February, etc. int getDay(); private: int month; int day; void testDate(); }; int main() DayOfYear date1(2, 21), date2(5), date3; cout << "Initialized dates:\n"; date1.output(); cout << endl; date2.output(); cout << endl; date3.output(); cout << endl; // date3 = date1 + date2; // cout << "Added date 3 = \n"; // date3.output(); cout << endl; date1 = DayOfYear(10, 31); cout << "date1 reset to the following:\n"; system("pause"); return 0; } DayOfYear::DayOfYear(int monthValue, int dayValue) : month(monthValue), day(dayValue) testDate(); DayOfYear::DayOfYear(int monthValue) : month(monthValue), day(1) DayOfYear::DayOfYear() : month(1), day(1) {/*Body intentionally empty.*/ //uses iostream and cstdlib: void DayOfYear::testDate() if ((month < 1) || (month > 12)) cout << "Illegal month value!\n"; exit(1); if ((day < 1) || (day > 31)) cout << "Illegal day value!\n"; int DayOfYear::getMonthNumber() return month; int DayOfYear::getDay() return day; //Uses iostream and cstdlib: void DayOfYear::input() cout << "Enter the month as a number: "; cin >> month; cout << "Enter the day of the month: "; cin >> day; if ((month < 1) || (month > 12) || (day < 1) || (day > 31)) cout << "Illegal date! Program aborted.\n"; void DayOfYear::output() switch (month) case 1: cout << "January "; break; case 2: cout << "February "; break; case 3: cout << "March "; break; case 4: cout << "April "; break; case 5: cout << "May "; break; case 6: cout << "June "; break; case 7: cout << "July "; break; case 8: cout << "August "; break; case 9: cout << "September "; break; case 10: cout << "October "; break; case 11: cout << "November "; break; case 12: cout << "December "; break; default: cout << "Error in DayOfYear::output. Contact software vendor."; cout << day;

Member Operator in Action Money cost(1, 50), tax(0, 15), total; total = cost + tax; If "+" overloaded as member operator: Variable/object cost is calling object Object tax is single argument Think of as: total = cost.+(tax); Declaration of "+" in class definition: const Money operator +(const Money& amount); Notice only ONE argument

Overloading Operators: Which Method? Object-Oriented-Programming Principles suggest member operators Many agree, to maintain "spirit" of OOP Member operators more efficient No need to call accessor & mutator functions At least one significant disadvantage (Later in chapter…)

Overloading >> and << Enables input and output of our objects Improves readability Enables: cout << myObject; cin >> myObject; Instead of need for: myObject.output(); …

What really is cout/cin Both are defined as objects inside of <iostream> The << and >> symbols are binary operands Need to define return values for << and >> operations Example: cout << "Hello"; Recall Money class Used member function output() Nicer if we can use >> operator: Money amount(100); cout << "I have " << amount << endl; instead of: cout << "I have "; amount.output() Operand 1 is the object Operand 2 is a string constant

Overloaded >> Example 1 of 5

Overloaded >> Example 2 of 5

Overloaded >> Example 3 of 5

Overloaded >> Example 4 of 5

Overloaded >> Example 5 of 5

Assignment Operator, = Must be overloaded as member operator Automatically overloaded Default assignment operator: Member-wise copy Member variables from one object  corresponding member variables from other Default OK for simple classes But with pointers  must write our own!

Increment and Decrement Each operator has two versions Prefix notation: ++x; Postfix notation: x++; Must distinguish in overload Prefix – use operator ++(object) { … } Postfix - use operator ++(object1, object1) { … } Just a marker for compiler! Specifies postfix is allowed

Increment Overload Practice Add the increment ++ code to the Date project. Add one to the months value Make sure to check for going beyond 12 months.

Overload Array Operator, [ ] Can overload [ ] for your class To be used with objects of your class Operator must return a reference! Operator [ ] must be a member function!

Key Takeaways Overloading is just ONE aspect of polymorphism Operators are really just functions C++ built-in operators can be overloaded To work with objects of your class Can overload <<, >> Return type is a reference to stream type