Overview of C++ Overloading

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Operator overloading Object Oriented Programming.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Review of C++ Programming Part II Sheng-Fang Huang.
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++
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.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Operatorsand Operators Overloading. Introduction C++ allows operators to be overloaded specifically for a user-defined class. Operator overloading offers.
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 Operator Overloading allows a programmer to define new uses of the existing C/C++ operator symbols. –useful for defining common operations.
Overloading Operator MySting Example. Operator Overloading 1+2 Matrix M 1 + M 2 Using traditional operators with user-defined objects More convenient.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
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.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Overview of C++ Polymorphism
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
1 C++ Classes & Object Oriented Programming Overview & Terminology.
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:
Operator Overloading.
Chapter 18 - C++ Operator Overloading
Object-Oriented Programming (OOP) Lecture No. 16
Chapter 2 Objects and Classes
CSE1002 – Problem Solving with Object Oriented Programming
Operator Overloading CS 3370 – C++ Chapter 14.
Motivation for Generic Programming in C++
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 13: Overloading and Templates
C++ Templates.
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
Overloading Operator MySting Example
Object-Oriented Design (OOD) and C++
Object-Oriented Programming (OOP) Lecture No. 21
Function Overloading.
Inheritance & Polymorphism
Object-Oriented Programming (OOP) Lecture No. 16
Templates.
The dirty secrets of objects
Operator Overloading; String and Array Objects
Andy Wang Object Oriented Programming in C++ COP 3330
Advanced Program Design with C++
Operator Overloading; String and Array Objects
Abstraction: Generic Programming, pt. 2
Operator Overloading.
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,
COP 3330 Object-oriented Programming in C++
Overview of C++ Polymorphism
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Operator Overloading; String and Array Objects
Andy Wang Object Oriented Programming in C++ COP 3330
C++ Class Members Class Definition class Name { public: constructor(s)
C++ Object Oriented 1.
Presentation transcript:

Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both operators and functions can be overloaded Different definitions must be distinguished by their signatures (otherwise which to call is ambiguous) Reminder: signature is the operator/function name and the ordered list of its argument types E.g., add(int,long) and add(long,int) have different signatures E.g., add(const Base &) and add(const Derived &) have different signatures, even if Derived is-a Base Most specific match is used to select which one to call

Overloading vs. Overriding Overriding a base class member function is similar to overloading a function or operator But for overriding, definitions are distinguished by their scopes rather than by their signatures C++ can distinguish method definitions according to either static or dynamic type Depends on whether a method is virtual or not Depends on whether called via a reference or pointer vs. directly on an object Depends on whether the call states the scope explicitly (e.g., Foo::baz();)

Function Overloading class A { public: int add(int i, int j); // not allowed, would be // ambiguous with above: // long add(int m, int n); // Ok, different signature long add(long m, long n); }; int main (int argc, char **argv) { int a = 7; int b = 8; int c = add(a, b); return 0; } Calls to overloaded functions and operators are resolved by Finding all possible matches based on passed arguments May involve type promotion May involve instantiating templates Finding the “best match” among those possible matches Signature does not include the return type Which might not help even if it did, i.e., calls may ignore result So, overloading can’t be resolved by return type alone Compiler generates an error if the call can’t be resolved

Operator Overloading Similar to function overloading class A { friend ostream &operator<< (ostream &, const A &); private: int m_; }; ostream &operator<< (ostream &out, const A &a) { out << "A::m_ = " << a.m_; return out; } int main () { A a; cout << a << endl; return 0; Similar to function overloading Resolved by signature Best match is used But the list of operators and the “arity” of each is fixed Can’t invent operators (e.g., like ** for exponentiation ) Must use same number of arguments as for built-in types (except for operator()) Some operators are off limits :: (scope) ?: (conditional) .* (member dereference) . (member) sizeof typeid (RTTI)

Operator Symmetry, Precedence class Complex { public: // Constructor Complex (double r, double i); friend Complex operator* (const Complex &, const Complex &); // but not friend Complex operator^ // (const Complex &, const Complex &); private: int real_; int imaginary_; }; // multiplication works just fine Complex operator* (const Complex &, const Complex &); // exponentiation operator unworkable // Complex operator^ (const Complex &, // const Complex &); Make arithmetic operators symmetric As non-member friends Return result by value Don’t mix base and derived types in their parameter lists Operators for user-defined types obey the same precedence rules as for built-in types Can lead to some unexpected mistakes E.g., if uncommented exponentiation for a + b * c ^ 2

Member vs. Non-Member Overloading // declarations in .h file class A { public: friend bool operator< (const A &, const A &); A operator++(); // prefix A operator++(int); // postfix private: int m_; }; bool operator==(const A &lhs, const A &rhs); // definitions in .cpp file const A &rhs) {return lhs.m_ == rhs.m_;} A A::operator++() // prefix {++m_; return *this;} A A::operator++(int) // postfix {A ret(*this); ++*this; return ret;} Remember a this pointer is passed to any non-static member function Object doesn’t appear in parameters For non-member functions and operators all parameters are listed The rules about operator arity are obeyed in code on left Operator == is binary Prefix/postfix ++ are unary, parameter for postfix distinguishes its signature Must declare and define [] and = and -> and () as member operators Non-member operators are needed when working with classes you wrote and classes you didn’t write E.g., ostream << and istream >> Non-member operators are also useful to preserve symmetry E.g., for arithmetic/relational operators May help to avoid unexpected type conversions, especially with an inheritance hierarchy

Summary: Tips on Overloading Use virtual overriding when you want to substitute different subtypes polymorphically E.g., move() in derived and base classes Use overloading when you want to provide related interfaces to similar abstractions E.g., migrate(Bird &) vs. migrate(Elephant &) Make[] -> () = *= ++ -- etc. members Make << >> + * - / == < etc. non-members Use different names when the abstractions differ E.g., fly() versus walk()