CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array.

Slides:



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

Chapter 8 Operator Overloading, Friends, and References.
Operator overloading redefine the operations of operators
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Structures Spring 2013Programming and Data Structure1.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
OOP Spring 2007 – Recitation 31 Object Oriented Programming Spring 2007 Recitation 3.
C++ data types. Structs vs. Classes C++ Classes.
Templates CS-341 Dick Steflik. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply certain attributes.
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
CMSC 2021 Stream I/O Operators and Friend Functions.
1 Overloading functions C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define.
CS 117 Spring 2002 Review for Exam 3 arrays strings files classes.
Templates. Example… A useful routine to have is void swap(int &a, int &b){ int tmp = a; a = b; b = tmp; }
C++ Operator Overloading Gordon College CPS212 Gordon College CPS212.
OOP Egar 2008 – Recitation 41 Object Oriented Programming Spring 2006 Recitation 6.
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Operator Overloading 1. Introduction Let’s define a class for Complex numbers: class Complex { private: double real, image; public: Complex () : real(0.0),
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
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+
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator 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.
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.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Overview of C++ Templates
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
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?
1 CMSC 202 ADTs and C++ Classes. 2 Announcements Project 1 due Sunday February 25 th at midnight – don’t be late! Notes and clarifications for Project.
11 Introduction to Object Oriented Programming (Continued) Cats.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 COMS 261 Computer Science I Title: Classes Date: November 9, 2005 Lecture Number: 29.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
COMP 3000 Object-Oriented Programming for Engineers and Scientists Operator Overloading Dr. Xiao Qin Auburn University
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Operator Overloading.
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?
Operator Overloading CMSC 202.
A First C++ Class – a Circle
More about OOP and ADTs Classes
Operator Overloading BCA Sem III K.I.R.A.S.
Templates.
Overloading the << operator
Name: Rubaisha Rajpoot
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.
More about OOP and ADTs Classes
Operator Overloading, Friends, and References
Overloading the << operator
C++ data types.
(4 – 2) Introduction to Classes in C++
Presentation transcript:

CMSC 202, Version 2/02 1 Operator Overloading Strong Suggestion: Go over the Array class example in Section 8.8 of your text. (You may ignore the Array copy constructor for now.)

CMSC 202, Version 2/02 2 Review -- Function Signatures A function signature is what the compiler and linker use to identify a function. In C, functions are identified only by their name. In C++, a function’s signature includes its name, parameters, and (for member functions) const. It does NOT include the return type.

CMSC 202, Version 2/02 3 A C++ swap( ) Function We still need separate functions, but they can all have the same name. –void swap (int& a, int& b); –void swap (double& a, double& b); –void swap (struct bob& a, struct bob& b);

CMSC 202, Version 2/02 4 Operator Overloading Overview Many C++ operator are already overloaded for primitive types. Examples: + - * / > It is often convenient for our classes to imitate the operations available on primitive types (e.g., + or - ). Then we can use the same concise notation for manipulating our objects.

CMSC 202, Version 2/02 5 A Complex Number Class class Complex { public: Complex (int real = 0, int imagine = 0); int getReal ( ) const; int getImagine ( ) const; void setReal (int n); void setImagine (int d); private: int real; int imagine; };

CMSC 202, Version 2/02 6 Using Complex Class It makes sense to want to perform mathematical operations with Complex objects. Complex C1 (3, 5), C2 (5, 9), C3; C3 = C1 + C2; // addition C2 = C3 * C1; // subtraction C1 = -C2; // negation

CMSC 202, Version 2/02 7 Operators Are Really Functions For user-defined types, when you use an operator, you are making a function call. Consider the expression: C2 + C1 –This is translated into a function call. –The name of the function is “operator+” –The call is: C2.operator+(C1);

CMSC 202, Version 2/02 8 Declaring operator+ As a Member Function class Complex { public: const Complex operator+ (const Complex &rhs) const; … }; Note all of the const’s!

CMSC 202, Version 2/02 9 operator+ Implementation const Complex Complex :: operator+ (const Complex &rhs) const { Complex sum; // accessor and mutators not required sum.imagine = imagine + rhs.imagine; // but preferred sum.setReal( getReal( ) + rhs.getReal ( ) ); return sum; }

CMSC 202, Version 2/02 10 Using operator+ We can now write C3 = C2 + C1; We can also use cascading operators. C4 = C3 + C2 + C1; And we can write C3 = C2 + 7; But C3 = 7 + C2 is a compiler error. (Why?)

CMSC 202, Version 2/02 11 operator+ As a Non-member, Non-friend const Complex operator+ (const Complex &lhs, // extra parameter const Complex &rhs) // not const { // must use accessors and mutators Complex sum; sum.setImagine (lhs.getImagine( ) + rhs.getImagine( ) ); sum.setReal (lhs.getReal ( ) + rhs.getReal( ) ); return sum; } // is now commutative

CMSC 202, Version 2/02 12 Declaring operator+ As a Non-member Friend Declare operator+ as a friend in the class definition. class Complex { public: friend const Complex operator+ (const Complex& a, const Complex& b); … };

CMSC 202, Version 2/02 13 Operator+ As a Non-member Friend (con’t) const Complex operator+ (const Complex& lhs, const Complex& rhs) { Complex sum; // accessors and mutators not required sum.imagine = lhs.imagine + rhs.imagine; // but preferred sum.setReal( lhs.getReal( ) + rhs.getReal( )) ; return sum; } // violates encapsulation! Non-friend better.

CMSC 202, Version 2/02 14 Printing Objects Each object should be responsible for printing itself. This guarantees objects are always printed the same way. It allows us to write intuitive output code: Complex C5 (5, 3); cout << C5 << endl;

CMSC 202, Version 2/02 15 Operator<< The insertion operator >, too. << is a binary operator. The left-hand operand is of type ostream& Therefore, operator<< cannot be a member function. It must be a non- member.

CMSC 202, Version 2/02 16 operator<< ostream& operator<< (ostream& out, const Complex& c) { out << c.getReal( ); int imagine = c.getImagine( ); out << (imagine < 0 ? “ - ” : “ + ” ) out << imagine << “i”; return out; } Could be, and often, is a friend Note: no endl

CMSC 202, Version 2/02 17 Operator<< Returns Type ‘ostream &’ Why? So we can write statements such as cout << C5 << “is a complex number” OR cout << C3 << endl << C2 << endl; << associates from left to right.

CMSC 202, Version 2/02 18 Overloading Unary Operators Complex C1(4, 5), C2; C2 = -C1; is an example of a unary operator (minus). We can and should overload this operator as a member function.

CMSC 202, Version 2/02 19 Unary operator- const Complex Complex :: operator- ( ) const { Complex x; x.real = -real; x.imagine = imagine; return x; }

CMSC 202, Version 2/02 20 Overloading = Remember that assignment performs a memberwise (shallow) copy by default. This is not sufficient when a data member is dynamically allocated. = must be overloaded to do a deep copy.

CMSC 202, Version 2/02 21 Restrictions Not all operators can be overloaded. You can’t make up your own operators. You can’t overload operators for primitive types (like int). You can’t change the precedence of an operator. You can’t change the associativity of an operator.

CMSC 202, Version 2/02 22 Good Programming Practices Overload operators so that they mimic the behavior of primitive data types. Overloaded binary arithmetic operators should –return const objects by value –be written as non-member functions when appropriate to allow commutativity –be written as non-friend functions (if data member accessors are available) Overload unary operators as member functions. Always overload << Always overload = for objects with dynamic data members.