Overloading CMPS 2143. Overloading A term is overloaded if it has many different meanings ▫ many English words are : eg. blue In programming languages,

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

Operator overloading redefine the operations of operators
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Chapter 7: User-Defined Functions II
ReviewSlides CMPS Advantages/Disadvantages Statically typed languages ▫ Type checking can be done at compile time ▫ Memory layout can be determined.
Class and Objects.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Overloading, Overriding
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),
Operator overloading Object Oriented Programming.
1 CSC241: Object Oriented Programming Lecture No 07.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
Chapter 12: Adding Functionality to Your Classes.
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
Case Study - Fractions Timothy Budd Oregon State University.
Introduction to Classes in C++
Operator Overloading & Exception Handling TCP1201 OOPDS 1 Lecture 5 1.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Operator Overloading Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Computer Science Department CPS 235 Object Oriented Programming Paradigm Lecturer Aisha Khalid Khan Operator Overloading.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
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.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
Object Oriented Programming
LECTURE LECTURE 13 Operator Overloading Textbook p.203—216 Today: const member functions Overloading Operators Postfix/infix increment.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
Polymorphism CMPS Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
CSCI-383 Object-Oriented Programming & Design Lecture 11.
1 CSC241: Object Oriented Programming Lecture No 08.
Dale Roberts Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
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.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Object Oriented Programming COP3330 / CGS5409.  Arithmetic Operator Overloading  Increment (++) / Decrement (--)  In-class exercise.
Operator Overloading.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 7: Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Operator Overloading BCA Sem III K.I.R.A.S.
Lecture Set 4 Data Types and Variables
Overview of C++ Overloading
Operator Overloading.
Operator Overloading.
COP 3330 Object-oriented Programming in C++
Chapter 7 Expressions and Assignment Statements.
PRESENTED BY ADNAN M. UZAIR NOMAN
Presentation transcript:

Overloading CMPS 2143

Overloading A term is overloaded if it has many different meanings ▫ many English words are : eg. blue In programming languages, it is usually function and method names and operators Also known as ad hoc polymorphism ▫ may be used in non-OO languages (hence the inclusion of functions) 2

Overloading vs Overriding Both take a function or method named in an invocation and select for execution one out of potentially many different function/method implementations Differences ▫ Overloading performed at compile time (early binding)  determined based on different signatures ▫ Overriding performed at runtime (late binding)  signatures the same, so based on dynamic values 3

Type Signatures Key to understanding overloading Used by compiler to determine correct function or method to call Description of argument types, their order, the return type ▫ overloaded methods – omit the type of receiver from the signature, but it is understood and determined by the context of the call double power (double base, int exponent) ▫ has signature double x int  double 4

Scope 2 nd key idea is scope – defines the portion of a program in which the name can be used ▫ name has meaning within braces of a function ▫ outside class declaration, if method not public, it has no meaning At any time in the static (textual) representation of a program there can be multiple active scopes ▫ within the class, then within a method in the class 5

2 broad categories of overloading 2 methods with same name and possibly same signature (as long as they are in different classes) ▫ different scopes ▫ if you use both classes, use Classname::methodname() to distinguish 2 methods with same name, but different signatures ▫ regardless of scope 6

Overloading based on Type signatures procedures, functions, methods are allowed to share a name Disambiguated by number, order, and in statically typed languages, the type of arguments they require ▫ EVEN IF THEY SHARE THE SAME SCOPE Occurs in C++, C#, Java, Delphi Pascal, CLOS, Ada, and many functional languages Good example is the overloading of the constructor methods ▫ default, parameterized, and copy constructors 7

Can do with other methods class Example { int sum (int a) {return a;} int sum (int a, int b) {return b;} int sum (int [] list, int n) { int tot=0; for (int i=0; i< n; i++) tot += list[i]; return sum; } }; 8

OPERATOR OVERLOADING Some languages allow operator overloading ▫ Java does not Giving the normal C++ operators such as +, -, *, <=, and += etc., additional meanings when they are applied to user-defined types (classes) You cannot create new operators like *& and try to overload them; only existing operators can be overloaded

OPERATOR OVELOADING The precedence of an existing operator cannot be changed by overloading The associativity cannot be changed The arity (number of arguments) cannot be changed The meaning of how an operator works on built-in types cannot be changed Operator overloading works only on objects of user defined types or with a mixture of user-defined and built-in types At least one argument of an operator function must be a class object or a reference to a class object

OPERATOR OVERLOADING Operators that cannot be overloaded “::”, “ ?: ”, “.” Operators that can be used without overloading “ = ” “, ” “&” Operator functions can be member functions or global friend functions ▫ I will discuss friends later

Creating a Member Operator Function return-type class-name::operator # (arg-list) { //operations } Creating a Global Operator Function return-type operator # (arg-list) { //operations }

OVERLOADING UNARY OPERATORS class Counter{ private: int count; public: Counter() {count=0; } int getcount() {return count;} void operator ++() { ++count;} }; // END OF CLASS void main() { Counter c1, c2; cout << “\nc1 = “ << c1.getcount(); cout << “\nc2 = “ << c2.getcount(); ++c1; //prefix ++c2; //prefix ++c2; //translated to // c2.operator++() by compiler cout << “\nc1 = “ << c1.getcount(); cout << “\nc2 = “ << c2.getcount();

OPERATOR RETURN VALUES In the previous example, you cannot write c1=++c2;//Error; Why? Counter operator ++() { ++count; Counter temp; temp.count=count; return temp; }

POSTFIX NOTATION Counter operator ++(int) { return Counter(count++); } The only difference is in the int in the parenthesis This isn’t really an argument, and it doesn’t mean an integer It is simply a signal to the compiler to create a postfix version of the operator

OVERLOADING BINARY OPERATORS class StringExt : public Object { public: //constructors StringExt(); StringExt (string s); StringExt (const StringExt & other); //overloaded + for concatenation StringExt operator + (StringExt se); StringExt operator + (int i); StringExt operator + (double r); StringExt operator + (string s); //overloaded = for assignment void operator = (string s); void operator = (StringExt se); //returns the string equivalent of the StringExt virtual string toString (); private: string val; };

OVERLOADING BINARY OPERATORS StringExt::StringExt (const StringExt & other) : Object("StringExt") {val = other.val;} StringExt StringExt::operator + (StringExt se) { StringExt result(val); result.val.append (se.val); return result; } void StringExt::operator = (StringExt se) {val = se.val;}

OVERLOADING BINARY OPERATORS #include “StringExt.h” void main() { StringExt se1(“Hi”), se2(“Howdy”), se3, se4; se3 = se1 + se2; //same as se1.operator + (se2) se4 = se1+ se2 + e3; //note cascading se4.toString(); }

In se3 = se1 + se2; The argument on the LEFT side of the + operator (se1 in this case) is the receiver object of which the operator is a member. The argument on the RIGHT side of the + operator (se2 in this case) must be furnished as an argument to the operator The operator returns a value which can be assigned or used in other ways; in this case it is assigned to se3 (using another overloaded operator =) An overloaded operator implemented as a member function always requires one less argument than its no. of operands, since one operand is the object of which the operator is a member (that is, it is the receiver object of the message). That is why unary operators require no arguments.

Creating non-member overloaded methods #include “StringExt.h” //prototypes:I/O for programmer-defined class StringExt ostream & operator << (ostream & cout, StringExt se); istream & operator >> (istream & cout, StringExt & se); // Implementations I/O for programmer-defined types ostream & operator << (ostream & cout, StringExt se) { cout << se.toString(); return cout; }

Creating non-member overloaded methods istream & operator >> (istream & source, StringExt & se) { string s; source >> s; StringExt se2(s); se = se2; return source; }

Using non-member overloaded methods void getStringExt (string prompt, StringExt & x) { cout << prompt; cin >> x; }

DEMO StringExt Example

Coercion and conversion integer + is radically different from floating-point + Sometimes overloading is NOT the only activity occurring, sometimes coercion goes on, especially with arithmetic operations ▫ occurs when we have mixed types In my example se+i or se+se or se+d or se+s ▫ no coercion occurs, just overloading If we had se + se and s + s, and no se + s ▫ then s could be coerced to an se ▫ even s+s could coerce both s to se

Substitution as conversion coercion – change in type principle of substitution introduces a form of coercion found in OOP languages ▫ Could use inheritance ▫ or implement an interface ▫ StringExt didn’t use inheritance from string  used composition

Overloading and Inheritance in Java Java has some subtle interactions If two or more methods have the same name and number of parameters, the compiler uses algorithm to determine match 1.find all methods that could possible apply to the method call and if one matches argument types exactly, use that one. 2.if a method’s parameter types are all assignments to any other method in the set, then eliminate the second method 3.If exactly one method remains, use it, else give compiler error

Example: Dessert, Pie, Cake, ApplePie, and ChocolateCake void order (Dessert d, Cake c); void order (Pie p, Dessert d); void order (ApplePie a, Cake c); order (aDessert, aCake); //legal - 1 order (anApplePie, aDessert); //legal - 2 order (aDessert, aDessert); //illegal //all eliminated in step 1, can’t downcast order (anApplePie, aChocolateCake); //legal //all in set 1, but 1,2 eliminated in step 2, //leaving 3 order (aPie, aCake); //illegal //3 elim. in step 1, but 2 and 3 survive step 2

Conversion C++ gives programmer control over how an instance of a class can be implicitly converted to another type ▫ use a constructor with a single argument ▫ rule to compiler to convert arg type to class type class ComplexNumber { public: ComplexNumber(); ComplexNumber(double r, double i); ComplexNumber (double r); : } ComplexNumber::ComplexNumber (double r) { real = r; imag = 0; }

ComplexNumber c(4.5,3.2); ComplexNumber result; double r = 5; result = c + r; //r will be converted to a //ComplexNumber and ComplexNumber + //will be performed

Conversion in other direction Make a Fraction a double. class Fraction { public: : operator double() { return numerator() / (double) denominator(); } }; Fraction f(2, 3); double d = f * 3.14; Coerce ComplexNumber to double ?

Redefinition Child class defines a method using same name as method in parent class, BUT with a different type signature ▫ so NO overriding here Ho hum? NO – depends on language Two different techniques ▫ merge model (Java)  creates a set and all possibilities are examined ▫ hierarchical model (C++)  determines which to use from inner scope outward

Polyadicity A polyadic function is one that can take a variable number of arguments ▫ cout << “sum is “” << sum << endl; Easy to write the call More difficult to write the body C++ uses an ad hoc method (involving a list, 3 methods, ellipsis C# has a more elegant solution ▫ public void Write (params object [ ] args) {…}

Study Questions pg. 307: 11-7, 10 program 4 extra credit: ▫ add the conversion for a double to a complex number and another case to your menu options and switch ▫ turn in program 4 a week early