CS410 – Software Engineering Lecture #12: C++ Basics V

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - Operator Overloading Outline 8.1 Introduction 8.2 Fundamentals of Operator Overloading 8.3.
March 25, 2014CS410 – Software Engineering Lecture #12: Advanced C++ I 1 Alumni Party The Alumni Party will take place on Tuesday, May 13 It would be great.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
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++
Review of C++ Programming Part II Sheng-Fang Huang.
CSIS 123A Lecture 5 Friend Functions Glenn Stevenson CSIS 113A MSJC.
March 4, 2014CS410 – Software Engineering Lecture #9: C++ Basics III 1 Today’s Topics The operators new and deleteThe operators new and delete The scope.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
March 6, 2014CS410 – Software Engineering Lecture #10: C++ Basics IV 1 Structure Pointer Operator For accessing members in structures and classes we have.
CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Part 9:
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.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
 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.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Object-Oriented Programming Course No.: Fall 2014 Overloading.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
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);
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Operator Overloading CS 3370 – C++ Chapter 14.
Operator Overloading Introduction
Overloading C++ supports the concept of overloading Two main types
Eine By: Avinash Reddy 09/29/2016.
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Overloading Operator MySting Example
References as Function Arguments
Object-Oriented Programming (OOP) Lecture No. 21
CS410 – Software Engineering Lecture #11: C++ Basics IV
Operator Overloading BCA Sem III K.I.R.A.S.
group work #hifiTeam
Operators and Expressions
Operator Overloading
Overloading the << operator
Operator Overloading; String and Array Objects
Operator Overloading.
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
Advanced Program Design with C++
Operator Overloading; String and Array Objects
Operator overloading Dr. Bhargavi Goswami
Operator Overloading, Friends, and References
Operator Overloading.
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
COP 3330 Object-oriented Programming in C++
Operator Overloading; String and Array Objects
CS410 – Software Engineering Lecture #5: C++ Basics III
Review of Function Overloading
Overloading the << operator
Overloading the << Operator
CS410 – Software Engineering Lecture #6: Advanced C++ I
(4 – 2) Introduction to Classes in C++
Presentation transcript:

CS410 – Software Engineering Lecture #12: C++ Basics V ADT Conversions Previously we talked about constructors as conversions. For example, in our class ModInt we provided the following constructor: ModInt(int i = 0): v(i % modulus) {} It is used automatically to convert values of type int into values of type ModInt: int i = 39; ModInt m; m = i; // legal operation October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

CS410 – Software Engineering Lecture #12: C++ Basics V ADT Conversions However, this constructor does not tell the compiler how to convert a ModInt value into an int value: int i = 39; ModInt m; i = m; // illegal operation It is not possible for the user to add a constructor to a built-in type such as int or double. How can we solve this problem? October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

CS410 – Software Engineering Lecture #12: C++ Basics V ADT Conversions The solution is to provide a special conversion function inside the user-defined class. The general form of such a member function is: operator type() { … } Such a member function must be nonstatic, have no parameters, have no declared return type, return an expression of the designated type. October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

CS410 – Software Engineering Lecture #12: C++ Basics V ADT Conversions For example, we could add such a special conversion function to the ModInt class: ModInt::operator int() { return v; } Then the following code is correct: int i = 39; ModInt m; i = m; Here, i receives the value 0. October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Overloading Operators We now know how to use the operator keyword to define a type-conversion member function. We will now take a look at how to use the operator keyword to overload the built-in C++ operators. We have already seen that overloading can give a function name a variety of meanings, depending on its arguments. Overloading operators can give additional meanings to the built-in operators such as ‘+’, allows infix expressions of both ADTs and built-in types, leads to shorter, more readable programs. October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Overloading Operators Example: class Foo { public: Foo operator-(); // unary minus: -Foo Foo operator-(int); // binary minus: Foo – int Foo operator-(Foo); // binary minus: Foo – Foo }; Foo operator-(int, Foo); // binary minus: int - Foo October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Unary Operator Overloading Example: class Clock { public: Clock(unsigned long i); void Print() const { cout << mins << ":" << secs << endl; } void Tick(); // add one second Clock operator++() { Tick(); return *this; } private: unsigned long totSecs, secs, mins; }; October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Unary Operator Overloading Clock::Clock(unsigned long i) { totSecs = i; secs = totSecs % 60; // convert into minutes-seconds format mins = (totSecs / 60) % 60; } void Clock::Tick() Clock Temp = Clock(++totSecs); secs = Temp.secs; mins = Temp.mins; October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Unary Operator Overloading int main() { Clock C1(59), C2(600); cout << "Initial times:" << endl; C1.Print(); C2.Print(); cout << endl; ++C1; // increase times by one second ++C2; cout << "After one second times are:" << endl; } October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Unary Operator Overloading Output: Initial times: 0:59 10:0 After one second times are: 1:0 10:1 October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Unary Operator Overloading We could also have overloaded the prefix ++ by using an ordinary function: Clock operator++(Clock& C) { C.Tick(); return C; } October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Binary Operator Overloading We continue with our clock example and show how to overload binary operators. When a binary operator is overloaded using a member function, it has as its first argument the implicitly passed class variable, as its second argument the single argument-list parameter. Friend functions and ordinary functions have both arguments specified in the parameter list. Of course, ordinary functions cannot access private members. October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Binary Operator Overloading class Clock { … friend Clock operator+(Clock C1, Clock C2); }; Clock operator+(Clock C1, Clock C2) return (C1.totSecs + C2.totSecs); } Here, the Clock constructor provides the implicit conversion from unsigned long to Clock. October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Binary Operator Overloading Analogously, we can overload the multiplication operator: class Clock { … friend Clock operator*(unsigned long factor, Clock C); } Clock operator*(unsigned long factor, Clock C) return (factor*C.totSecs); Notice that this function demands a fixed ordering (unsigned long * Clock). October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Binary Operator Overloading To avoid this, we can add a second overloaded function: Clock operator*(Clock C, unsigned long factor) { return (factor*C); } Notice that this second function overloading ‘*’ is defined in terms of the first one. Therefore, we do not need to make it a friend function. October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Binary Operator Overloading Testing the new functions: int main() { … cout << "The sum of these times is:" << endl; Clock C3 = C1 + C2; C3.Print(); cout<< endl; cout << "Multiplied by two is:" << endl; Clock C4 = 2*C3; C4.Print(); } October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Binary Operator Overloading Output: Initial times: 0:59 10:0 After one second times are: 1:0 10:1 The sum of these times is: 11:1 Multiplied by two is: 22:2 October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Overloading the << Operator Do you remember the Clock example? class Clock { public: Clock(unsigned long i = 0); void Print() const { cout << mins << ":" << secs << endl; } void Tick(); // add one second Clock operator++() { Tick(); return *this; } private: unsigned long totSecs, secs, mins; }; October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Overloading the << Operator Printing a time value works as follows: int main() { Clock C1(59), C2(600); cout << "Initial times:" << endl; C1.Print(); C2.Print(); cout << endl; } It would be more convenient to use the << operator for creating output, so we are going to overload the << operator for Clock objects. October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Overloading the << Operator How does the << operator work? Example: int main() { string text1(“John is ”), text2(“years old.”); int age = 43; cout << text1 << age << text2; } October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Overloading the << Operator cout << text1 << age << text2; cout << age << text2; ostream &operator<<(ostream &ostr, string &s); cout << age << text2; cout << text2; … ostream &operator<<(ostream &ostr, int i); October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Overloading the << Operator In order to overload the << operator for printing Clock objects, we add the following code to the Clock class: class Clock { public: … friend ostream &operator<<(ostream &out, Clock C); }; ostream &operator<<(ostream &out, Clock C) return (out << C.mins << ":" << C.secs); } October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Overloading the << Operator With this addition, we can output Clock values as follows: int main() { Clock C1(59), C2(600); cout << "Initial times:\n" << C1 << "\n" << C2; C1++; C2++; cout << "\n\nAfter one second times are:\n" << C1 << "\n" << C2; Clock C3 = C1 + C2; cout << "\n\nThe sum of these times is:\n" << C3; Clock C4 = 2*C3; cout << "\n\nMultiplied by two is:\n" << C4 << "\n"; } October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Overloading the << Operator The output looks like before: Initial times: 0:59 10:0 After one second times are: 1:0 10:1 The sum of these times is: 11:1 Multiplied by two is: 22:2 October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Overloading the << Operator Overloading the <<, (), and = operators can be nicely demonstrated with the Matrix example. In order to overload the << operator for printing matrices, we add the following code: class Matrix { public: … friend ostream &operator<<(ostream &out, const Matrix &m); }; (continued on next slide) October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V

Overloading the << Operator ostream &operator<<(ostream &out, const Matrix &m) { out << endl; for (int y = 0; y < m.dy; y++) for (int x = 0; x < m.dx; x++) out << m.p[x][y] << "\t"; } return out; October 13, 2016 CS410 – Software Engineering Lecture #12: C++ Basics V