Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.

Slides:



Advertisements
Similar presentations
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
What is a copy constructor? A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance during initialization.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
Operator Overloading in C++
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Overloading Operators. Operators  Operators are functions, but with a different kind of name – a symbol.  Functions.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Type Equivalence Rules Ada –Strict name equivalence except for almost everything Unique array constructors give rise to unique types Subtypes can create.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Introduction to Classes in C++
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Operator Overloading. Objectives At the conclusion of this lesson, students should be able to Explain what operator overloading is Write code that overloads.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 17: Vectors and Arrays 1 Based on slides created by Bjarne Stroustrup.
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 RAD due Friday in your Wiki. Presentations week 6 – next week. Schedule on next slide. Today: –Operator.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Operator Overloading.
Operator Overloading Introduction
Yan Shi CS/SE 2630 Lecture Notes
Overloading C++ supports the concept of overloading Two main types
Learning Objectives Pointers as dada members
AKA the birth, life, and death of variables.
Operator Overloading CMSC 202.
CS 2304: Operator Overloading
Pointers Revisited What is variable address, name, value?
Operator Overloading BCA Sem III K.I.R.A.S.
Anatomy of a class Part II
Anatomy of a class Part I
Lecture 6 C++ Programming
The dirty secrets of objects
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
Operator Overloading CSCE 121 J. Michael Moore
Const in Classes CSCE 121 J. Michael Moore.
Explicit and Implicit Type Changes
Operators Lecture 10 Fri, Feb 8, 2008.
Abstraction: Generic Programming, pt. 2
Copy Assignment CSCE 121 J. Michael Moore.
Function Overloading CSCE 121 J. Michael Moore
Operator Overloading.
Anatomy of Inheritance
CISC/CMPE320 - Prof. McLeod
COP 3330 Object-oriented Programming in C++
Java Programming Language
Operator Overloading; String and Array Objects
Anatomy of a class Part II
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
ENERGY 211 / CME 211 Lecture 18 October 31, 2008.
Anatomy of Inheritance
Copy Assignment CSCE 121.
Anatomy of a class Part I
Presentation transcript:

Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.

Operators Assignment: = Arithmetic: +, -, *, /, % Compound assignment: +=, -=, *=, /=, %= Increment/decrement: ++, -- Logical: !, &&, || Comparison: ==, !=, <, >, <=, >= Other: <<, >>, etc.

Operators (kind of like functions...) int a = 0, b = 5; int c = a + b; // int +(int a, int b); c++; // void ++(int& c); int d = a; // void =(const int& a, int &d);

Operators (a lot like functions...) int i = 0, j = 5; string s; i++; // matches ++ operator’s ‘arguments’ s++; // no match int k = i + j; // integer addition string s2 = s + s; // string concatenation

is a PRIMITIVE datatype Operator Overloading int k = i + j; // int +(int a, int b); string s2 = s + s; // string +(string a, string b); NOT actually a function, because int is a PRIMITIVE datatype

Operator Overloading int k = i + j; // int +(int a, int b); string s2 = s + s; // string operator+(const& string a, const& string b);

Does not have access to private MyClass data and methods. Operator overloading class MyClass { int myAttr = 7; }; // outside class scope MyClass operator+(const MyClass& a, const MyClass& b) { // ... } Does not have access to private MyClass data and methods.

Operator overloading (inside class scope) class MyClass { int myAttr = 7; // ... public: MyClass(int k) : myAttr(k) {} MyClass operator==(const MyClass& b) { } }; MyClass operator+(const MyClass& a, const MyClass& b); Has access to private MyClass data and methods

Parameters (implicit and explicit) bool MyClass::operator==(const MyClass& other){ return (myAttr == other.myAttr); } // ... MyClass a = MyClass(7); MyClass B = MyClass(11); if (a == b) { Equivalent to calling: if (a.operator==(b)) {

Alternatively element access operator for pointers to objects bool MyClass::operator==(const MyClass& other){ return (this->myAttr == other.myAttr); } element access operator for pointers to objects Recall: ‘this’ is a pointer to the instance of the class Student that called the == operator

Return types string operator+(const string& a, const string& b); bool MyClass::operator==(const MyClass& other); MyClass& MyClass::operator=(const MyClass& other);

Returning a reference in a overloaded operator MyClass& MyClass::operator=(const MyClass& other) { this->myAttr = other.myAttr; return *this; } Essentially, dereference ‘this’ and the compiler knows how to get the address from the class.

Rules for Operator Overloading Can only overload existing operators can’t create your own Cannot change the number of operands Must take at least one non-primitive datatype operand