ENERGY 211 / CME 211 Lecture 18 October 31, 2008.

Slides:



Advertisements
Similar presentations
Chapter 11 Operator Overloading; String and Array Objects Chapter 11 Operator Overloading; String and Array Objects Part I.
Advertisements

Introduction to Programming Lecture 31. Operator Overloading.
Operator Overloading Fundamentals
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Operator Overloading and Type Conversions
OPERATOR OVERLOADING. Closely related to function overloading is - operator overloading. In C++ you can overload most operators so that they perform special.
Chapter 12: Adding Functionality to Your Classes.
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.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Function and Operator Overloading. Overloading Review of function overloading –It is giving several definitions to a single function name –The compiler.
Operator Overloads Part2. Issue Provide member +(int) operator Rational + int OK int + Rational Error.
Templates ©Bruce M. Reynolds & Cliff Green1 C++ Programming Certificate University of Washington Cliff Green.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Midterm Review CS1220 Spring Disclaimer The following questions are representative of those that will appear on the midterm exam. They do not represent.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Operator overloading and type convesions BCAS,Bapatla B.mohini devi.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
OPERATOR OVERLOADING Understand Operator Overloading and how it is implemented in C++ ? | Website for students | VTU NOTES.
Operator Overloading Operator Overloading allows a programmer to define new types from the built-in types. –Operator Overloading is useful for redefining.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
1 ENERGY 211 / CME 211 Lecture 7 October 6, 2008.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Chapter 2 Objects and Classes
More About Objects and Methods
OOP: Encapsulation &Abstraction
Programming with ANSI C ++
C++ Templates.
Developed By : Ms. K. S. Kotecha
Object-Oriented Programming (OOP) Lecture No. 21
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
Constructor & Destructor
Operator Overloading BCA Sem III K.I.R.A.S.
Operator Overloading CSCE 121 J. Michael Moore
Operator Overloading
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Operator Overloading; String and Array Objects
Overview of C++ Overloading
Operator overloading Dr. Bhargavi Goswami
Standard Version of Starting Out with C++, 4th Edition
CISC/CMPE320 - Prof. McLeod
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Exceptions, Templates, and the Standard Template Library (STL)
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Parameters, Overloading Methods, and Random Garbage
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
ENERGY 211 / CME 211 Lecture 29 December 3, 2008.
Presentation transcript:

ENERGY 211 / CME 211 Lecture 18 October 31, 2008

Operator Overloading Built-in operators are implemented for fundamental types or pointer types Many of these operators make sense for other types of data, such as vectors or matrices, but in C, they must be implemented using ordinary functions Most operators in C++ can be overloaded to simplify these operations Overloads are functions with operands as arguments that return the result

Rules for Operands Overloads of [] and = must have left operand of class type If left operand is of class type, overload should be in class definition, and left operand is current object, not included in argument list If left operand not of class type, or of class type that can't be modified, overload is declared outside of any class, with all operands in argument list

Conversion Operators Overloading assignment operators is useful for implementing conversions Example: Complex& operator=(double x); Can have unintended consequences if multiple conversions can apply Can resolve ambiguity using explicit casts, adding conversion functions, or even more overloading to provide better match for compiler

Function Objects The () operator, used for calling functions, can also be overloaded This allows an object to be treated as a function Resulting function objects can store data in private members, allowing for persistence between calls and shorter argument lists Like [] and =, can only be overloaded for objects of class type

Next Time All about sequential containers vectors, lists, stacks and queues More about iterators More operations on strings