More on Operator Overloading CS-2303, C-Term 20101 More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.

Slides:



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

Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
計算機程式 第十一單元 Operator Overloading I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 18 - C++ Operator Overloading Outline 18.1Introduction.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.
Loose endsCS-2301, B-Term “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
"Loose ends"CS-2301 D-term “Loose Ends” CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming Language, 2 nd edition,
CS Winter 2011 Introduction to the C programming language.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
2 Objectives You should be able to describe: Operator Functions Two Useful Alternatives – operator() and operator[] Data Type Conversions Class Inheritance.
Symbolic Constants in CCS-2303, C-Term Digression – Symbolic Constants in C CS-2303, System Programming Concepts (Slides include materials from The.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Chapter 15: Operator Overloading
 2008 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
CS-2303 System Programming Concepts
Operator OverloadingCS-2303, C-Term Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 18 - Operator Overloading Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 12: Adding Functionality to Your Classes.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
The Rest of the Story.  Constructors  Compiler-generated  The Initializer List  Copy Constructors  Single-arg (conversion ctors)  The Assignment.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Operator Overloading Like most languages, C++ supports a set of operators for its built-in types. Example: int x=2+3; // x=5 However, most concepts for.
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 types from the built-in types. –Operator Overloading is useful for redefining.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Chapter 1 C++ Templates (Sections 1.6, 1.7). Templates Type-independent patterns that can work with multiple data types. Function Templates  These define.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading; String and Array Objects.
Operator Overloading; String and Array Objects
Class: Special Topics Copy Constructors Static members Friends this
Pass by Reference, const, readonly, struct
Operator Overloading; String and Array Objects
Constructors and destructors
Classes, Constructors, etc., in C++
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Symbolic Constants in C
Programming Assignment #5
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
9-10 Classes: A Deeper Look.
Operator Overloading; String and Array Objects
A Deeper Look at Classes
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
9-10 Classes: A Deeper Look.
Introduction to Classes and Objects
Presentation transcript:

More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel, and from The C++ Programming Language, 3 rd edition, by Bjarne Stroustrup)

More on Operator Overloading CS-2303, C-Term Case Study — Array Class Problems with pointer-based arrays in C++:– –No range checking. –Cannot be compared meaningfully with == –No array assignment (array names are const pointers). –If array passed to a function, size must be passed as a separate argument. Can we do better? –I.e., can we create a data type that looks like an array but has the missing properties

More on Operator Overloading CS-2303, C-Term Point of Operator Overloading By using C++ classes and operator overloading, … … one can significantly change the capabilities of a built-in type In this case, of the built-in array type

More on Operator Overloading CS-2303, C-Term Case Study — Array Class Implement an Array class with:– –Range checking –Array assignment ( = ) –Arrays that know their own size. –Array comparisons with == and != –Output/input of entire arrays with > Deitel & Deitel, §22.8

More on Operator Overloading CS-2303, C-Term Array Class Copy Constructor Used whenever copy of object is needed: –Passing by value (return value or parameter). –Initializing an object with a copy of another of same type. Array newArray( oldArray ); or Array newArray = oldArray; –Both are identical –newArray is a copy of oldArray.

More on Operator Overloading CS-2303, C-Term Definition – Copy Constructor A constructor of some type T of the form T (const & T); The single argument must be a const reference to an existing object of same type Creates a duplicate of the existing object Used whenever a copy of an object is needed Including arguments to functions, results returned from functions Copy constructors always take reference argument! Reason: avoid infinite recursion!

More on Operator Overloading CS-2303, C-Term Most operators overloaded as member functions (except > which must be global functions) Prototype for copy constructor != operator simply returns opposite of == operator – only need to define the == operator Array Class Definition

More on Operator Overloading CS-2303, C-Term Array Class Definition Note weird return type:– constant reference Not explained in D&D

More on Operator Overloading CS-2303, C-Term Operators for accessing specific elements of Array object Note: An example of pointer data member Array Class Definition (continued)

More on Operator Overloading CS-2303, C-Term Array Class Implementation

More on Operator Overloading CS-2303, C-Term We must declare a new integer array so the objects do not point to the same memory Array Class Implementation (continued)

More on Operator Overloading CS-2303, C-Term Want to avoid self assignment This would be dangerous if this is the same Array as right Array Class Implementation (continued) Note that Copy Constructor and assignment are not the same thing. (But they are similar!)

More on Operator Overloading CS-2303, C-Term integers1[ 5 ] calls integers1.operator[]( 5 ) Array Class Implementation (continued)

More on Operator Overloading CS-2303, C-Term Array Class Implementation (continued) Should be some error checking here Notice the return type

More on Operator Overloading CS-2303, C-Term Array Class Implementation (continued) Notice the return type

More on Operator Overloading CS-2303, C-Term Retrieve number of elements in Array Use overloaded >> operator to input Array Class Usage

More on Operator Overloading CS-2303, C-Term Use overloaded << operator to output Use overloaded != operator to test for inequality Use copy constructor Use overloaded = operator to assign Array Class Usage (continued)

More on Operator Overloading CS-2303, C-Term Use overloaded == operator to test for equality Use overloaded [] operator to access individual integers, with range-checking Array Class Usage (continued)

More on Operator Overloading CS-2303, C-Term Questions?

More on Operator Overloading CS-2303, C-Term Note about Type Conversion Any single-argument constructor can be used as a type-conversion between the argument type and the object type May be invoked implicitly or explicitly Explict:– let a and b be complex a = b + complex(2.0) Implicit:– whenever compiler figures out it needs one type but has another a = b + 2.0/* NO overloaded ‘+’ operator for complex, real */

More on Operator Overloading CS-2303, C-Term Forcing explicit Type Conversion Sometimes, you don’t want compiler to make an implicit conversion Prefix constructor by explicit in type definition –Example:– (line 15 of Array class definition) explicit Array(int =10); –Suppresses use of this constructor for an implicit type conversion.

More on Operator Overloading CS-2303, C-Term Questions?