Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
Operator Overloading Fundamentals
Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
Short C++ Review CS 302 – Data Structures. Call by value/reference.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Class template Describing a generic class Instantiating classes that are type-specific version of this generic class Also are called parameterized types.
Constructors & Destructors Review CS 308 – Data Structures.
Copy Constructors Shallow Copy: –The data members of one object are copied into the data members of another object without taking any dynamic memory pointed.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Pointer Data Type and Pointer Variables
Recap, Test 1 prep, Composition and Inheritance. Dates Test 1 – 12 th of March Assignment 1 – 20 th of March.
C++ Programming for Graphics Lecture 5 References, New, Arrays and Destructors.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
C++ Review CS 302 – Data Structures Review Topics Calling functions by value or reference Pointers and reference variables Static and dynamic arrays.
February 11, 2005 More Pointers Dynamic Memory Allocation.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
C++ Review (3) Structs, Classes, Data Abstraction.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
1 CSC241: Object Oriented Programming Lecture No 22.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Pointers You are not responsible for virtual functions (starting on.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Data Structures Lecture 2: Array Azhar Maqsood NUST Institute of Information Technology (NIIT)
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Operator Overloading.
Chapter 2 Objects and Classes
Yan Shi CS/SE 2630 Lecture Notes
C++ Review Data Structures.
Pointers and Dynamic Arrays
Overloading Operator MySting Example
Concepts of Constructors and Its Types
Constructor & Destructor
Constructors & Destructors
14.4 Copy Constructors.
LinkedList Class.
group work #hifiTeam
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CS 144 Advanced C++ Programming March 21 Class Meeting
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
Presentation transcript:

Operator Overloading CS 308 – Data Structures

What is operator overloading? Changing the definition of an operator so it can be applied on the objects of a class is called operator overloading. To overload an operator, we need to write a function for the operator we are overloading.

Special cases The assignment operator (=) may be used with every class without explicit overloading. The default behavior of the assignment operator is a memberwise assignment of the data members of the class. The address operator (&) may also be used with objects of any class without explicit overloading. It returns the address of the object in memory.

Explicit overloading of the assignment operator The default overloading is not enough for classes with pointer members. void operator=(class_name&); class string { private: char *s; int size; public: string(char *); // constructor ~string(); // destructor void operator=(string&); void print(); void copy(char *); };

void string::operator=(string& old_str) { char *tmp; size = old_str.size; tmp = new char[size+1]; // assign new memory strcpy(tmp, old_str.s); delete [] s; // must release previously assigned memory s = tmp; } void main() { string str1("George"); string str2("Mary"); string str3("John"); str1.print(); // what is printed ? str2.print(); str2 = str1; str3.copy("Ha ha"); str1.print(); // what is printed now ? str2.print(); }

Differences between copy constructor and assignment operator The copy constructor creates a new object. The assignment operator works on an already valid object.

Another example: overloading the [] operator class Array { private: int numElems; int *arr; public: Array(int); // constructor ~Array(); // destructor int& operator[](int); }; Array::Array(int n) { numElems = n; arr = new int[n]; } Array::~Array() { delete [] arr; }

int& Array::operator[](int index) { if ((index = numElems)) { cout << "Out of bounds error !!" << endl; exit(0); // error: invalid index !! } else return(arr[index]); } void main() { int i; Array A(10); for(i=0; i<=10; i++) // i=10: error !! A[i] = i; }

Comments on operator overloading Attempting to create new operators via operator overloading is a syntax error. Attempting to change the "arity" of an operator via operator overloading is a syntax error. Overloading is allowed only if at least one operand is a class instance (e.g., you cannot overload an operator to take two integers as operands).